format-strings-scanf.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral %s
  2. // Test that -Wformat=0 works:
  3. // RUN: %clang_cc1 -fsyntax-only -Werror -Wformat=0 %s
  4. #include <stdarg.h>
  5. typedef __typeof(sizeof(int)) size_t;
  6. typedef struct _FILE FILE;
  7. typedef __WCHAR_TYPE__ wchar_t;
  8. int fscanf(FILE * restrict, const char * restrict, ...) ;
  9. int scanf(const char * restrict, ...) ;
  10. int sscanf(const char * restrict, const char * restrict, ...) ;
  11. int my_scanf(const char * restrict, ...) __attribute__((__format__(__scanf__, 1, 2)));
  12. int vscanf(const char * restrict, va_list);
  13. int vfscanf(FILE * restrict, const char * restrict, va_list);
  14. int vsscanf(const char * restrict, const char * restrict, va_list);
  15. void test(const char *s, int *i) {
  16. scanf(s, i); // expected-warning{{ormat string is not a string literal}}
  17. scanf("%0d", i); // expected-warning{{zero field width in scanf format string is unused}}
  18. scanf("%00d", i); // expected-warning{{zero field width in scanf format string is unused}}
  19. scanf("%d%[asdfasdfd", i, s); // expected-warning{{no closing ']' for '%[' in scanf format string}}
  20. unsigned short s_x;
  21. scanf ("%" "hu" "\n", &s_x); // no-warning
  22. scanf("%y", i); // expected-warning{{invalid conversion specifier 'y'}}
  23. scanf("%%"); // no-warning
  24. scanf("%%%1$d", i); // no-warning
  25. scanf("%1$d%%", i); // no-warning
  26. scanf("%d", i, i); // expected-warning{{data argument not used by format string}}
  27. scanf("%*d", i); // // expected-warning{{data argument not used by format string}}
  28. scanf("%*d", i); // // expected-warning{{data argument not used by format string}}
  29. scanf("%*d%1$d", i); // no-warning
  30. scanf("%s", (char*)0); // no-warning
  31. scanf("%s", (volatile char*)0); // no-warning
  32. scanf("%s", (signed char*)0); // no-warning
  33. scanf("%s", (unsigned char*)0); // no-warning
  34. scanf("%hhu", (signed char*)0); // no-warning
  35. }
  36. void bad_length_modifiers(char *s, void *p, wchar_t *ws, long double *ld) {
  37. scanf("%hhs", "foo"); // expected-warning{{length modifier 'hh' results in undefined behavior or no effect with 's' conversion specifier}}
  38. scanf("%1$zp", &p); // expected-warning{{length modifier 'z' results in undefined behavior or no effect with 'p' conversion specifier}}
  39. scanf("%ls", ws); // no-warning
  40. scanf("%#.2Lf", ld); // expected-warning{{invalid conversion specifier '#'}}
  41. }
  42. // Test that the scanf call site is where the warning is attached. If the
  43. // format string is somewhere else, point to it in a note.
  44. void pr9751() {
  45. int *i;
  46. char str[100];
  47. const char kFormat1[] = "%00d"; // expected-note{{format string is defined here}}}
  48. scanf(kFormat1, i); // expected-warning{{zero field width in scanf format string is unused}}
  49. scanf("%00d", i); // expected-warning{{zero field width in scanf format string is unused}}
  50. const char kFormat2[] = "%["; // expected-note{{format string is defined here}}}
  51. scanf(kFormat2, str); // expected-warning{{no closing ']' for '%[' in scanf format string}}
  52. scanf("%[", str); // expected-warning{{no closing ']' for '%[' in scanf format string}}
  53. const char kFormat3[] = "%hu"; // expected-note{{format string is defined here}}}
  54. scanf(kFormat3, &i); // expected-warning {{format specifies type 'unsigned short *' but the argument}}
  55. const char kFormat4[] = "%lp"; // expected-note{{format string is defined here}}}
  56. scanf(kFormat4, &i); // expected-warning {{length modifier 'l' results in undefined behavior or no effect with 'p' conversion specifier}}
  57. }
  58. void test_variants(int *i, const char *s, ...) {
  59. FILE *f = 0;
  60. char buf[100];
  61. fscanf(f, "%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
  62. sscanf(buf, "%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
  63. my_scanf("%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
  64. va_list ap;
  65. va_start(ap, s);
  66. vscanf("%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
  67. vfscanf(f, "%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
  68. vsscanf(buf, "%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}}
  69. }
  70. void test_scanlist(int *ip, char *sp, wchar_t *ls) {
  71. scanf("%[abc]", ip); // expected-warning{{format specifies type 'char *' but the argument has type 'int *'}}
  72. scanf("%h[abc]", sp); // expected-warning{{length modifier 'h' results in undefined behavior or no effect with '[' conversion specifier}}
  73. scanf("%l[xyx]", ls); // no-warning
  74. scanf("%ll[xyx]", ls); // expected-warning {{length modifier 'll' results in undefined behavior or no effect with '[' conversion specifier}}
  75. // PR19559
  76. scanf("%[]% ]", sp); // no-warning
  77. scanf("%[^]% ]", sp); // no-warning
  78. scanf("%[a^]% ]", sp); // expected-warning {{invalid conversion specifier ' '}}
  79. }
  80. void test_alloc_extension(char **sp, wchar_t **lsp, float *fp) {
  81. /* Make sure "%a" gets parsed as a conversion specifier for float,
  82. * even when followed by an 's', 'S' or '[', which would cause it to be
  83. * parsed as a length modifier in C90. */
  84. scanf("%as", sp); // expected-warning{{format specifies type 'float *' but the argument has type 'char **'}}
  85. scanf("%aS", lsp); // expected-warning{{format specifies type 'float *' but the argument has type 'wchar_t **'}}
  86. scanf("%a[bcd]", sp); // expected-warning{{format specifies type 'float *' but the argument has type 'char **'}}
  87. // Test that the 'm' length modifier is only allowed with s, S, c, C or [.
  88. // TODO: Warn that 'm' is an extension.
  89. scanf("%ms", sp); // No warning.
  90. scanf("%mS", lsp); // No warning.
  91. scanf("%mc", sp); // No warning.
  92. scanf("%mC", lsp); // No warning.
  93. scanf("%m[abc]", sp); // No warning.
  94. scanf("%md", sp); // expected-warning{{length modifier 'm' results in undefined behavior or no effect with 'd' conversion specifier}}
  95. // Test argument type check for the 'm' length modifier.
  96. scanf("%ms", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}}
  97. scanf("%mS", fp); // expected-warning-re{{format specifies type 'wchar_t **' (aka '{{[^']+}}') but the argument has type 'float *'}}
  98. scanf("%mc", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}}
  99. scanf("%mC", fp); // expected-warning-re{{format specifies type 'wchar_t **' (aka '{{[^']+}}') but the argument has type 'float *'}}
  100. scanf("%m[abc]", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}}
  101. }
  102. void test_quad(int *x, long long *llx) {
  103. scanf("%qd", x); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}}
  104. scanf("%qd", llx); // no-warning
  105. }
  106. void test_writeback(int *x) {
  107. scanf("%n", (void*)0); // expected-warning{{format specifies type 'int *' but the argument has type 'void *'}}
  108. scanf("%n %c", x, x); // expected-warning{{format specifies type 'char *' but the argument has type 'int *'}}
  109. scanf("%hhn", (signed char*)0); // no-warning
  110. scanf("%hhn", (char*)0); // no-warning
  111. scanf("%hhn", (unsigned char*)0); // no-warning
  112. scanf("%hhn", (int*)0); // expected-warning{{format specifies type 'signed char *' but the argument has type 'int *'}}
  113. scanf("%hn", (short*)0); // no-warning
  114. scanf("%hn", (unsigned short*)0); // no-warning
  115. scanf("%hn", (int*)0); // expected-warning{{format specifies type 'short *' but the argument has type 'int *'}}
  116. scanf("%n", (int*)0); // no-warning
  117. scanf("%n", (unsigned int*)0); // no-warning
  118. scanf("%n", (char*)0); // expected-warning{{format specifies type 'int *' but the argument has type 'char *'}}
  119. scanf("%ln", (long*)0); // no-warning
  120. scanf("%ln", (unsigned long*)0); // no-warning
  121. scanf("%ln", (int*)0); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}}
  122. scanf("%lln", (long long*)0); // no-warning
  123. scanf("%lln", (unsigned long long*)0); // no-warning
  124. scanf("%lln", (int*)0); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}}
  125. scanf("%qn", (long long*)0); // no-warning
  126. scanf("%qn", (unsigned long long*)0); // no-warning
  127. scanf("%qn", (int*)0); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}}
  128. }
  129. void test_qualifiers(const int *cip, volatile int* vip,
  130. const char *ccp, volatile char* vcp,
  131. const volatile int *cvip) {
  132. scanf("%d", cip); // expected-warning{{format specifies type 'int *' but the argument has type 'const int *'}}
  133. scanf("%n", cip); // expected-warning{{format specifies type 'int *' but the argument has type 'const int *'}}
  134. scanf("%s", ccp); // expected-warning{{format specifies type 'char *' but the argument has type 'const char *'}}
  135. scanf("%d", cvip); // expected-warning{{format specifies type 'int *' but the argument has type 'const volatile int *'}}
  136. scanf("%d", vip); // No warning.
  137. scanf("%n", vip); // No warning.
  138. scanf("%c", vcp); // No warning.
  139. typedef int* ip_t;
  140. typedef const int* cip_t;
  141. scanf("%d", (ip_t)0); // No warning.
  142. scanf("%d", (cip_t)0); // expected-warning{{format specifies type 'int *' but the argument has type 'cip_t' (aka 'const int *')}}
  143. }