Skip to content

Commit 01c13c4

Browse files
authored
Add files via upload
1 parent 15049ca commit 01c13c4

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
if(len=funcReadData()==0) return 1; // BAD: variable `len` will not equal the value returned by function `funcReadData()`
2+
...
3+
if((len=funcReadData())==0) return 1; // GOOD: variable `len` equal the value returned by function `funcReadData()`
4+
...
5+
bool a=true;
6+
a++;// BAD: variable `a` does not change its meaning
7+
bool b;
8+
b=-a;// BAD: variable `b` equal `true`
9+
...
10+
a=false;// GOOD: variable `a` equal `false`
11+
b=!a;// GOOD: variable `b` equal `false`
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>Finding places of confusing use of boolean type. For example, a unary minus does not work before a boolean type and an increment always gives true.</p>
7+
8+
9+
</overview>
10+
<recommendation>
11+
12+
<p>we recommend making the code simpler.</p>
13+
14+
</recommendation>
15+
<example>
16+
<p>The following example demonstrates erroneous and fixed methods for using a boolean data type.</p>
17+
<sample src="OperatorPrecedenceLogicErrorWhenUseBoolType.c" />
18+
19+
</example>
20+
<references>
21+
22+
<li>
23+
CERT C Coding Standard:
24+
<a href="https://wiki.sei.cmu.edu/confluence/display/c/EXP00-C.+Use+parentheses+for+precedence+of+operation">EXP00-C. Use parentheses for precedence of operation</a>.
25+
</li>
26+
27+
</references>
28+
</qhelp>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @name Operator Precedence Logic Error When Use Bool Type
3+
* @description --Finding places of confusing use of boolean type.
4+
* --For example, a unary minus does not work before a boolean type and an increment always gives true.
5+
* @kind problem
6+
* @id cpp/operator-precedence-logic-error-when-use-bool-type
7+
* @problem.severity warning
8+
* @precision medium
9+
* @tags correctness
10+
* security
11+
* external/cwe/cwe-783
12+
* external/cwe/cwe-480
13+
*/
14+
15+
import cpp
16+
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
17+
18+
/** Holds, if it is an expression, a boolean increment. */
19+
predicate incrementBoolType(Expr exp) {
20+
exp.(IncrementOperation).getOperand().getType() instanceof BoolType
21+
}
22+
23+
/** Holds, if this is an expression, applies a minus to a boolean type. */
24+
predicate revertSignBoolType(Expr exp) {
25+
exp.(AssignExpr).getRValue().(UnaryMinusExpr).getAnOperand().getType() instanceof BoolType and
26+
exp.(AssignExpr).getLValue().getType() instanceof BoolType
27+
}
28+
29+
/** Holds, if this is an expression, uses comparison and assignment outside of execution precedence. */
30+
predicate assignBoolType(Expr exp) {
31+
exists(ComparisonOperation co |
32+
exp.(AssignExpr).getRValue() = co and
33+
exp.isCondition() and
34+
not co.isParenthesised() and
35+
not exp.(AssignExpr).getLValue().getType() instanceof BoolType and
36+
co.getLeftOperand() instanceof FunctionCall and
37+
not co.getRightOperand().getType() instanceof BoolType and
38+
not co.getRightOperand().getValue() = "0" and
39+
not co.getRightOperand().getValue() = "1"
40+
)
41+
}
42+
43+
from Expr exp
44+
where
45+
incrementBoolType(exp) or
46+
revertSignBoolType(exp) or
47+
assignBoolType(exp)
48+
select exp, "this expression needs attention"

0 commit comments

Comments
 (0)