Skip to content

Commit 0dc2c48

Browse files
committed
C++: provide integer format specifier suggestions in Printf library
1 parent 998d4ba commit 0dc2c48

12 files changed

Lines changed: 279 additions & 32 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: feature
3+
---
4+
* Added `FormatLiteral.getSuggestedIntegerConversionSpecifier` to `semmle.code.cpp.commons.Printf`. It provides matching specifiers for plain decimal integer conversions and promoted `int`, `long`, and `long long` argument types, including unsigned variants.

‎cpp/ql/lib/semmle/code/cpp/commons/Printf.qll‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,42 @@ class FormatLiteral extends Literal instanceof StringLiteral {
704704
*/
705705
string getConversionChar(int n) { this.parseConvSpec(n, _, _, _, _, _, _, result) }
706706

707+
/**
708+
* Gets a suggested replacement for the `n`th conversion specifier for an
709+
* argument of type `type`, after default argument promotions.
710+
*
711+
* This only handles plain decimal integer conversions (`d`, `i`, and `u`,
712+
* optionally with `l` or `ll`) and the signed and unsigned variants of `int`,
713+
* `long`, and `long long`. There is no result for other types or conversions,
714+
* or for conversions with flags, width, precision, or positional parameters.
715+
* The result includes `%` and preserves `i` for signed integer arguments.
716+
*
717+
* This does not check whether the existing specifier matches `type`.
718+
*/
719+
string getSuggestedIntegerConversionSpecifier(int n, Type type) {
720+
exists(IntegralType t, string length, string conversion, string originalConversion |
721+
this.parseConvSpec(n, _, "", "", "", "", ["", "l", "ll"], originalConversion) and
722+
originalConversion = ["d", "i", "u"] and
723+
t = type.getUnspecifiedType() and
724+
(
725+
t instanceof IntType and length = ""
726+
or
727+
t instanceof LongType and length = "l"
728+
or
729+
t instanceof LongLongType and length = "ll"
730+
) and
731+
(
732+
if t.isUnsigned()
733+
then conversion = "u"
734+
else
735+
if originalConversion = "i"
736+
then conversion = "i"
737+
else conversion = "d"
738+
) and
739+
result = "%" + length + conversion
740+
)
741+
}
742+
707743
/**
708744
* Gets the size of pointers in the target this formatting function is
709745
* compiled for.

‎cpp/ql/src/Likely Bugs/Format/WrongTypeFormatArguments.ql‎

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -152,40 +152,16 @@ predicate trivialConversion(ExpectedType expected, Type actual) {
152152
*/
153153
int sizeof_IntType() { exists(IntType it | result = it.getSize()) }
154154

155-
/**
156-
* Gets a suggested specifier for a simple decimal integer conversion.
157-
* Use the promoted argument type, and do not suggest changing width or precision arguments.
158-
* Decorated, positional, and non-decimal conversions are deliberately left unchanged.
159-
*/
160-
private string suggestedIntegerSpecifier(FormattingFunctionCall ffc, int n, Expr arg, Type actual) {
161-
exists(FormatLiteral format, IntegralType t, string length, string conversion |
162-
format = ffc.getFormat() and
163-
ffc.getConversionArgument(n) = arg and
164-
format.getConvSpec(n).regexpMatch("%(l|ll)?[diu]") and
165-
t = actual.getUnspecifiedType() and
166-
(
167-
t instanceof IntType and length = ""
168-
or
169-
t instanceof LongType and length = "l"
170-
or
171-
t instanceof LongLongType and length = "ll"
172-
) and
173-
(
174-
if t.isUnsigned()
175-
then conversion = "u"
176-
else
177-
if format.getConversionChar(n) = "i"
178-
then conversion = "i"
179-
else conversion = "d"
180-
) and
181-
result = "%" + length + conversion
182-
)
183-
}
184-
185155
bindingset[ffc, n, arg, actual]
186156
private string integerFormatSuggestion(FormattingFunctionCall ffc, int n, Expr arg, Type actual) {
187-
if exists(suggestedIntegerSpecifier(ffc, n, arg, actual))
188-
then result = " Consider using '" + suggestedIntegerSpecifier(ffc, n, arg, actual) + "' instead."
157+
if
158+
ffc.getConversionArgument(n) = arg and
159+
exists(ffc.getFormat().(FormatLiteral).getSuggestedIntegerConversionSpecifier(n, actual))
160+
then
161+
result =
162+
" Consider using '" +
163+
ffc.getFormat().(FormatLiteral).getSuggestedIntegerConversionSpecifier(n, actual) +
164+
"' instead."
189165
else result = ""
190166
}
191167

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle-extractor-options: --clang --edg --signed_chars
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
| ../cases.h:11:10:11:13 | %d | 0 | %d | %d |
2+
| ../cases.h:12:10:12:13 | %d | 0 | %u | %u |
3+
| ../cases.h:13:10:13:13 | %d | 0 | %ld | %ld |
4+
| ../cases.h:14:10:14:13 | %d | 0 | %lu | %lu |
5+
| ../cases.h:15:10:15:13 | %d | 0 | %lld | %lld |
6+
| ../cases.h:16:10:16:13 | %d | 0 | %llu | %llu |
7+
| ../cases.h:17:10:17:13 | %i | 0 | %i | %i |
8+
| ../cases.h:18:10:18:13 | %i | 0 | %u | %u |
9+
| ../cases.h:19:10:19:13 | %i | 0 | %li | %li |
10+
| ../cases.h:20:10:20:13 | %i | 0 | %lu | %lu |
11+
| ../cases.h:21:10:21:13 | %i | 0 | %lli | %lli |
12+
| ../cases.h:22:10:22:13 | %i | 0 | %llu | %llu |
13+
| ../cases.h:23:10:23:13 | %u | 0 | %d | %d |
14+
| ../cases.h:24:10:24:14 | %ld | 0 | %d | %d |
15+
| ../cases.h:25:10:25:14 | %li | 0 | %u | %u |
16+
| ../cases.h:26:10:26:14 | %lu | 0 | %lld | %lld |
17+
| ../cases.h:27:10:27:15 | %lld | 0 | %ld | %ld |
18+
| ../cases.h:28:10:28:15 | %lli | 0 | %llu | %llu |
19+
| ../cases.h:29:10:29:15 | %llu | 0 | %lu | %lu |
20+
| ../cases.h:30:10:30:13 | %d | 0 | %lu | %lu |
21+
| ../cases.h:32:10:32:15 | %lld | 0 | %d | |
22+
| ../cases.h:33:10:33:15 | %lld | 0 | %d | |
23+
| ../cases.h:34:10:34:15 | %lld | 0 | %d | |
24+
| ../cases.h:35:10:35:15 | %lld | 0 | %d | |
25+
| ../cases.h:36:10:36:15 | %lld | 0 | | |
26+
| ../cases.h:38:10:38:23 | value=%d end | 0 | %llu | %llu |
27+
| ../cases.h:39:10:39:19 | %% %d %i | 0 | %ld | %ld |
28+
| ../cases.h:39:10:39:19 | %% %d %i | 1 | %llu | %llu |
29+
| ../cases.h:42:10:42:14 | %+d | 0 | | |
30+
| ../cases.h:43:10:43:14 | % d | 0 | | |
31+
| ../cases.h:44:10:44:14 | %-d | 0 | | |
32+
| ../cases.h:45:10:45:14 | %#d | 0 | | |
33+
| ../cases.h:46:10:46:14 | %'d | 0 | | |
34+
| ../cases.h:47:10:47:15 | %08d | 0 | | |
35+
| ../cases.h:48:10:48:15 | %12d | 0 | | |
36+
| ../cases.h:49:10:49:14 | %.d | 0 | | |
37+
| ../cases.h:50:10:50:15 | %.3d | 0 | | |
38+
| ../cases.h:51:10:51:14 | %*d | 0 | | |
39+
| ../cases.h:52:10:52:15 | %.*d | 0 | | |
40+
| ../cases.h:53:10:53:15 | %1$d | 0 | | |
41+
| ../cases.h:54:10:54:20 | %2$d %1$i | 0 | | |
42+
| ../cases.h:54:10:54:20 | %2$d %1$i | 1 | | |
43+
| ../cases.h:56:10:56:15 | %hhd | 0 | | |
44+
| ../cases.h:57:10:57:14 | %hd | 0 | | |
45+
| ../cases.h:58:10:58:14 | %jd | 0 | | |
46+
| ../cases.h:59:10:59:14 | %zd | 0 | | |
47+
| ../cases.h:60:10:60:14 | %td | 0 | | |
48+
| ../cases.h:61:10:61:14 | %qd | 0 | | |
49+
| ../cases.h:62:10:62:16 | %I64d | 0 | | |
50+
| ../cases.h:63:10:63:13 | %x | 0 | | |
51+
| ../cases.h:64:10:64:13 | %X | 0 | | |
52+
| ../cases.h:65:10:65:13 | %o | 0 | | |
53+
| ../cases.h:66:10:66:13 | %c | 0 | | |
54+
| ../cases.h:67:10:67:13 | %s | 0 | | |
55+
| ../cases.h:68:10:68:13 | %p | 0 | | |
56+
| ../cases.h:69:10:69:13 | %f | 0 | | |
57+
| ../cases.h:70:10:70:13 | %n | 0 | | |
58+
| ../cases.h:71:10:71:13 | %d | 0 | | |
59+
| ../cases.h:72:10:72:13 | %d | 0 | | |
60+
| ../cases.h:73:10:73:13 | %d | 0 | | |
61+
| ../cases.h:74:10:74:14 | %kd | 0 | | |
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import semmle.code.cpp.commons.Printf
2+
3+
from FormatLiteral format, int n
4+
where exists(format.getConvSpecOffset(n))
5+
select format, n,
6+
concat(Expr arg |
7+
arg = format.getUse().getConversionArgument(n)
8+
|
9+
format.getSuggestedIntegerConversionSpecifier(n, arg.getFullyConverted().getType()), ", "
10+
),
11+
concat(Expr arg |
12+
arg = format.getUse().getConversionArgument(n)
13+
|
14+
format.getSuggestedIntegerConversionSpecifier(n, arg.getUnconverted().getType()), ", "
15+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "../cases.h"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle-extractor-options: --microsoft --edg --target --edg win64
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
| ../cases.h:11:10:11:13 | %d | 0 | %d | %d |
2+
| ../cases.h:12:10:12:13 | %d | 0 | %u | %u |
3+
| ../cases.h:13:10:13:13 | %d | 0 | %ld | %ld |
4+
| ../cases.h:14:10:14:13 | %d | 0 | %lu | %lu |
5+
| ../cases.h:15:10:15:13 | %d | 0 | %lld | %lld |
6+
| ../cases.h:16:10:16:13 | %d | 0 | %llu | %llu |
7+
| ../cases.h:17:10:17:13 | %i | 0 | %i | %i |
8+
| ../cases.h:18:10:18:13 | %i | 0 | %u | %u |
9+
| ../cases.h:19:10:19:13 | %i | 0 | %li | %li |
10+
| ../cases.h:20:10:20:13 | %i | 0 | %lu | %lu |
11+
| ../cases.h:21:10:21:13 | %i | 0 | %lli | %lli |
12+
| ../cases.h:22:10:22:13 | %i | 0 | %llu | %llu |
13+
| ../cases.h:23:10:23:13 | %u | 0 | %d | %d |
14+
| ../cases.h:24:10:24:14 | %ld | 0 | %d | %d |
15+
| ../cases.h:25:10:25:14 | %li | 0 | %u | %u |
16+
| ../cases.h:26:10:26:14 | %lu | 0 | %lld | %lld |
17+
| ../cases.h:27:10:27:15 | %lld | 0 | %ld | %ld |
18+
| ../cases.h:28:10:28:15 | %lli | 0 | %llu | %llu |
19+
| ../cases.h:29:10:29:15 | %llu | 0 | %lu | %lu |
20+
| ../cases.h:30:10:30:13 | %d | 0 | %lu | %lu |
21+
| ../cases.h:32:10:32:15 | %lld | 0 | %d | |
22+
| ../cases.h:33:10:33:15 | %lld | 0 | %d | |
23+
| ../cases.h:34:10:34:15 | %lld | 0 | %d | |
24+
| ../cases.h:35:10:35:15 | %lld | 0 | %d | |
25+
| ../cases.h:36:10:36:15 | %lld | 0 | | |
26+
| ../cases.h:38:10:38:23 | value=%d end | 0 | %llu | %llu |
27+
| ../cases.h:39:10:39:19 | %% %d %i | 0 | %ld | %ld |
28+
| ../cases.h:39:10:39:19 | %% %d %i | 1 | %llu | %llu |
29+
| ../cases.h:42:10:42:14 | %+d | 0 | | |
30+
| ../cases.h:43:10:43:14 | % d | 0 | | |
31+
| ../cases.h:44:10:44:14 | %-d | 0 | | |
32+
| ../cases.h:45:10:45:14 | %#d | 0 | | |
33+
| ../cases.h:46:10:46:14 | %'d | 0 | | |
34+
| ../cases.h:47:10:47:15 | %08d | 0 | | |
35+
| ../cases.h:48:10:48:15 | %12d | 0 | | |
36+
| ../cases.h:49:10:49:14 | %.d | 0 | | |
37+
| ../cases.h:50:10:50:15 | %.3d | 0 | | |
38+
| ../cases.h:51:10:51:14 | %*d | 0 | | |
39+
| ../cases.h:52:10:52:15 | %.*d | 0 | | |
40+
| ../cases.h:53:10:53:15 | %1$d | 0 | | |
41+
| ../cases.h:54:10:54:20 | %2$d %1$i | 0 | | |
42+
| ../cases.h:54:10:54:20 | %2$d %1$i | 1 | | |
43+
| ../cases.h:56:10:56:15 | %hhd | 0 | | |
44+
| ../cases.h:57:10:57:14 | %hd | 0 | | |
45+
| ../cases.h:58:10:58:14 | %jd | 0 | | |
46+
| ../cases.h:59:10:59:14 | %zd | 0 | | |
47+
| ../cases.h:60:10:60:14 | %td | 0 | | |
48+
| ../cases.h:61:10:61:14 | %qd | 0 | | |
49+
| ../cases.h:62:10:62:16 | %I64d | 0 | | |
50+
| ../cases.h:63:10:63:13 | %x | 0 | | |
51+
| ../cases.h:64:10:64:13 | %X | 0 | | |
52+
| ../cases.h:65:10:65:13 | %o | 0 | | |
53+
| ../cases.h:66:10:66:13 | %c | 0 | | |
54+
| ../cases.h:67:10:67:13 | %s | 0 | | |
55+
| ../cases.h:68:10:68:13 | %p | 0 | | |
56+
| ../cases.h:69:10:69:13 | %f | 0 | | |
57+
| ../cases.h:70:10:70:13 | %n | 0 | | |
58+
| ../cases.h:71:10:71:13 | %d | 0 | | |
59+
| ../cases.h:72:10:72:13 | %d | 0 | | |
60+
| ../cases.h:73:10:73:13 | %d | 0 | | |
61+
| ../cases.h:74:10:74:14 | %kd | 0 | | |
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import semmle.code.cpp.commons.Printf
2+
3+
from FormatLiteral format, int n
4+
where exists(format.getConvSpecOffset(n))
5+
select format, n,
6+
concat(Expr arg |
7+
arg = format.getUse().getConversionArgument(n)
8+
|
9+
format.getSuggestedIntegerConversionSpecifier(n, arg.getFullyConverted().getType()), ", "
10+
),
11+
concat(Expr arg |
12+
arg = format.getUse().getConversionArgument(n)
13+
|
14+
format.getSuggestedIntegerConversionSpecifier(n, arg.getUnconverted().getType()), ", "
15+
)

0 commit comments

Comments
 (0)