Skip to content

Commit 39b2d2c

Browse files
authored
Merge pull request #17906 from github/calumgrant/bmn/wrong-number-format-args
C++: Fix FPs in cpp/wrong-number-format-arguments
2 parents a35a4b2 + 397bf7c commit 39b2d2c

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

cpp/ql/src/Likely Bugs/Format/WrongNumberOfFormatArguments.ql

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@
1616

1717
import cpp
1818

19+
class SyntaxError extends CompilerError {
20+
SyntaxError() { this.getTag().matches("exp_%") }
21+
22+
predicate affects(Element e) {
23+
exists(Location l1, Location l2 |
24+
l1 = this.getLocation() and
25+
l2 = e.getLocation()
26+
|
27+
l1.getFile() = l2.getFile() and
28+
l1.getStartLine() = l2.getStartLine()
29+
)
30+
}
31+
}
32+
1933
from FormatLiteral fl, FormattingFunctionCall ffc, int expected, int given, string ffcName
2034
where
2135
ffc = fl.getUse() and
@@ -27,7 +41,10 @@ where
2741
if ffc.isInMacroExpansion()
2842
then ffcName = ffc.getTarget().getName() + " (in a macro expansion)"
2943
else ffcName = ffc.getTarget().getName()
30-
)
44+
) and
45+
// A typical problem is that string literals are concatenated, but if one of the string
46+
// literals is an undefined macro, then this just leads to a syntax error.
47+
not exists(SyntaxError e | e.affects(fl))
3148
select ffc,
3249
"Format for " + ffcName + " expects " + expected.toString() + " arguments but given " +
3350
given.toString()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// semmle-extractor-options: --expect_errors
2+
3+
extern int printf(const char *fmt, ...);
4+
5+
void test_syntax_error() {
6+
printf("Error code %d: " FMT_MSG, 0, "");
7+
}

0 commit comments

Comments
 (0)