| 1 |
// RUN: %clang_cc1 -std=c11 -fsyntax-only -verify -Wformat-nonliteral %s |
| 2 |
|
| 3 |
// Test that -Wformat=0 works: |
| 4 |
// RUN: %clang_cc1 -std=c11 -fsyntax-only -Werror -Wformat=0 %s |
| 5 |
|
| 6 |
#include <stdarg.h> |
| 7 |
typedef __SIZE_TYPE__ size_t; |
| 8 |
#define __SSIZE_TYPE__ \ |
| 9 |
__typeof__(_Generic((__SIZE_TYPE__)0, \ |
| 10 |
unsigned long long int : (long long int)0, \ |
| 11 |
unsigned long int : (long int)0, \ |
| 12 |
unsigned int : (int)0, \ |
| 13 |
unsigned short : (short)0, \ |
| 14 |
unsigned char : (signed char)0)) |
| 15 |
typedef __SSIZE_TYPE__ ssize_t; |
| 16 |
|
| 17 |
typedef __PTRDIFF_TYPE__ ptrdiff_t; |
| 18 |
#define __UNSIGNED_PTRDIFF_TYPE__ \ |
| 19 |
__typeof__(_Generic((__PTRDIFF_TYPE__)0, \ |
| 20 |
long long int : (unsigned long long int)0, \ |
| 21 |
long int : (unsigned long int)0, \ |
| 22 |
int : (unsigned int)0, \ |
| 23 |
short : (unsigned short)0, \ |
| 24 |
signed char : (unsigned char)0)) |
| 25 |
|
| 26 |
typedef struct _FILE FILE; |
| 27 |
typedef __WCHAR_TYPE__ wchar_t; |
| 28 |
|
| 29 |
int fscanf(FILE * restrict, const char * restrict, ...) ; |
| 30 |
int scanf(const char * restrict, ...) ; |
| 31 |
int sscanf(const char * restrict, const char * restrict, ...) ; |
| 32 |
int my_scanf(const char * restrict, ...) __attribute__((__format__(__scanf__, 1, 2))); |
| 33 |
int my_gnu_scanf(const char * restrict, ...) __attribute__((__format__(gnu_scanf, 1, 2))); |
| 34 |
|
| 35 |
int vscanf(const char * restrict, va_list); |
| 36 |
int vfscanf(FILE * restrict, const char * restrict, va_list); |
| 37 |
int vsscanf(const char * restrict, const char * restrict, va_list); |
| 38 |
|
| 39 |
void test(const char *s, int *i) { // #test |
| 40 |
scanf(s, i); // expected-warning{{diagnostic behavior may be improved by adding the 'format(scanf, 1, 2)' attribute to the declaration of 'test'}} |
| 41 |
// expected-note@#test {{'test' declared here}} |
| 42 |
scanf("%0d", i); // expected-warning{{zero field width in scanf format string is unused}} |
| 43 |
scanf("%00d", i); // expected-warning{{zero field width in scanf format string is unused}} |
| 44 |
scanf("%d%[asdfasdfd", i, s); // expected-warning{{no closing ']' for '%[' in scanf format string}} |
| 45 |
scanf("%B", i); // expected-warning{{invalid conversion specifier 'B'}} |
| 46 |
|
| 47 |
unsigned short s_x; |
| 48 |
scanf ("%" "hu" "\n", &s_x); // no-warning |
| 49 |
scanf("%hb", &s_x); |
| 50 |
scanf("%y", i); // expected-warning{{invalid conversion specifier 'y'}} |
| 51 |
scanf("%%"); // no-warning |
| 52 |
scanf("%%%1$d", i); // no-warning |
| 53 |
scanf("%1$d%%", i); // no-warning |
| 54 |
scanf("%d", i, i); // expected-warning{{data argument not used by format string}} |
| 55 |
scanf("%*d", i); // // expected-warning{{data argument not used by format string}} |
| 56 |
scanf("%*d", i); // // expected-warning{{data argument not used by format string}} |
| 57 |
scanf("%*d%1$d", i); // no-warning |
| 58 |
|
| 59 |
scanf("%s", (char*)0); // no-warning |
| 60 |
scanf("%s", (volatile char*)0); // no-warning |
| 61 |
scanf("%s", (signed char*)0); // no-warning |
| 62 |
scanf("%s", (unsigned char*)0); // no-warning |
| 63 |
scanf("%hhu", (signed char*)0); // no-warning |
| 64 |
scanf("%hhb", (signed char*)0); // no-warning |
| 65 |
} |
| 66 |
|
| 67 |
void bad_length_modifiers(char *s, void *p, wchar_t *ws, long double *ld) { |
| 68 |
scanf("%hhs", "foo"); // expected-warning{{length modifier 'hh' results in undefined behavior or no effect with 's' conversion specifier}} |
| 69 |
scanf("%1$zp", &p); // expected-warning{{length modifier 'z' results in undefined behavior or no effect with 'p' conversion specifier}} |
| 70 |
scanf("%ls", ws); // no-warning |
| 71 |
scanf("%#.2Lf", ld); // expected-warning{{invalid conversion specifier '#'}} |
| 72 |
} |
| 73 |
|
| 74 |
void missing_argument_with_length_modifier() { |
| 75 |
char buf[30]; |
| 76 |
scanf("%s:%900s", buf); // expected-warning{{more '%' conversions than data arguments}} |
| 77 |
} |
| 78 |
|
| 79 |
// Test that the scanf call site is where the warning is attached. If the |
| 80 |
// format string is somewhere else, point to it in a note. |
| 81 |
void pr9751(void) { |
| 82 |
int *i; |
| 83 |
char str[100]; |
| 84 |
const char kFormat1[] = "%00d"; // expected-note{{format string is defined here}}} |
| 85 |
scanf(kFormat1, i); // expected-warning{{zero field width in scanf format string is unused}} |
| 86 |
scanf("%00d", i); // expected-warning{{zero field width in scanf format string is unused}} |
| 87 |
const char kFormat2[] = "%["; // expected-note{{format string is defined here}}} |
| 88 |
scanf(kFormat2, str); // expected-warning{{no closing ']' for '%[' in scanf format string}} |
| 89 |
scanf("%[", str); // expected-warning{{no closing ']' for '%[' in scanf format string}} |
| 90 |
const char kFormat3[] = "%hu"; // expected-note{{format string is defined here}}} |
| 91 |
scanf(kFormat3, &i); // expected-warning {{format specifies type 'unsigned short *' but the argument}} |
| 92 |
const char kFormat4[] = "%lp"; // expected-note{{format string is defined here}}} |
| 93 |
scanf(kFormat4, &i); // expected-warning {{length modifier 'l' results in undefined behavior or no effect with 'p' conversion specifier}} |
| 94 |
} |
| 95 |
|
| 96 |
void test_variants(int *i, const char *s, ...) { |
| 97 |
FILE *f = 0; |
| 98 |
char buf[100]; |
| 99 |
|
| 100 |
fscanf(f, "%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}} |
| 101 |
sscanf(buf, "%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}} |
| 102 |
my_scanf("%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}} |
| 103 |
my_gnu_scanf("%ld", i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}} |
| 104 |
|
| 105 |
va_list ap; |
| 106 |
va_start(ap, s); |
| 107 |
|
| 108 |
vscanf("%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}} |
| 109 |
vfscanf(f, "%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}} |
| 110 |
vsscanf(buf, "%[abc", ap); // expected-warning{{no closing ']' for '%[' in scanf format string}} |
| 111 |
} |
| 112 |
|
| 113 |
void test_scanlist(int *ip, char *sp, wchar_t *ls) { |
| 114 |
scanf("%[abc]", ip); // expected-warning{{format specifies type 'char *' but the argument has type 'int *'}} |
| 115 |
scanf("%h[abc]", sp); // expected-warning{{length modifier 'h' results in undefined behavior or no effect with '[' conversion specifier}} |
| 116 |
scanf("%l[xyx]", ls); // no-warning |
| 117 |
scanf("%ll[xyx]", ls); // expected-warning {{length modifier 'll' results in undefined behavior or no effect with '[' conversion specifier}} |
| 118 |
|
| 119 |
// PR19559 |
| 120 |
scanf("%[]% ]", sp); // no-warning |
| 121 |
scanf("%[^]% ]", sp); // no-warning |
| 122 |
scanf("%[a^]% ]", sp); // expected-warning {{invalid conversion specifier ' '}} |
| 123 |
} |
| 124 |
|
| 125 |
void test_alloc_extension(char **sp, wchar_t **lsp, float *fp) { |
| 126 |
/* Make sure "%a" gets parsed as a conversion specifier for float, |
| 127 |
* even when followed by an 's', 'S' or '[', which would cause it to be |
| 128 |
* parsed as a length modifier in C90. */ |
| 129 |
scanf("%as", sp); // expected-warning{{format specifies type 'float *' but the argument has type 'char **'}} |
| 130 |
scanf("%aS", lsp); // expected-warning{{format specifies type 'float *' but the argument has type 'wchar_t **'}} |
| 131 |
scanf("%a[bcd]", sp); // expected-warning{{format specifies type 'float *' but the argument has type 'char **'}} |
| 132 |
|
| 133 |
// Test that the 'm' length modifier is only allowed with s, S, c, C or [. |
| 134 |
// TODO: Warn that 'm' is an extension. |
| 135 |
scanf("%ms", sp); // No warning. |
| 136 |
scanf("%mS", lsp); // No warning. |
| 137 |
scanf("%mc", sp); // No warning. |
| 138 |
scanf("%mC", lsp); // No warning. |
| 139 |
scanf("%m[abc]", sp); // No warning. |
| 140 |
scanf("%md", sp); // expected-warning{{length modifier 'm' results in undefined behavior or no effect with 'd' conversion specifier}} |
| 141 |
|
| 142 |
// Test argument type check for the 'm' length modifier. |
| 143 |
scanf("%ms", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}} |
| 144 |
scanf("%mS", fp); // expected-warning-re{{format specifies type 'wchar_t **' (aka '{{[^']+}}') but the argument has type 'float *'}} |
| 145 |
scanf("%mc", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}} |
| 146 |
scanf("%mC", fp); // expected-warning-re{{format specifies type 'wchar_t **' (aka '{{[^']+}}') but the argument has type 'float *'}} |
| 147 |
scanf("%m[abc]", fp); // expected-warning{{format specifies type 'char **' but the argument has type 'float *'}} |
| 148 |
} |
| 149 |
|
| 150 |
void test_quad(int *x, long long *llx) { |
| 151 |
scanf("%qd", x); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}} |
| 152 |
scanf("%qd", llx); // no-warning |
| 153 |
} |
| 154 |
|
| 155 |
void test_writeback(int *x) { |
| 156 |
scanf("%n", (void*)0); // expected-warning{{format specifies type 'int *' but the argument has type 'void *'}} |
| 157 |
scanf("%n %c", x, x); // expected-warning{{format specifies type 'char *' but the argument has type 'int *'}} |
| 158 |
|
| 159 |
scanf("%hhn", (signed char*)0); // no-warning |
| 160 |
scanf("%hhn", (char*)0); // no-warning |
| 161 |
scanf("%hhn", (unsigned char*)0); // no-warning |
| 162 |
scanf("%hhn", (int*)0); // expected-warning{{format specifies type 'signed char *' but the argument has type 'int *'}} |
| 163 |
|
| 164 |
scanf("%hn", (short*)0); // no-warning |
| 165 |
scanf("%hn", (unsigned short*)0); // no-warning |
| 166 |
scanf("%hn", (int*)0); // expected-warning{{format specifies type 'short *' but the argument has type 'int *'}} |
| 167 |
|
| 168 |
scanf("%n", (int*)0); // no-warning |
| 169 |
scanf("%n", (unsigned int*)0); // no-warning |
| 170 |
scanf("%n", (char*)0); // expected-warning{{format specifies type 'int *' but the argument has type 'char *'}} |
| 171 |
|
| 172 |
scanf("%ln", (long*)0); // no-warning |
| 173 |
scanf("%ln", (unsigned long*)0); // no-warning |
| 174 |
scanf("%ln", (int*)0); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}} |
| 175 |
|
| 176 |
scanf("%lln", (long long*)0); // no-warning |
| 177 |
scanf("%lln", (unsigned long long*)0); // no-warning |
| 178 |
scanf("%lln", (int*)0); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}} |
| 179 |
|
| 180 |
scanf("%qn", (long long*)0); // no-warning |
| 181 |
scanf("%qn", (unsigned long long*)0); // no-warning |
| 182 |
scanf("%qn", (int*)0); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}} |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
void test_qualifiers(const int *cip, volatile int* vip, |
| 187 |
const char *ccp, volatile char* vcp, |
| 188 |
const volatile int *cvip) { |
| 189 |
scanf("%d", cip); // expected-warning{{format specifies type 'int *' but the argument has type 'const int *'}} |
| 190 |
scanf("%n", cip); // expected-warning{{format specifies type 'int *' but the argument has type 'const int *'}} |
| 191 |
scanf("%s", ccp); // expected-warning{{format specifies type 'char *' but the argument has type 'const char *'}} |
| 192 |
scanf("%d", cvip); // expected-warning{{format specifies type 'int *' but the argument has type 'const volatile int *'}} |
| 193 |
|
| 194 |
scanf("%d", vip); // No warning. |
| 195 |
scanf("%n", vip); // No warning. |
| 196 |
scanf("%c", vcp); // No warning. |
| 197 |
|
| 198 |
typedef int* ip_t; |
| 199 |
typedef const int* cip_t; |
| 200 |
scanf("%d", (ip_t)0); // No warning. |
| 201 |
scanf("%d", (cip_t)0); // expected-warning{{format specifies type 'int *' but the argument has type 'cip_t' (aka 'const int *')}} |
| 202 |
} |
| 203 |
|
| 204 |
void test_size_types(void) { |
| 205 |
size_t s = 0; |
| 206 |
scanf("%zu", &s); // No warning. |
| 207 |
scanf("%zb", &s); |
| 208 |
|
| 209 |
double d1 = 0.; |
| 210 |
scanf("%zu", &d1); // expected-warning-re{{format specifies type 'size_t *' (aka '{{.+}}') but the argument has type 'double *'}} |
| 211 |
|
| 212 |
ssize_t ss = 0; |
| 213 |
scanf("%zd", &s); // No warning. |
| 214 |
|
| 215 |
double d2 = 0.; |
| 216 |
scanf("%zd", &d2); // expected-warning-re{{format specifies type 'signed size_t *' (aka '{{.+}}') but the argument has type 'double *'}} |
| 217 |
|
| 218 |
ssize_t sn = 0; |
| 219 |
scanf("%zn", &sn); // No warning. |
| 220 |
|
| 221 |
double d3 = 0.; |
| 222 |
scanf("%zn", &d3); // expected-warning-re{{format specifies type 'signed size_t *' (aka '{{.+}}') but the argument has type 'double *'}} |
| 223 |
} |
| 224 |
|
| 225 |
void test_ptrdiff_t_types(void) { |
| 226 |
__UNSIGNED_PTRDIFF_TYPE__ p1 = 0; |
| 227 |
scanf("%tu", &p1); // No warning. |
| 228 |
scanf("%tb", &p1); |
| 229 |
|
| 230 |
double d1 = 0.; |
| 231 |
scanf("%tu", &d1); // expected-warning-re{{format specifies type 'unsigned ptrdiff_t *' (aka '{{.+}}') but the argument has type 'double *'}} |
| 232 |
|
| 233 |
ptrdiff_t p2 = 0; |
| 234 |
scanf("%td", &p2); // No warning. |
| 235 |
|
| 236 |
double d2 = 0.; |
| 237 |
scanf("%td", &d2); // expected-warning-re{{format specifies type 'ptrdiff_t *' (aka '{{.+}}') but the argument has type 'double *'}} |
| 238 |
|
| 239 |
ptrdiff_t p3 = 0; |
| 240 |
scanf("%tn", &p3); // No warning. |
| 241 |
|
| 242 |
double d3 = 0.; |
| 243 |
scanf("%tn", &d3); // expected-warning-re{{format specifies type 'ptrdiff_t *' (aka '{{.+}}') but the argument has type 'double *'}} |
| 244 |
} |
| 245 |
|
| 246 |
void check_conditional_literal(char *s, int *i) { |
| 247 |
scanf(0 ? "%s" : "%d", i); // no warning |
| 248 |
scanf(1 ? "%s" : "%d", i); // expected-warning{{format specifies type 'char *'}} |
| 249 |
scanf(0 ? "%d %d" : "%d", i); // no warning |
| 250 |
scanf(1 ? "%d %d" : "%d", i); // expected-warning{{more '%' conversions than data arguments}} |
| 251 |
scanf(0 ? "%d %d" : "%d", i, s); // expected-warning{{data argument not used}} |
| 252 |
scanf(1 ? "%d %s" : "%d", i, s); // no warning |
| 253 |
scanf(i ? "%d %s" : "%d", i, s); // no warning |
| 254 |
scanf(i ? "%d" : "%d", i, s); // expected-warning{{data argument not used}} |
| 255 |
scanf(i ? "%s" : "%d", s); // expected-warning{{format specifies type 'int *'}} |
| 256 |
} |
| 257 |
|
| 258 |
void test_promotion(void) { |
| 259 |
// No promotions for *scanf pointers clarified in N2562 |
| 260 |
// https://github.com/llvm/llvm-project/issues/57102 |
| 261 |
// N2562: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2562.pdf |
| 262 |
int i; |
| 263 |
signed char sc; |
| 264 |
unsigned char uc; |
| 265 |
short ss; |
| 266 |
unsigned short us; |
| 267 |
|
| 268 |
// pointers could not be "promoted" |
| 269 |
scanf("%hhd", &i); // expected-warning{{format specifies type 'char *' but the argument has type 'int *'}} |
| 270 |
scanf("%hd", &i); // expected-warning{{format specifies type 'short *' but the argument has type 'int *'}} |
| 271 |
scanf("%d", &i); // no-warning |
| 272 |
// char & uchar |
| 273 |
scanf("%hhd", &sc); // no-warning |
| 274 |
scanf("%hhd", &uc); // no-warning |
| 275 |
scanf("%hd", &sc); // expected-warning{{format specifies type 'short *' but the argument has type 'signed char *'}} |
| 276 |
scanf("%hd", &uc); // expected-warning{{format specifies type 'short *' but the argument has type 'unsigned char *'}} |
| 277 |
scanf("%d", &sc); // expected-warning{{format specifies type 'int *' but the argument has type 'signed char *'}} |
| 278 |
scanf("%d", &uc); // expected-warning{{format specifies type 'int *' but the argument has type 'unsigned char *'}} |
| 279 |
// short & ushort |
| 280 |
scanf("%hhd", &ss); // expected-warning{{format specifies type 'char *' but the argument has type 'short *'}} |
| 281 |
scanf("%hhd", &us); // expected-warning{{format specifies type 'char *' but the argument has type 'unsigned short *'}} |
| 282 |
scanf("%hd", &ss); // no-warning |
| 283 |
scanf("%hd", &us); // no-warning |
| 284 |
scanf("%d", &ss); // expected-warning{{format specifies type 'int *' but the argument has type 'short *'}} |
| 285 |
scanf("%d", &us); // expected-warning{{format specifies type 'int *' but the argument has type 'unsigned short *'}} |
| 286 |
|
| 287 |
// long types |
| 288 |
scanf("%ld", &i); // expected-warning{{format specifies type 'long *' but the argument has type 'int *'}} |
| 289 |
scanf("%lld", &i); // expected-warning{{format specifies type 'long long *' but the argument has type 'int *'}} |
| 290 |
scanf("%ld", &sc); // expected-warning{{format specifies type 'long *' but the argument has type 'signed char *'}} |
| 291 |
scanf("%lld", &sc); // expected-warning{{format specifies type 'long long *' but the argument has type 'signed char *'}} |
| 292 |
scanf("%ld", &uc); // expected-warning{{format specifies type 'long *' but the argument has type 'unsigned char *'}} |
| 293 |
scanf("%lld", &uc); // expected-warning{{format specifies type 'long long *' but the argument has type 'unsigned char *'}} |
| 294 |
scanf("%llx", &i); // expected-warning{{format specifies type 'unsigned long long *' but the argument has type 'int *'}} |
| 295 |
|
| 296 |
// ill-formed floats |
| 297 |
scanf("%hf", // expected-warning{{length modifier 'h' results in undefined behavior or no effect with 'f' conversion specifier}} |
| 298 |
&sc); |
| 299 |
|
| 300 |
// pointers in scanf |
| 301 |
scanf("%s", i); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}} |
| 302 |
|
| 303 |
// FIXME: does this match what the C committee allows or should it be pedantically warned on? |
| 304 |
char c; |
| 305 |
void *vp; |
| 306 |
scanf("%hhd", &c); // Pedantic warning? |
| 307 |
scanf("%hhd", vp); // expected-warning{{format specifies type 'char *' but the argument has type 'void *'}} |
| 308 |
} |