Skip to content

Commit 7608840

Browse files
committed
CPP: Add test cases using various combinations of width and precision specifiers, positional arguments, and flags.
1 parent f2a9876 commit 7608840

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_signed_chars/WrongTypeFormatArguments.expected

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,37 @@
1616
| printf1.h:114:18:114:18 | d | This argument should be of type 'long double' but is of type 'double' |
1717
| printf1.h:147:19:147:19 | i | This argument should be of type 'long long' but is of type 'int' |
1818
| printf1.h:148:19:148:20 | ui | This argument should be of type 'unsigned long long' but is of type 'unsigned int' |
19+
| printf1.h:159:18:159:18 | i | This argument should be of type 'char *' but is of type 'int' |
20+
| printf1.h:160:18:160:18 | i | This argument should be of type 'char *' but is of type 'int' |
21+
| printf1.h:167:17:167:17 | i | This argument should be of type 'char *' but is of type 'int' |
22+
| printf1.h:168:18:168:18 | i | This argument should be of type 'char *' but is of type 'int' |
23+
| printf1.h:169:19:169:19 | i | This argument should be of type 'char *' but is of type 'int' |
24+
| printf1.h:174:17:174:17 | s | This argument should be of type 'int' but is of type 'char *' |
25+
| printf1.h:175:18:175:18 | s | This argument should be of type 'int' but is of type 'char *' |
26+
| printf1.h:176:19:176:19 | s | This argument should be of type 'int' but is of type 'char *' |
27+
| printf1.h:180:17:180:17 | s | This argument should be of type 'int' but is of type 'char *' |
28+
| printf1.h:181:20:181:20 | i | This argument should be of type 'char *' but is of type 'int' |
29+
| printf1.h:183:18:183:18 | s | This argument should be of type 'int' but is of type 'char *' |
30+
| printf1.h:184:21:184:21 | i | This argument should be of type 'char *' but is of type 'int' |
31+
| printf1.h:186:19:186:19 | s | This argument should be of type 'int' but is of type 'char *' |
32+
| printf1.h:187:22:187:22 | i | This argument should be of type 'char *' but is of type 'int' |
33+
| printf1.h:189:19:189:19 | s | This argument should be of type 'int' but is of type 'char *' |
34+
| printf1.h:190:22:190:22 | i | This argument should be of type 'char *' but is of type 'int' |
35+
| printf1.h:192:19:192:19 | s | This argument should be of type 'int' but is of type 'char *' |
36+
| printf1.h:193:22:193:22 | s | This argument should be of type 'int' but is of type 'char *' |
37+
| printf1.h:194:25:194:25 | i | This argument should be of type 'char *' but is of type 'int' |
38+
| printf1.h:213:28:213:28 | s | This argument should be of type 'int' but is of type 'char *' |
39+
| printf1.h:214:28:214:28 | s | This argument should be of type 'int' but is of type 'char *' |
40+
| printf1.h:215:28:215:28 | s | This argument should be of type 'int' but is of type 'char *' |
41+
| printf1.h:216:28:216:28 | s | This argument should be of type 'int' but is of type 'char *' |
42+
| printf1.h:221:18:221:18 | s | This argument should be of type 'int' but is of type 'char *' |
43+
| printf1.h:222:20:222:20 | s | This argument should be of type 'int' but is of type 'char *' |
44+
| printf1.h:233:22:233:22 | s | This argument should be of type 'int' but is of type 'char *' |
45+
| printf1.h:233:25:233:25 | i | This argument should be of type 'char *' but is of type 'int' |
46+
| printf1.h:234:22:234:22 | s | This argument should be of type 'int' but is of type 'char *' |
47+
| printf1.h:234:25:234:25 | i | This argument should be of type 'char *' but is of type 'int' |
48+
| printf1.h:235:22:235:22 | s | This argument should be of type 'int' but is of type 'char *' |
49+
| printf1.h:235:25:235:25 | i | This argument should be of type 'char *' but is of type 'int' |
1950
| real_world.h:61:21:61:22 | & ... | This argument should be of type 'int *' but is of type 'short *' |
2051
| real_world.h:62:22:62:23 | & ... | This argument should be of type 'short *' but is of type 'int *' |
2152
| real_world.h:63:22:63:24 | & ... | This argument should be of type 'short *' but is of type 'unsigned int *' |

cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_signed_chars/printf1.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,86 @@ void fun4()
151151
printf("%qi\n", ll); // GOOD
152152
printf("%qu\n", ull); // GOOD
153153
}
154+
155+
void complexFormatSymbols(int i, const char *s)
156+
{
157+
// positional arguments
158+
printf("%1$i", i, s); // GOOD
159+
printf("%2$s", i, s); // GOOD [FALSE POSITIVE]
160+
printf("%1$s", i, s); // BAD
161+
printf("%2$i", i, s); // BAD [NOT DETECTED]
162+
163+
// width / precision
164+
printf("%4i", i); // GOOD
165+
printf("%.4i", i); // GOOD
166+
printf("%4.4i", i); // GOOD
167+
printf("%4s", i); // BAD
168+
printf("%.4s", i); // BAD
169+
printf("%4.4s", i); // BAD
170+
171+
printf("%4s", s); // GOOD
172+
printf("%.4s", s); // GOOD
173+
printf("%4.4s", s); // GOOD
174+
printf("%4i", s); // BAD
175+
printf("%.4i", s); // BAD
176+
printf("%4.4i", s); // BAD
177+
178+
// variable width / precision
179+
printf("%*s", i, s); // GOOD
180+
printf("%*s", s, s); // BAD
181+
printf("%*s", i, i); // BAD
182+
printf("%.*s", i, s); // GOOD
183+
printf("%.*s", s, s); // BAD
184+
printf("%.*s", i, i); // BAD
185+
printf("%*.4s", i, s); // GOOD
186+
printf("%*.4s", s, s); // BAD
187+
printf("%*.4s", i, i); // BAD
188+
printf("%4.*s", i, s); // GOOD
189+
printf("%4.*s", s, s); // BAD
190+
printf("%4.*s", i, i); // BAD
191+
printf("%*.*s", i, i, s); // GOOD
192+
printf("%*.*s", s, i, s); // BAD
193+
printf("%*.*s", i, s, s); // BAD
194+
printf("%*.*s", i, i, i); // BAD
195+
196+
// positional arguments mixed with variable width / precision
197+
printf("%2$*1$s", i, s); // GOOD
198+
printf("%2$*2$s", i, s); // BAD [NOT DETECTED]
199+
printf("%1$*1$s", i, s); // BAD [NOT DETECTED]
200+
201+
printf("%2$*1$.4s", i, s); // GOOD
202+
printf("%2$*2$.4s", i, s); // BAD [NOT DETECTED]
203+
printf("%1$*1$.4s", i, s); // BAD [NOT DETECTED]
204+
205+
printf("%2$.*1$s", i, s); // GOOD
206+
printf("%2$.*2$s", i, s); // BAD [NOT DETECTED]
207+
printf("%1$.*1$s", i, s); // BAD [NOT DETECTED]
208+
209+
printf("%2$4.*1$s", i, s); // GOOD
210+
printf("%2$4.*2$s", i, s); // BAD [NOT DETECTED]
211+
printf("%1$4.*1$s", i, s); // BAD [NOT DETECTED]
212+
213+
printf("%2$*1$.*1$s", i, s); // GOOD [FALSE POSITIVE]
214+
printf("%2$*2$.*1$s", i, s); // BAD
215+
printf("%2$*1$.*2$s", i, s); // BAD
216+
printf("%1$*1$.*1$s", i, s); // BAD
217+
218+
// left justify flag
219+
printf("%-4s", s); // GOOD
220+
printf("%1$-4s", s); // GOOD
221+
printf("%-4i", s); // BAD
222+
printf("%1$-4i", s); // BAD
223+
224+
printf("%1$-4s", s, i); // GOOD
225+
printf("%2$-4s", s, i); // BAD [NOT DETECTED]
226+
227+
printf("%1$-.4s", s, i); // GOOD
228+
printf("%2$-.4s", s, i); // BAD [NOT DETECTED]
229+
230+
printf("%1$-4.4s", s, i); // GOOD
231+
printf("%2$-4.4s", s, i); // BAD [NOT DETECTED]
232+
233+
printf("%1$-*2$s", s, i); // GOOD [FALSE POSITIVE x2]
234+
printf("%2$-*2$s", s, i); // BAD [ADDITIONAL RESULT IS A FALSE POSITIVE]
235+
printf("%1$-*1$s", s, i); // BAD [ADDITIONAL RESULT IS A FALSE POSITIVE]
236+
}

0 commit comments

Comments
 (0)