• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: "Format not a string literal and no format arguments"
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "Format not a string literal and no format arguments"


  • Subject: Re: "Format not a string literal and no format arguments"
  • From: Quincey Morris <email@hidden>
  • Date: Sat, 29 Aug 2009 12:19:59 -0700

On Aug 29, 2009, at 11:54, Peter Duniho wrote:

Or is it implied that the compiler is doing compile-time verification of the format string when a literal is provided?

Sort of, but not exactly. The first parameter to NSLog is *really* a format string, not just a string. If an arbitrary string passed as the first parameter happens to contain a stray '%' character, that's likely going to result in a crash.


So if you provide a string literal, there's no warning because it's assumed you're not going to hard-code stray '%' characters in it. If you provide arguments after the string (whether literal or not), it's assumed you've made sure the string really is a format string. If you provide a non-ilteral string and no arguments, there's a fair chance that you've forgotten that some potential string values will blow up, hence the warning.

The compiler's reasoning is heuristic, not impeccable. You can certainly do the wrong thing using a string literal too (or by giving the wrong number of arguments, for that matter), but the warning is generated in a commonly-overlooked case, not in all potentially buggy cases.

In Jonathan's case, the string *looked* like a literal in the source code, but wasn't, because of his compile options. Presumably his non- literal string is never going to get changed to something bad, but he'll have to code around it to keep the compiler from complaining.


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: "Format not a string literal and no format arguments"
      • From: Ken Thomases <email@hidden>
References: 
 >"Format not a string literal and no format arguments" (From: Jonathan del Strother <email@hidden>)
 >Re: "Format not a string literal and no format arguments" (From: Quincey Morris <email@hidden>)
 >Re: "Format not a string literal and no format arguments" (From: Peter Duniho <email@hidden>)

  • Prev by Date: Re: "Format not a string literal and no format arguments"
  • Next by Date: Re: "Format not a string literal and no format arguments"
  • Previous by thread: Re: "Format not a string literal and no format arguments"
  • Next by thread: Re: "Format not a string literal and no format arguments"
  • Index(es):
    • Date
    • Thread