Skip to content

Commit 853128c

Browse files
committed
C++: Clean up false-positives
C++: Change note
1 parent fe85e00 commit 853128c

File tree

6 files changed

+16
-13
lines changed

6 files changed

+16
-13
lines changed

cpp/ql/lib/semmle/code/cpp/models/implementations/Printf.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ private class Fprintf extends FormattingFunction, NonThrowingFunction {
5050
override int getFormatParameterIndex() { result = 1 }
5151

5252
override int getOutputParameterIndex(boolean isStream) { result = 0 and isStream = true }
53+
54+
override int getFirstFormatArgumentIndex() { result = 2 }
5355
}
5456

5557
/**
@@ -91,7 +93,7 @@ private class Sprintf extends FormattingFunction, NonThrowingFunction {
9193
override int getFirstFormatArgumentIndex() {
9294
if this.hasName("__builtin___sprintf_chk")
9395
then result = 4
94-
else result = this.getNumberOfParameters()
96+
else result = this.getNumberOfExplicitParameters()
9597
}
9698
}
9799

cpp/ql/lib/semmle/code/cpp/models/interfaces/FormattingFunction.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ abstract class FormattingFunction extends ArrayFunction, TaintFunction {
143143
* from implicit function declarations. If there is some inconsistency in the number
144144
* of parameters, then don't return anything.
145145
*/
146-
private int getNumberOfExplicitParameters() {
146+
int getNumberOfExplicitParameters() {
147147
forex(FunctionDeclarationEntry fde | fde = this.getAnExplicitDeclarationEntry() |
148148
result = fde.getNumberOfParameters()
149149
)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ where
170170
) and
171171
not arg.isAffectedByMacro() and
172172
not arg.isFromUninstantiatedTemplate(_) and
173-
not actual.getUnspecifiedType() instanceof ErroneousType
173+
not actual.getUnspecifiedType() instanceof ErroneousType and
174+
not arg.(Call).getTarget().getADeclarationEntry().isImplicit()
174175
select arg,
175176
"This format specifier for type '" + expected.getName() + "' does not match the argument type '" +
176177
actual.getUnspecifiedType().getName() + "'."
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Fixed false positives in the `cpp/wrong-type-format-argument` ("Wrong type of arguments to formatting function") query if there are extraction errors in the function.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
| tests.c:7:18:7:18 | 1 | This format specifier for type 'char *' does not match the argument type 'int'. |
2-
| tests.c:8:18:8:34 | call to implicit_function | This format specifier for type 'char *' does not match the argument type 'int'. |
3-
| tests.c:9:13:9:13 | 0 | This format specifier for type 'char *' does not match the argument type 'int'. |
4-
| tests.c:10:13:10:13 | 0 | This format specifier for type 'char *' does not match the argument type 'int'. |
1+
| tests.c:6:18:6:18 | 1 | This format specifier for type 'char *' does not match the argument type 'int'. |
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// semmle-extractor-options: --expect_errors
22

33
int printf(const char * format, ...);
4-
int fprintf();
54

6-
int f() {
7-
printf("%s", 1); // BAD - TP
8-
printf("%s", implicit_function()); // BAD (FP) - we should not infer the return type
9-
sprintf(0, "%s", ""); // BAD (FP)
10-
fprintf(0, "%s", ""); // BAD (FP)
5+
void f() {
6+
printf("%s", 1); // BAD
7+
printf("%s", implicit_function()); // GOOD - we should ignore the type
8+
sprintf(0, "%s", ""); // GOOD
9+
fprintf(0, "%s", ""); // GOOD
1110
}

0 commit comments

Comments
 (0)