Skip to content

Commit 31684d2

Browse files
committed
C++: Remove FPs in cpp/wrong-number-format-arguments due to BMN
1 parent 6a0212e commit 31684d2

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

cpp/ql/lib/semmle/code/cpp/Function.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,8 @@ class FunctionDeclarationEntry extends DeclarationEntry, @fun_decl {
652652
/**
653653
* Holds if this declaration is an implicit function declaration, that is,
654654
* where a function is used before it is declared (under older C standards).
655+
* This can also happen in standalone extraction when a function has not been
656+
* properly declared.
655657
*/
656658
predicate isImplicit() { fun_implicit(underlyingElement(this)) }
657659

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,31 @@ abstract class FormattingFunction extends ArrayFunction, TaintFunction {
121121
* the first format specifier in the format string.
122122
*/
123123
int getFirstFormatArgumentIndex() {
124-
result = this.getNumberOfParameters() and
125-
// the formatting function either has a definition in the snapshot, or all
124+
// The formatting function either has a definition in the snapshot, or all
126125
// `DeclarationEntry`s agree on the number of parameters (otherwise we don't
127126
// really know the correct number)
128-
(
129-
this.hasDefinition()
130-
or
131-
forall(FunctionDeclarationEntry fde | fde = this.getADeclarationEntry() |
132-
result = fde.getNumberOfParameters()
133-
)
127+
if this.hasDefinition()
128+
then result = this.getDefinition().getNumberOfParameters()
129+
else result = this.getNumberOfExplicitParameters()
130+
}
131+
132+
/**
133+
* Gets a non-implicit function declaration entry.
134+
*/
135+
FunctionDeclarationEntry getAnExplicitDeclarationEntry() {
136+
result = this.getADeclarationEntry() and
137+
not result.isImplicit()
138+
}
139+
140+
/**
141+
* Gets the number of parameters, excluding any parameters that have been defined
142+
* from implicit function declarations. If there is some inconsistency in the number
143+
* of parameters, then don't return anything.
144+
*/
145+
int getNumberOfExplicitParameters() {
146+
result = this.getAnExplicitDeclarationEntry().getNumberOfParameters() and
147+
forall(FunctionDeclarationEntry fde | fde = this.getAnExplicitDeclarationEntry() |
148+
result = fde.getNumberOfParameters()
134149
)
135150
}
136151

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,4 @@
1010
| test.c:15:2:15:7 | call to printf | Format for printf expects 3 arguments but given 2 |
1111
| test.c:19:2:19:7 | call to printf | Format for printf expects 2 arguments but given 1 |
1212
| test.c:29:3:29:8 | call to printf | Format for printf expects 2 arguments but given 1 |
13-
| test.c:51:2:51:10 | call to my_logger | Format for my_logger expects 6 arguments but given 1 |
14-
| test.c:52:2:52:10 | call to my_logger | Format for my_logger expects 3 arguments but given 0 |
15-
| test.c:53:2:53:10 | call to my_logger | Format for my_logger expects 3 arguments but given 0 |
13+
| test.c:53:2:53:10 | call to my_logger | Format for my_logger expects 3 arguments but given 2 |

0 commit comments

Comments
 (0)