Skip to content

Commit 1376385

Browse files
committed
C++: Add formatLiteral test.
1 parent 3a99b98 commit 1376385

File tree

5 files changed

+202
-0
lines changed

5 files changed

+202
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
| test.c:14:9:14:10 | | 1 |
2+
| test.c:15:9:15:14 | | 2 |
3+
| test.c:16:9:16:12 | \t | 2 |
4+
| test.c:17:9:17:12 | %% | 2 |
5+
| test.c:20:9:20:12 | %c | 2 |
6+
| test.c:21:9:21:16 | %c%c%c | 4 |
7+
| test.c:24:9:24:23 | Hello, world! | 14 |
8+
| test.c:25:9:25:12 | %s | 14 |
9+
| test.c:26:9:26:14 | %.4s | 5 |
10+
| test.c:27:9:27:16 | %s, %s | 14 |
11+
| test.c:30:9:30:12 | %i | 12 |
12+
| test.c:31:9:31:14 | %lli | 12 |
13+
| test.c:32:9:32:12 | %i | 12 |
14+
| test.c:33:9:33:14 | %lli | 21 |
15+
| test.c:34:9:34:12 | %d | 12 |
16+
| test.c:35:9:35:12 | %u | 11 |
17+
| test.c:36:9:36:12 | %x | 9 |
18+
| test.c:37:9:37:12 | %X | 9 |
19+
| test.c:38:9:38:13 | %#x | 11 |
20+
| test.c:39:9:39:12 | %o | 12 |
21+
| test.c:40:9:40:13 | %#o | 13 |
22+
| test.c:43:9:43:12 | %f | 318 |
23+
| test.c:44:9:44:14 | %.2f | |
24+
| test.c:45:9:45:12 | %e | 15 |
25+
| test.c:59:10:59:14 | %Ii | 12 |
26+
| test.c:66:10:66:14 | %zu | 21 |
27+
| test.c:67:10:67:14 | %Zu | 21 |
28+
| test.c:74:10:74:14 | %lc | 2 |
29+
| test.c:78:9:78:20 | %2$i, %1$i | 5 |
30+
| test.c:79:9:79:20 | %2$i, %1$i | 25 |
31+
| test.c:81:9:81:24 | %2$02i %1$4.2f | |
32+
| test.c:85:10:85:18 | %2$*1$d | |
33+
| test.c:86:10:86:19 | %2$0*1$d | |
34+
| test.c:92:10:92:19 | %2$.*1$f | |
35+
| test.c:99:10:99:12 | # | 2 |
36+
| test.c:100:10:100:13 | %% | 2 |
37+
| test.c:101:10:101:15 | %%%% | 3 |
38+
| test.c:102:10:102:15 | %%%f | 319 |
39+
| test.c:103:10:103:17 | %%%%%f | 320 |
40+
| test.c:104:10:104:18 | %4.2f%% | |
41+
| test.c:105:10:105:17 | %%%f%% | 320 |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import semmle.code.cpp.commons.Printf
2+
3+
from FormatLiteral fl
4+
select fl, concat(fl.getMaxConvertedLength().toString(), ", ")
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
| test.c:20:9:20:12 | %c | 0 | | c | | file://:0:0:0:0 | char |
2+
| test.c:21:9:21:16 | %c%c%c | 0 | | c | | file://:0:0:0:0 | char |
3+
| test.c:21:9:21:16 | %c%c%c | 1 | | c | | file://:0:0:0:0 | char |
4+
| test.c:21:9:21:16 | %c%c%c | 2 | | c | | file://:0:0:0:0 | char |
5+
| test.c:25:9:25:12 | %s | 0 | | s | | file://:0:0:0:0 | char * |
6+
| test.c:26:9:26:14 | %.4s | 0 | | s | | file://:0:0:0:0 | char * |
7+
| test.c:27:9:27:16 | %s, %s | 0 | | s | | file://:0:0:0:0 | char * |
8+
| test.c:27:9:27:16 | %s, %s | 1 | | s | | file://:0:0:0:0 | char * |
9+
| test.c:30:9:30:12 | %i | 0 | | i | | file://:0:0:0:0 | int |
10+
| test.c:31:9:31:14 | %lli | 0 | | i | ll | file://:0:0:0:0 | long long |
11+
| test.c:32:9:32:12 | %i | 0 | | i | | file://:0:0:0:0 | int |
12+
| test.c:33:9:33:14 | %lli | 0 | | i | ll | file://:0:0:0:0 | long long |
13+
| test.c:34:9:34:12 | %d | 0 | | d | | file://:0:0:0:0 | int |
14+
| test.c:35:9:35:12 | %u | 0 | | u | | file://:0:0:0:0 | unsigned int |
15+
| test.c:36:9:36:12 | %x | 0 | | x | | file://:0:0:0:0 | unsigned int |
16+
| test.c:37:9:37:12 | %X | 0 | | X | | file://:0:0:0:0 | unsigned int |
17+
| test.c:38:9:38:13 | %#x | 0 | | x | | file://:0:0:0:0 | unsigned int |
18+
| test.c:39:9:39:12 | %o | 0 | | o | | file://:0:0:0:0 | unsigned int |
19+
| test.c:40:9:40:13 | %#o | 0 | | o | | file://:0:0:0:0 | unsigned int |
20+
| test.c:43:9:43:12 | %f | 0 | | f | | file://:0:0:0:0 | double |
21+
| test.c:44:9:44:14 | %.2f | 0 | | f | | file://:0:0:0:0 | double |
22+
| test.c:45:9:45:12 | %e | 0 | | e | | file://:0:0:0:0 | double |
23+
| test.c:59:10:59:14 | %Ii | 0 | | i | | file://:0:0:0:0 | int |
24+
| test.c:66:10:66:14 | %zu | 0 | | u | z | test.c:50:27:50:32 | size_t |
25+
| test.c:67:10:67:14 | %Zu | 0 | | u | Z | test.c:50:27:50:32 | size_t |
26+
| test.c:74:10:74:14 | %lc | 0 | | c | l | file://:0:0:0:0 | wchar_t |
27+
| test.c:78:9:78:20 | %2$i, %1$i | 0 | 2$ | i | | file://:0:0:0:0 | int |
28+
| test.c:78:9:78:20 | %2$i, %1$i | 1 | 1$ | i | | file://:0:0:0:0 | int |
29+
| test.c:79:9:79:20 | %2$i, %1$i | 0 | 2$ | i | | file://:0:0:0:0 | int |
30+
| test.c:79:9:79:20 | %2$i, %1$i | 1 | 1$ | i | | file://:0:0:0:0 | int |
31+
| test.c:81:9:81:24 | %2$02i %1$4.2f | 0 | 2$ | i | | file://:0:0:0:0 | int |
32+
| test.c:81:9:81:24 | %2$02i %1$4.2f | 1 | 1$ | f | | file://:0:0:0:0 | double |
33+
| test.c:85:10:85:18 | %2$*1$d | 0 | 2$ | d | | file://:0:0:0:0 | int |
34+
| test.c:86:10:86:19 | %2$0*1$d | 0 | 2$ | d | | file://:0:0:0:0 | int |
35+
| test.c:92:10:92:19 | %2$.*1$f | 0 | 2$ | f | | file://:0:0:0:0 | double |
36+
| test.c:102:10:102:15 | %%%f | 0 | | f | | file://:0:0:0:0 | double |
37+
| test.c:103:10:103:17 | %%%%%f | 0 | | f | | file://:0:0:0:0 | double |
38+
| test.c:104:10:104:18 | %4.2f%% | 0 | | f | | file://:0:0:0:0 | double |
39+
| test.c:105:10:105:17 | %%%f%% | 0 | | f | | file://:0:0:0:0 | double |
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import semmle.code.cpp.commons.Printf
2+
3+
from FormatLiteral fl, int i
4+
select
5+
fl,
6+
i,
7+
concat(fl.getParameterField(i).toString(), ", "),
8+
fl.getConversionChar(i),
9+
fl.getLength(i),
10+
concat(fl.getConversionType(i).getLocation().toString(), ", "),
11+
concat(fl.getConversionType(i).toString(), ", ")
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/** standard printf functions */
2+
3+
int printf(const char *format, ...);
4+
5+
/** test program */
6+
7+
int main(int argc, char *argv[])
8+
{
9+
long long int lli;
10+
double d;
11+
int i;
12+
13+
// constant expressions
14+
printf("");
15+
printf("\x20");
16+
printf("\t");
17+
printf("%%");
18+
19+
// characters
20+
printf("%c", 'a');
21+
printf("%c%c%c", 'a', 'b', 'c');
22+
23+
// strings
24+
printf("Hello, world!");
25+
printf("%s", "Hello, world!");
26+
printf("%.4s", "Hello, world!");
27+
printf("%s, %s", "Hello", "world!");
28+
29+
// integers
30+
printf("%i", i);
31+
printf("%lli", i);
32+
printf("%i", lli);
33+
printf("%lli", lli);
34+
printf("%d", i);
35+
printf("%u", i);
36+
printf("%x", i);
37+
printf("%X", i);
38+
printf("%#x", i);
39+
printf("%o", i);
40+
printf("%#o", i);
41+
42+
// doubles
43+
printf("%f", d);
44+
printf("%.2f", d);
45+
printf("%e", d);
46+
47+
return 0;
48+
}
49+
50+
typedef long unsigned int size_t;
51+
typedef unsigned int wint_t;
52+
53+
void more_cases(int a, int b)
54+
{
55+
// integers
56+
{
57+
int i;
58+
59+
printf("%Ii", i); // glibc 2.2 'I' prefix
60+
}
61+
62+
// size_t
63+
{
64+
size_t st;
65+
66+
printf("%zu", st); // size_t
67+
printf("%Zu", st); // non-standard synonym for 'z'
68+
}
69+
70+
// wint_t
71+
{
72+
wint_t wt;
73+
74+
printf("%lc", wt); // wide character
75+
}
76+
77+
// posix indexed format arguments
78+
printf("%2$i, %1$i", 1, 2); // '2, 1'
79+
printf("%2$i, %1$i", a, b);
80+
81+
printf("%2$02i %1$4.2f", 3.3333f, 6); // 06, 3.33
82+
{
83+
int width, num;
84+
85+
printf("%2$*1$d", width, num);
86+
printf("%2$0*1$d", width, num);
87+
}
88+
{
89+
int precision;
90+
float num;
91+
92+
printf("%2$.*1$f", precision, num);
93+
}
94+
95+
// %%
96+
{
97+
float num;
98+
99+
printf("#");
100+
printf("%%");
101+
printf("%%%%");
102+
printf("%%%f", num);
103+
printf("%%%%%f", num);
104+
printf("%4.2f%%", num);
105+
printf("%%%f%%", num);
106+
}
107+
}

0 commit comments

Comments
 (0)