Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: feature
---
* 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.
148 changes: 72 additions & 76 deletions cpp/ql/lib/semmle/code/cpp/commons/Printf.qll
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,30 @@ class FormatLiteral extends Literal instanceof StringLiteral {
*/
string getConversionChar(int n) { this.parseConvSpec(n, _, _, _, _, _, _, result) }

/**
* Gets a suggested replacement for the `n`th conversion specifier for an
* argument of type `type`, after default argument promotions.
*
* This only handles plain decimal integer conversions (`d`, `i`, and `u`,
* optionally with `l` or `ll`) and the signed and unsigned variants of `int`,
* `long`, and `long long`. There is no result for other types or conversions,
* or for conversions with flags, width, precision, or positional parameters.
* The result includes `%` and preserves `i` for signed integer arguments.
*
* This does not check whether the existing specifier matches `type`.
*/
string getSuggestedIntegerConversionSpecifier(int n, Type type) {
exists(string length, string conversion, string originalConversion |
this.parseConvSpec(n, _, "", "", "", "", ["", "l", "ll"], originalConversion) and
originalConversion = ["d", "i", "u"] and
length = ["", "l", "ll"] and
(if originalConversion = "i" then conversion = ["i", "u"] else conversion = ["d", "u"]) and
this.getIntegerConversionType(length, conversion) =
type.getUnspecifiedType().(IntegralType).getCanonicalArithmeticType() and
result = "%" + length + conversion
)
}

/**
* Gets the size of pointers in the target this formatting function is
* compiled for.
Expand Down Expand Up @@ -759,75 +783,49 @@ class FormatLiteral extends Literal instanceof StringLiteral {
* Gets the family of integral types required by the nth conversion
* specifier's length flag.
*/
Type getIntegralConversion(int n) {
exists(string len |
len = this.getLength(n) and
(
len = "hh" and result instanceof IntType
or
len = "h" and result instanceof IntType
or
len = "l" and result = this.getLongType()
or
len = ["ll", "L", "q"] and
result instanceof LongLongType
or
len = "j" and result = this.getIntmax_t()
or
len = ["z", "Z"] and
(result = this.getSize_t() or result = this.getSsize_t())
or
len = "t" and result = this.getPtrdiff_t()
or
len = "I" and
(result = this.getSize_t() or result = this.getPtrdiff_t())
or
len = "I32" and
exists(MicrosoftInt32Type t | t.getUnsigned() = result.(IntegralType).getUnsigned())
or
len = "I64" and
exists(MicrosoftInt64Type t | t.getUnsigned() = result.(IntegralType).getUnsigned())
or
len = "" and result instanceof IntType
)
)
Type getIntegralConversion(int n) { result = this.getIntegralTypeForLength(this.getLength(n)) }

/** Gets the integer type family associated with a length flag. */
private Type getIntegralTypeForLength(string len) {
len = "hh" and result instanceof IntType
or
len = "h" and result instanceof IntType
or
len = "l" and result = this.getLongType()
or
len = ["ll", "L", "q"] and
result instanceof LongLongType
or
len = "j" and result = this.getIntmax_t()
or
len = ["z", "Z"] and
(result = this.getSize_t() or result = this.getSsize_t())
or
len = "t" and result = this.getPtrdiff_t()
or
len = "I" and
(result = this.getSize_t() or result = this.getPtrdiff_t())
or
len = "I32" and
exists(MicrosoftInt32Type t | t.getUnsigned() = result.(IntegralType).getUnsigned())
or
len = "I64" and
exists(MicrosoftInt64Type t | t.getUnsigned() = result.(IntegralType).getUnsigned())
or
len = "" and result instanceof IntType
}

/**
* Gets the family of integral types output / displayed by the nth
* conversion specifier's length flag.
*/
Type getIntegralDisplayType(int n) {
exists(string len |
len = this.getLength(n) and
(
len = "hh" and result instanceof CharType
or
len = "h" and result instanceof ShortType
or
len = "l" and result = this.getLongType()
or
len = ["ll", "L", "q"] and
result instanceof LongLongType
or
len = "j" and result = this.getIntmax_t()
or
len = ["z", "Z"] and
(result = this.getSize_t() or result = this.getSsize_t())
or
len = "t" and result = this.getPtrdiff_t()
or
len = "I" and
(result = this.getSize_t() or result = this.getPtrdiff_t())
or
len = "I32" and
exists(MicrosoftInt32Type t | t.getUnsigned() = result.(IntegralType).getUnsigned())
or
len = "I64" and
exists(MicrosoftInt64Type t | t.getUnsigned() = result.(IntegralType).getUnsigned())
or
len = "" and result instanceof IntType
)
exists(string len | len = this.getLength(n) |
len = "hh" and result instanceof CharType
or
len = "h" and result instanceof ShortType
or
not len = ["hh", "h"] and result = this.getIntegralTypeForLength(len)
)
}

Expand Down Expand Up @@ -869,9 +867,8 @@ class FormatLiteral extends Literal instanceof StringLiteral {
* Gets the argument type required by the nth conversion specifier.
*/
Type getConversionType(int n) {
result = this.getConversionType1(n) or
result = this.getIntegerConversionType(this.getLength(n), this.getConversionChar(n)) or
result = this.getConversionType1b(n) or
result = this.getConversionType2(n) or
result = this.getConversionType3(n) or
result = this.getConversionType4(n) or
result = this.getConversionType6(n) or
Expand All @@ -881,12 +878,19 @@ class FormatLiteral extends Literal instanceof StringLiteral {
result = this.getConversionType10(n)
}

private Type getConversionType1(int n) {
exists(string cnv | cnv = this.getConversionChar(n) |
cnv = ["d", "i"] and
result = this.getIntegralConversion(n) and
/**
* Gets the integer argument type for a length flag and conversion character.
* Shared by conversion type inference and replacement specifier suggestions.
*/
private Type getIntegerConversionType(string len, string conv) {
result = this.getIntegralTypeForLength(len) and
(
conv = ["d", "i"] and
not result.getUnderlyingType().(IntegralType).isExplicitlySigned() and
not result.getUnderlyingType().(IntegralType).isExplicitlyUnsigned()
or
conv = ["o", "u", "x", "X"] and
result.getUnderlyingType().(IntegralType).isUnsigned()
)
}

Expand Down Expand Up @@ -921,14 +925,6 @@ class FormatLiteral extends Literal instanceof StringLiteral {
)
}

private Type getConversionType2(int n) {
exists(string cnv | cnv = this.getConversionChar(n) |
cnv = ["o", "u", "x", "X"] and
result = this.getIntegralConversion(n) and
result.getUnderlyingType().(IntegralType).isUnsigned()
)
}

private Type getConversionType3(int n) {
exists(string cnv | cnv = this.getConversionChar(n) |
cnv = ["a", "A", "e", "E", "f", "F", "g", "G"] and result = this.getFloatingPointConversion(n)
Expand Down
15 changes: 14 additions & 1 deletion cpp/ql/src/Likely Bugs/Format/WrongTypeFormatArguments.ql
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,19 @@ predicate trivialConversion(ExpectedType expected, Type actual) {
*/
int sizeof_IntType() { exists(IntType it | result = it.getSize()) }

bindingset[ffc, n, arg, actual]
private string integerFormatSuggestion(FormattingFunctionCall ffc, int n, Expr arg, Type actual) {
if
ffc.getConversionArgument(n) = arg and
exists(ffc.getFormat().(FormatLiteral).getSuggestedIntegerConversionSpecifier(n, actual))
then
result =
" Consider using '" +
ffc.getFormat().(FormatLiteral).getSuggestedIntegerConversionSpecifier(n, actual) +
"' instead."
else result = ""
}

from FormattingFunctionCall ffc, int n, Expr arg, Type expected, Type actual
where
(
Expand All @@ -178,4 +191,4 @@ where
count(ffc.getTarget().getFormatParameterIndex()) = 1
select arg,
"This format specifier for type '" + expected.getName() + "' does not match the argument type '" +
actual.getUnspecifiedType().getName() + "'."
actual.getUnspecifiedType().getName() + "'." + integerFormatSuggestion(ffc, n, arg, actual)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* The `cpp/wrong-type-format-argument` query now suggests a matching format specifier for simple decimal integer conversions with mismatched `int`, `long`, or `long long` arguments. The set of reported results is unchanged.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
semmle-extractor-options: --clang --edg --signed_chars
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
| ../cases.h:11:10:11:13 | %d | 0 | %d | %d |
| ../cases.h:12:10:12:13 | %d | 0 | %u | %u |
| ../cases.h:13:10:13:13 | %d | 0 | %ld | %ld |
| ../cases.h:14:10:14:13 | %d | 0 | %lu | %lu |
| ../cases.h:15:10:15:13 | %d | 0 | %lld | %lld |
| ../cases.h:16:10:16:13 | %d | 0 | %llu | %llu |
| ../cases.h:17:10:17:13 | %i | 0 | %i | %i |
| ../cases.h:18:10:18:13 | %i | 0 | %u | %u |
| ../cases.h:19:10:19:13 | %i | 0 | %li | %li |
| ../cases.h:20:10:20:13 | %i | 0 | %lu | %lu |
| ../cases.h:21:10:21:13 | %i | 0 | %lli | %lli |
| ../cases.h:22:10:22:13 | %i | 0 | %llu | %llu |
| ../cases.h:23:10:23:13 | %u | 0 | %d | %d |
| ../cases.h:24:10:24:14 | %ld | 0 | %d | %d |
| ../cases.h:25:10:25:14 | %li | 0 | %u | %u |
| ../cases.h:26:10:26:14 | %lu | 0 | %lld | %lld |
| ../cases.h:27:10:27:15 | %lld | 0 | %ld | %ld |
| ../cases.h:28:10:28:15 | %lli | 0 | %llu | %llu |
| ../cases.h:29:10:29:15 | %llu | 0 | %lu | %lu |
| ../cases.h:30:10:30:13 | %d | 0 | %lu | %lu |
| ../cases.h:32:10:32:15 | %lld | 0 | %d | |
| ../cases.h:33:10:33:15 | %lld | 0 | %d | |
| ../cases.h:34:10:34:15 | %lld | 0 | %d | |
| ../cases.h:35:10:35:15 | %lld | 0 | %d | |
| ../cases.h:36:10:36:15 | %lld | 0 | | |
| ../cases.h:38:10:38:23 | value=%d end | 0 | %llu | %llu |
| ../cases.h:39:10:39:19 | %% %d %i | 0 | %ld | %ld |
| ../cases.h:39:10:39:19 | %% %d %i | 1 | %llu | %llu |
| ../cases.h:42:10:42:14 | %+d | 0 | | |
| ../cases.h:43:10:43:14 | % d | 0 | | |
| ../cases.h:44:10:44:14 | %-d | 0 | | |
| ../cases.h:45:10:45:14 | %#d | 0 | | |
| ../cases.h:46:10:46:14 | %'d | 0 | | |
| ../cases.h:47:10:47:15 | %08d | 0 | | |
| ../cases.h:48:10:48:15 | %12d | 0 | | |
| ../cases.h:49:10:49:14 | %.d | 0 | | |
| ../cases.h:50:10:50:15 | %.3d | 0 | | |
| ../cases.h:51:10:51:14 | %*d | 0 | | |
| ../cases.h:52:10:52:15 | %.*d | 0 | | |
| ../cases.h:53:10:53:15 | %1$d | 0 | | |
| ../cases.h:54:10:54:20 | %2$d %1$i | 0 | | |
| ../cases.h:54:10:54:20 | %2$d %1$i | 1 | | |
| ../cases.h:56:10:56:15 | %hhd | 0 | | |
| ../cases.h:57:10:57:14 | %hd | 0 | | |
| ../cases.h:58:10:58:14 | %jd | 0 | | |
| ../cases.h:59:10:59:14 | %zd | 0 | | |
| ../cases.h:60:10:60:14 | %td | 0 | | |
| ../cases.h:61:10:61:14 | %qd | 0 | | |
| ../cases.h:62:10:62:16 | %I64d | 0 | | |
| ../cases.h:63:10:63:13 | %x | 0 | | |
| ../cases.h:64:10:64:13 | %X | 0 | | |
| ../cases.h:65:10:65:13 | %o | 0 | | |
| ../cases.h:66:10:66:13 | %c | 0 | | |
| ../cases.h:67:10:67:13 | %s | 0 | | |
| ../cases.h:68:10:68:13 | %p | 0 | | |
| ../cases.h:69:10:69:13 | %f | 0 | | |
| ../cases.h:70:10:70:13 | %n | 0 | | |
| ../cases.h:71:10:71:13 | %d | 0 | | |
| ../cases.h:72:10:72:13 | %d | 0 | | |
| ../cases.h:73:10:73:13 | %d | 0 | | |
| ../cases.h:74:10:74:14 | %kd | 0 | | |
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import semmle.code.cpp.commons.Printf

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

from FormatLiteral format, int n
where exists(format.getConvSpecOffset(n))
select format, n,
concat(Expr arg |
arg = format.getUse().getConversionArgument(n)
|
format.getSuggestedIntegerConversionSpecifier(n, arg.getFullyConverted().getType()), ", "
),
concat(Expr arg |
arg = format.getUse().getConversionArgument(n)
|
format.getSuggestedIntegerConversionSpecifier(n, arg.getUnconverted().getType()), ", "
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "../cases.h"
Loading