Skip to content

Commit d8800c0

Browse files
committed
C++: new helper predicates in ScanfFunctionCall
Extract some of the logic from the `cpp/missing-check-scanf` query into the more generally useful `getOutputArgument(int index)`, `getAnOutputArgument()`, and `getNumberOfOutputArguments()` predicates.
1 parent 5c894ae commit d8800c0

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

cpp/ql/lib/semmle/code/cpp/commons/Scanf.qll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,28 @@ class ScanfFunctionCall extends FunctionCall {
143143
* (rather than a `char*`).
144144
*/
145145
predicate isWideCharDefault() { this.getScanfFunction().isWideCharDefault() }
146+
147+
/**
148+
* Gets the output argument at position `n` in the vararg list of this call.
149+
*
150+
* The range of `n` is from `0` to `this.getNumberOfOutputArguments() - 1`.
151+
*/
152+
Expr getOutputArgument(int n) {
153+
result = this.getArgument(this.getTarget().getNumberOfParameters() + n) and
154+
n >= 0
155+
}
156+
157+
/**
158+
* Gets an output argument given to this call in vararg position.
159+
*/
160+
Expr getAnOutputArgument() { result = this.getOutputArgument(_) }
161+
162+
/**
163+
* Gets the number of output arguments present in this call.
164+
*/
165+
int getNumberOfOutputArguments() {
166+
result = this.getNumberOfArguments() - this.getTarget().getNumberOfParameters()
167+
}
146168
}
147169

148170
/**

cpp/ql/src/Critical/MissingCheckScanf.ql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ class ScanfOutput extends Expr {
2525
ValueNumber valNum;
2626

2727
ScanfOutput() {
28-
this = call.getArgument(call.getTarget().getNumberOfParameters() + varargIndex) and
29-
varargIndex >= 0 and
28+
this = call.getOutputArgument(varargIndex) and
3029
instr.getUnconvertedResultExpression() = this and
3130
valueNumber(instr) = valNum and
3231
// The following line is a kludge to prohibit more than one associated `instr` field,

0 commit comments

Comments
 (0)