Skip to content

Commit 397bf7c

Browse files
committed
C++: Fix FPs caused by a syntax error
1 parent 496efee commit 397bf7c

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
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()

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
| macros.cpp:14:2:14:37 | call to printf | Format for printf (in a macro expansion) expects 4 arguments but given 3 |
66
| macros.cpp:21:2:21:36 | call to printf | Format for printf (in a macro expansion) expects 4 arguments but given 3 |
77
| macros.cpp:32:2:32:25 | call to printf | Format for printf (in a macro expansion) expects 1 arguments but given 0 |
8-
| syntax_errors.c:6:5:6:10 | call to printf | Format for printf expects 1 arguments but given 0 |
98
| test.c:9:2:9:7 | call to printf | Format for printf expects 1 arguments but given 0 |
109
| test.c:12:2:12:7 | call to printf | Format for printf expects 2 arguments but given 1 |
1110
| test.c:15:2:15:7 | call to printf | Format for printf expects 3 arguments but given 2 |

0 commit comments

Comments
 (0)