string - Valid printf() statements in C -


given that:

char *message = "hello, world"; char *format  = "x=%i\n"; int x = 10; 

why printf (message); invalid (i.e. rejected compiler being potentially insecure) , printf (format, x); isn't?

is format treated string literal in case , message format string? if so, why?


update


know why printf (message); rejected. question is, why printf (format, x); not rejected too.

i'm using clang. error message printf (message); format string not string literal (potentially insecure).

it compiles fine under gcc. appear compiler specific , how clang sets warnings.

you can warning in both cases enabling -wformat-nonliteral option, not included in either -wall or -wextra (but is in -weverything).

for whatever reason, seems intentional design decision emit security warning when non-literal printf statement takes no additional arguments. source code emits warning can found in lib/sema/semachecking.cpp:

  // if there no arguments specified, warn -wformat-security, otherwise   // warn -wformat-nonliteral.   if (args.size() == firstdataarg)     diag(args[format_idx]->getlocstart(),          diag::warn_format_nonliteral_noargs)       << origformatexpr->getsourcerange();   else     diag(args[format_idx]->getlocstart(),          diag::warn_format_nonliteral)            << origformatexpr->getsourcerange(); 

i'd guess compatibility existing legacy code, that's pure speculation.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -