Skip to content

Commit 4197805

Browse files
committed
C++: Resolve firstFormatArgumentIndex in FormattingFunction CP
1 parent 4341fab commit 4197805

File tree

3 files changed

+22
-29
lines changed

3 files changed

+22
-29
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,14 @@ class Function extends Declaration, ControlFlowNode, AccessHolder, @function {
230230
)
231231
}
232232

233+
/**
234+
* Gets a non-implicit function declaration entry.
235+
*/
236+
FunctionDeclarationEntry getAnExplicitDeclarationEntry() {
237+
result = this.getADeclarationEntry() and
238+
not result.isImplicit()
239+
}
240+
233241
private predicate declEntry(FunctionDeclarationEntry fde) {
234242
fun_decls(unresolveElement(fde), underlyingElement(this), _, _, _) and
235243
// If one .cpp file specializes a function, and another calls the

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private class Sprintf extends FormattingFunction, NonThrowingFunction {
9191
override int getFirstFormatArgumentIndex() {
9292
if this.hasName("__builtin___sprintf_chk")
9393
then result = 4
94-
else result = this.getNumberOfExplicitParameters()
94+
else result = super.getFirstFormatArgumentIndex()
9595
}
9696
}
9797

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

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ private Type getAFormatterWideTypeOrDefault() {
4242
* A standard library function that uses a `printf`-like formatting string.
4343
*/
4444
abstract class FormattingFunction extends ArrayFunction, TaintFunction {
45+
int firstFormatArgumentIndex;
46+
47+
FormattingFunction() {
48+
firstFormatArgumentIndex > 0 and
49+
if this.hasDefinition()
50+
then firstFormatArgumentIndex = this.getDefinition().getNumberOfParameters()
51+
else
52+
forex(FunctionDeclarationEntry fde | fde = this.getAnExplicitDeclarationEntry() |
53+
firstFormatArgumentIndex = fde.getNumberOfParameters()
54+
)
55+
}
56+
4557
/** Gets the position at which the format parameter occurs. */
4658
abstract int getFormatParameterIndex();
4759

@@ -121,34 +133,7 @@ abstract class FormattingFunction extends ArrayFunction, TaintFunction {
121133
* the first format specifier in the format string. We ignore all
122134
* implicit function definitions.
123135
*/
124-
int getFirstFormatArgumentIndex() {
125-
// The formatting function either has a definition in the snapshot, or all
126-
// `DeclarationEntry`s agree on the number of parameters (otherwise we don't
127-
// really know the correct number)
128-
result > 0 and // Avoid invalid declarations
129-
if this.hasDefinition()
130-
then result = this.getDefinition().getNumberOfParameters()
131-
else result = this.getNumberOfExplicitParameters()
132-
}
133-
134-
/**
135-
* Gets a non-implicit function declaration entry.
136-
*/
137-
private FunctionDeclarationEntry getAnExplicitDeclarationEntry() {
138-
result = this.getADeclarationEntry() and
139-
not result.isImplicit()
140-
}
141-
142-
/**
143-
* Gets the number of parameters, excluding any parameters that have been defined
144-
* from implicit function declarations. If there is some inconsistency in the number
145-
* of parameters, then don't return anything.
146-
*/
147-
int getNumberOfExplicitParameters() {
148-
forex(FunctionDeclarationEntry fde | fde = this.getAnExplicitDeclarationEntry() |
149-
result = fde.getNumberOfParameters()
150-
)
151-
}
136+
int getFirstFormatArgumentIndex() { result = firstFormatArgumentIndex }
152137

153138
/**
154139
* Gets the position of the buffer size argument, if any.

0 commit comments

Comments
 (0)