I can find nothing in any spec which would shed light on what the correct behavior is for the warning, but it seems unlikely to be important to give fprintf a pointer to a volatile int as opposed to a normal int....<div class="gmail_extra">
<br><br><div class="gmail_quote">On Tue, Jul 31, 2012 at 12:27 AM, Alexey Samsonov <span dir="ltr"><<a href="mailto:samsonov@google.com" target="_blank">samsonov@google.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hello Hans,<div><br></div><div>After this patch one of our tests failed with the following error:</div><div><pre><span style="font-family:'Courier New',courier,monotype">error: format specifies type 'int *' but the argument has type 'volatile int *' [-Werror,-Wformat]
fprintf(stdout, "printing a string: %s%n\n", s, &a);
~~ ^~</span></pre><div class="gmail_quote">Do you think that we should fix the code, or Clang shouldn't produce the warning in this case?</div><div><div class="h5"><div class="gmail_quote">
<br>
</div><div class="gmail_quote">On Mon, Jul 30, 2012 at 9:11 PM, Hans Wennborg <span dir="ltr"><<a href="mailto:hans@hanshq.net" target="_blank">hans@hanshq.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Author: hans<br>
Date: Mon Jul 30 12:11:32 2012<br>
New Revision: 160966<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=160966&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=160966&view=rev</a><br>
Log:<br>
Make -Wformat check the argument type for %n.<br>
<br>
This makes Clang check that the corresponding argument for "%n" in a<br>
format string is a pointer to int.<br>
<br>
Modified:<br>
cfe/trunk/lib/Analysis/PrintfFormatString.cpp<br>
cfe/trunk/lib/Analysis/ScanfFormatString.cpp<br>
cfe/trunk/lib/Sema/SemaChecking.cpp<br>
cfe/trunk/test/Sema/format-strings-scanf.c<br>
cfe/trunk/test/Sema/format-strings.c<br>
<br>
Modified: cfe/trunk/lib/Analysis/PrintfFormatString.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/PrintfFormatString.cpp?rev=160966&r1=160965&r2=160966&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/PrintfFormatString.cpp?rev=160966&r1=160965&r2=160966&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/Analysis/PrintfFormatString.cpp (original)<br>
+++ cfe/trunk/lib/Analysis/PrintfFormatString.cpp Mon Jul 30 12:11:32 2012<br>
@@ -330,6 +330,8 @@<br>
return ArgTypeResult(Ctx.WCharTy, "wchar_t");<br>
case ConversionSpecifier::pArg:<br>
return ArgTypeResult::CPointerTy;<br>
+ case ConversionSpecifier::nArg:<br>
+ return Ctx.getPointerType(Ctx.IntTy);<br>
case ConversionSpecifier::ObjCObjArg:<br>
return ArgTypeResult::ObjCPointerTy;<br>
default:<br>
@@ -342,6 +344,10 @@<br>
<br>
bool PrintfSpecifier::fixType(QualType QT, const LangOptions &LangOpt,<br>
ASTContext &Ctx, bool IsObjCLiteral) {<br>
+ // %n is different from other conversion specifiers; don't try to fix it.<br>
+ if (CS.getKind() == ConversionSpecifier::nArg)<br>
+ return false;<br>
+<br>
// Handle Objective-C objects first. Note that while the '%@' specifier will<br>
// not warn for structure pointer or void pointer arguments (because that's<br>
// how CoreFoundation objects are implemented), we only show a fixit for '%@'<br>
<br>
Modified: cfe/trunk/lib/Analysis/ScanfFormatString.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/ScanfFormatString.cpp?rev=160966&r1=160965&r2=160966&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/ScanfFormatString.cpp?rev=160966&r1=160965&r2=160966&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/Analysis/ScanfFormatString.cpp (original)<br>
+++ cfe/trunk/lib/Analysis/ScanfFormatString.cpp Mon Jul 30 12:11:32 2012<br>
@@ -303,6 +303,9 @@<br>
case ConversionSpecifier::pArg:<br>
return ScanfArgTypeResult(ArgTypeResult(ArgTypeResult::CPointerTy));<br>
<br>
+ case ConversionSpecifier::nArg:<br>
+ return ArgTypeResult(Ctx.IntTy);<br>
+<br>
default:<br>
break;<br>
}<br>
@@ -315,6 +318,10 @@<br>
if (!QT->isPointerType())<br>
return false;<br>
<br>
+ // %n is different from other conversion specifiers; don't try to fix it.<br>
+ if (CS.getKind() == ConversionSpecifier::nArg)<br>
+ return false;<br>
+<br>
QualType PT = QT->getPointeeType();<br>
<br>
// If it's an enum, get its underlying type.<br>
<br>
Modified: cfe/trunk/lib/Sema/SemaChecking.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=160966&r1=160965&r2=160966&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=160966&r1=160965&r2=160966&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)<br>
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Mon Jul 30 12:11:32 2012<br>
@@ -2568,8 +2568,6 @@<br>
getLocationOfByte(CS.getStart()),<br>
/*IsStringLocation*/true,<br>
getSpecifierRange(startSpecifier, specifierLen));<br>
- // Continue checking the other format specifiers.<br>
- return true;<br>
}<br>
<br>
// The remaining checks depend on the data arguments.<br>
<br>
Modified: cfe/trunk/test/Sema/format-strings-scanf.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/format-strings-scanf.c?rev=160966&r1=160965&r2=160966&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/format-strings-scanf.c?rev=160966&r1=160965&r2=160966&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/test/Sema/format-strings-scanf.c (original)<br>
+++ cfe/trunk/test/Sema/format-strings-scanf.c Mon Jul 30 12:11:32 2012<br>
@@ -121,3 +121,8 @@<br>
scanf("%qd", x); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}}<br>
scanf("%qd", llx); // no-warning<br>
}<br>
+<br>
+void test_writeback(int *x) {<br>
+ scanf("%n", (void*)0); // expected-warning{{format specifies type 'int *' but the argument has type 'void *'}}<br>
+ scanf("%n %c", x, x); // expected-warning{{format specifies type 'char *' but the argument has type 'int *'}}<br>
+}<br>
<br>
Modified: cfe/trunk/test/Sema/format-strings.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/format-strings.c?rev=160966&r1=160965&r2=160966&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/format-strings.c?rev=160966&r1=160965&r2=160966&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/test/Sema/format-strings.c (original)<br>
+++ cfe/trunk/test/Sema/format-strings.c Mon Jul 30 12:11:32 2012<br>
@@ -91,6 +91,7 @@<br>
<br>
printf("%n",&x); // expected-warning {{'%n' in format string discouraged}}<br>
sprintf(b,"%d%%%n",1, &x); // expected-warning {{'%n' in format string dis}}<br>
+ printf("%n",b); // expected-warning {{'%n' in format string discouraged}} expected-warning{{format specifies type 'int *' but the argument has type 'char *'}}<br>
}<br>
<br>
void check_invalid_specifier(FILE* fp, char *buf)<br>
@@ -316,14 +317,14 @@<br>
// Bad flag usage<br>
printf("%#p", (void *) 0); // expected-warning{{flag '#' results in undefined behavior with 'p' conversion specifier}}<br>
printf("%0d", -1); // no-warning<br>
- printf("%#n", (void *) 0); // expected-warning{{flag '#' results in undefined behavior with 'n' conversion specifier}} expected-warning{{use of '%n' in format string discouraged (potentially insecure)}}<br>
- printf("%-n", (void *) 0); // expected-warning{{flag '-' results in undefined behavior with 'n' conversion specifier}} expected-warning{{use of '%n' in format string discouraged (potentially insecure)}}<br>
+ printf("%#n", (int *) 0); // expected-warning{{flag '#' results in undefined behavior with 'n' conversion specifier}} expected-warning{{use of '%n' in format string discouraged (potentially insecure)}}<br>
+ printf("%-n", (int *) 0); // expected-warning{{flag '-' results in undefined behavior with 'n' conversion specifier}} expected-warning{{use of '%n' in format string discouraged (potentially insecure)}}<br>
printf("%-p", (void *) 0); // no-warning<br>
<br>
// Bad optional amount use<br>
printf("%.2c", 'a'); // expected-warning{{precision used with 'c' conversion specifier, resulting in undefined behavior}}<br>
- printf("%1n", (void *) 0); // expected-warning{{field width used with 'n' conversion specifier, resulting in undefined behavior}} expected-warning{{use of '%n' in format string discouraged (potentially insecure)}}<br>
- printf("%.9n", (void *) 0); // expected-warning{{precision used with 'n' conversion specifier, resulting in undefined behavior}} expected-warning{{use of '%n' in format string discouraged (potentially insecure)}}<br>
+ printf("%1n", (int *) 0); // expected-warning{{field width used with 'n' conversion specifier, resulting in undefined behavior}} expected-warning{{use of '%n' in format string discouraged (potentially insecure)}}<br>
+ printf("%.9n", (int *) 0); // expected-warning{{precision used with 'n' conversion specifier, resulting in undefined behavior}} expected-warning{{use of '%n' in format string discouraged (potentially insecure)}}<br>
<br>
// Ignored flags<br>
printf("% +f", 1.23); // expected-warning{{flag ' ' is ignored when flag '+' is present}}<br>
@@ -436,8 +437,9 @@<br>
printf("%18$s\n", 1, "foo"); // expected-warning{{data argument position '18' exceeds the number of data arguments (2)}}<br>
<br>
const char kFormat3[] = "%n"; // expected-note{{format string is defined here}}<br>
- printf(kFormat3, "as"); // expected-warning{{use of '%n' in format string discouraged}}<br>
- printf("%n", "as"); // expected-warning{{use of '%n' in format string discouraged}}<br>
+ printf(kFormat3, (int*)NULL); // expected-warning{{use of '%n' in format string discouraged}}<br>
+ printf("%n", (int*)NULL); // expected-warning{{use of '%n' in format string discouraged}}<br>
+<br>
<br>
const char kFormat4[] = "%y"; // expected-note{{format string is defined here}}<br>
printf(kFormat4, 5); // expected-warning{{invalid conversion specifier 'y'}}<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu" target="_blank">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br><br clear="all"><div><br></div></div></div><span class="HOEnZb"><font color="#888888">-- <br><div>Alexey Samsonov, MSK</div><br>
</font></span></div>
<br>_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
<br></blockquote></div><br></div>