Skip to content

Commit d7e560c

Browse files
authored
Merge pull request github#5767 from ihsinme/ihsinme-patch-268
CPP: Add query for CWE-1126: Declaration of Variable with Unnecessarily Wide Scope
2 parents 922b276 + 9e5a38d commit d7e560c

File tree

6 files changed

+161
-0
lines changed

6 files changed

+161
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
while(intIndex > 2)
2+
{
3+
...
4+
intIndex--;
5+
...
6+
} // GOOD: correct cycle
7+
...
8+
while(intIndex > 2)
9+
{
10+
...
11+
int intIndex;
12+
intIndex--;
13+
...
14+
} // BAD: the variable used in the condition does not change.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>Using variables with the same name is dangerous. However, such a situation inside the while loop can create an infinite loop exhausting resources. Requires the attention of developers.</p>
7+
8+
</overview>
9+
<recommendation>
10+
<p>We recommend not to use local variables inside a loop if their names are the same as the variables in the condition of this loop.</p>
11+
12+
</recommendation>
13+
<example>
14+
<p>The following example demonstrates an erroneous and corrected use of a local variable within a loop.</p>
15+
<sample src="DeclarationOfVariableWithUnnecessarilyWideScope.c" />
16+
17+
</example>
18+
<references>
19+
20+
<li>
21+
CERT C Coding Standard:
22+
<a href="https://wiki.sei.cmu.edu/confluence/display/c/DCL01-C.+Do+not+reuse+variable+names+in+subscopes">DCL01-C. Do not reuse variable names in subscopes</a>.
23+
</li>
24+
25+
</references>
26+
</qhelp>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @name Errors When Using Variable Declaration Inside Loop
3+
* @description Using variables with the same name is dangerous.
4+
* However, such a situation inside the while loop can create an infinite loop exhausting resources.
5+
* Requires the attention of developers.
6+
* @kind problem
7+
* @id cpp/errors-when-using-variable-declaration-inside-loop
8+
* @problem.severity warning
9+
* @precision medium
10+
* @tags correctness
11+
* security
12+
* external/cwe/cwe-1126
13+
*/
14+
15+
import cpp
16+
17+
/**
18+
* Errors when using a variable declaration inside a loop.
19+
*/
20+
class DangerousWhileLoop extends WhileStmt {
21+
Expr exp;
22+
Declaration dl;
23+
24+
DangerousWhileLoop() {
25+
this = dl.getParentScope().(BlockStmt).getParent*() and
26+
exp = this.getCondition().getAChild*() and
27+
not exp instanceof PointerFieldAccess and
28+
not exp instanceof ValueFieldAccess and
29+
exp.(VariableAccess).getTarget().getName() = dl.getName() and
30+
not exp.getParent*() instanceof FunctionCall
31+
}
32+
33+
Declaration getDeclaration() { result = dl }
34+
35+
/** Holds when there are changes to the variables involved in the condition. */
36+
predicate isUseThisVariable() {
37+
exists(Variable v |
38+
this.getCondition().getAChild*().(VariableAccess).getTarget() = v and
39+
(
40+
exists(Assignment aexp |
41+
this = aexp.getEnclosingStmt().getParentStmt*() and
42+
(
43+
aexp.getLValue().(ArrayExpr).getArrayBase().(VariableAccess).getTarget() = v
44+
or
45+
aexp.getLValue().(VariableAccess).getTarget() = v
46+
)
47+
)
48+
or
49+
exists(CrementOperation crm |
50+
this = crm.getEnclosingStmt().getParentStmt*() and
51+
crm.getOperand().(VariableAccess).getTarget() = v
52+
)
53+
)
54+
)
55+
}
56+
}
57+
58+
from DangerousWhileLoop lp
59+
where not lp.isUseThisVariable()
60+
select lp.getDeclaration(), "A variable with this name is used in the $@ condition.", lp, "loop"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.c:14:9:14:16 | intIndex | A variable with this name is used in the $@ condition. | test.c:11:3:16:3 | while (...) ... | loop |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
experimental/Security/CWE/CWE-1126/DeclarationOfVariableWithUnnecessarilyWideScope.ql
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
void workFunction_0(char *s) {
2+
int intIndex = 10;
3+
int intGuard;
4+
char buf[80];
5+
while(intIndex > 2) // GOOD
6+
{
7+
buf[intIndex] = 1;
8+
intIndex--;
9+
}
10+
intIndex = 10;
11+
while(intIndex > 2)
12+
{
13+
buf[intIndex] = 1;
14+
int intIndex; // BAD
15+
intIndex--;
16+
}
17+
intIndex = 10;
18+
intGuard = 20;
19+
while(intIndex < intGuard--) // GOOD
20+
{
21+
buf[intIndex] = 1;
22+
int intIndex;
23+
intIndex--;
24+
}
25+
intIndex = 10;
26+
intGuard = 20;
27+
while(intIndex < intGuard) // GOOD
28+
{
29+
buf[intIndex] = 1;
30+
int intIndex;
31+
intIndex++;
32+
intGuard--;
33+
}
34+
intIndex = 10;
35+
intGuard = 20;
36+
while(intIndex < intGuard) // GOOD
37+
{
38+
buf[intIndex] = 1;
39+
int intIndex;
40+
intIndex--;
41+
intGuard -= 4;
42+
}
43+
intIndex = 10;
44+
while(intIndex > 2) // GOOD
45+
{
46+
buf[intIndex] = 1;
47+
intIndex -= 2;
48+
int intIndex;
49+
intIndex--;
50+
}
51+
intIndex = 10;
52+
while(intIndex > 2) // GOOD
53+
{
54+
buf[intIndex] = 1;
55+
--intIndex;
56+
int intIndex;
57+
intIndex--;
58+
}
59+
}

0 commit comments

Comments
 (0)