Skip to content

Commit c0c7748

Browse files
ihsinmejketema
andauthored
Apply suggestions from code review
Co-authored-by: Jeroen Ketema <[email protected]>
1 parent 01f9114 commit c0c7748

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

cpp/ql/src/experimental/Security/CWE/CWE-754/ImproperCheckReturnValueScanf.qhelp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
<qhelp>
55
<overview>
66
<p>Working with reading data without validation procedures and with uninitialized arguments can lead to unpredictable consequences.</p>
7+
</overview>
78

9+
<recommendation>
10+
<p>
11+
The user should check the return value of `scanf` and related functions and check that any additional argument was assigned a value before reading the additional argument.
12+
</p>
813
</recommendation>
914
<example>
1015
<p>The following example demonstrates erroneous and corrected work with a function call.</p>

cpp/ql/src/experimental/Security/CWE/CWE-754/ImproperCheckReturnValueScanf.ql

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* @name Improper check return value scanf
3-
* @description Using a function call without the ability to evaluate the correctness of the work can lead to unexpected results.
2+
* @name Improper check of return value of scanf
3+
* @description Not checking the return value of scanf and related functions may lead to undefined behavior.
44
* @kind problem
55
* @id cpp/improper-check-return-value-scanf
66
* @problem.severity warning
@@ -27,30 +27,30 @@ int posArgumentInFunctionCall(FunctionCall fc) {
2727

2828
/** Holds if a function argument was not initialized but used after the call. */
2929
predicate argumentIsNotInitializedAndIsUsed(Variable vt, FunctionCall fc) {
30-
exists(Expr e0 |
31-
// Fillable argument was not initialized.
32-
vt instanceof LocalScopeVariable and
33-
not vt.getAnAssignment().getASuccessor+() = fc and
34-
(
35-
not vt.hasInitializer()
36-
or
37-
exists(Expr e1, Variable v1 |
38-
e1 = vt.getInitializer().getExpr() and
39-
v1 = e1.(AddressOfExpr).getOperand().(VariableAccess).getTarget() and
40-
(
41-
not v1.hasInitializer() and
42-
not v1.getAnAssignment().getASuccessor+() = fc
43-
)
30+
// Fillable argument was not initialized.
31+
vt instanceof LocalScopeVariable and
32+
not vt.getAnAssignment().getASuccessor+() = fc and
33+
(
34+
not vt.hasInitializer()
35+
or
36+
exists(Expr e, Variable v |
37+
e = vt.getInitializer().getExpr() and
38+
v = e.(AddressOfExpr).getOperand().(VariableAccess).getTarget() and
39+
(
40+
not v.hasInitializer() and
41+
not v.getAnAssignment().getASuccessor+() = fc
4442
)
45-
) and
46-
not exists(AssignExpr ae |
47-
ae.getLValue() = vt.getAnAccess().getParent() and
48-
ae.getASuccessor+() = fc
49-
) and
50-
not exists(FunctionCall f0 |
51-
f0.getAnArgument().getAChild() = vt.getAnAccess() and
52-
f0.getASuccessor+() = fc
53-
) and
43+
)
44+
) and
45+
not exists(AssignExpr ae |
46+
ae.getLValue() = vt.getAnAccess().getParent() and
47+
ae.getASuccessor+() = fc
48+
) and
49+
not exists(FunctionCall f0 |
50+
f0.getAnArgument().getAChild() = vt.getAnAccess() and
51+
f0.getASuccessor+() = fc
52+
) and
53+
exists(Expr e0 |
5454
// After the call, the completed arguments are assigned or returned as the result of the operation of the upper function.
5555
fc.getASuccessor+() = e0 and
5656
(

0 commit comments

Comments
 (0)