Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f9edaba

Browse files
Fixing typo.
Restricting to analyze only logical & and | operations
1 parent 5212aa0 commit f9edaba

7 files changed

Lines changed: 47 additions & 76 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
/.vs/ql/v15/Browse.VC.db
1414
/.vs/ProjectSettings.json
1515

16+
/.vs/ql_6317/v15/Browse.VC.opendb
17+
/.vs/ql_6317/v15/Browse.VC.db
18+
/.vs/ql_6317/v15/.suo

cpp/ql/src/Likely Bugs/Likely Typos/IncorrectNotOperatorUsage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
void f_warning(int i)
44
{
55
// The usage of the logical not operator in this case is unlikely to be correct
6-
// as the output is being used as an opeartor for a bit-wise and operation
6+
// as the output is being used as an operator for a bit-wise and operation
77
if (i & !FLAGS)
88
{
99
// code

cpp/ql/src/Likely Bugs/Likely Typos/IncorrectNotOperatorUsage.qhelp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<p>This rule finds logical-not operator usage as an operator for in a bit-wise operation.</p>
88

99
<p>Due to the nature of logical operation result value, only the lowest bit could possibly be set, and it is unlikely to be intent in bitwise opeartions. Violations are often indicative of a typo, using a logical-not (<code>!</code>) opeartor instead of the bit-wise not (<code>~</code>) operator. </p>
10+
<p>This rule is restricted to analyze bit-wise and (<code>&amp;</code>) and bit-wise or (<code>|</code>) operation in order to provide better precision.</p>
1011
<p>This rule ignores instances where a double negation (<code>!!</code>) is explicitly used as the opeartor of the bitwise operation, as this is a commonly used as a mechanism to normalize an integer value to either 1 or 0.</p>
1112
<p>NOTE: It is not recommended to use this rule in kernel code or older C code as it will likely find several false positive instances.</p>
1213

cpp/ql/src/Likely Bugs/Likely Typos/IncorrectNotOperatorUsage.ql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @kind problem
77
* @id cpp/incorrect-not-operator-usage
88
* @problem.severity warning
9-
* @precision low
9+
* @precision medium
1010
* @tags security
1111
* external/cwe/cwe-480
1212
* external/microsoft/c6317
@@ -21,14 +21,15 @@ import cpp
2121
* indicates the explicit purpose to normalize the result for bit-wise or arithmetic purposes.
2222
*/
2323
predicate doubleNegationNormalization( NotExpr notexpr ){
24-
exists( NotExpr doubleNot |
25-
doubleNot = notexpr.getAnOperand())
24+
notexpr.getAnOperand() instanceof NotExpr
2625
}
2726

2827
from BinaryBitwiseOperation binbitwop
2928
where exists( NotExpr notexpr |
3029
binbitwop.getAnOperand() = notexpr
3130
and not doubleNegationNormalization(notexpr)
31+
and ( binbitwop instanceof BitwiseAndExpr
32+
or binbitwop instanceof BitwiseOrExpr )
3233
)
3334
select binbitwop, "Usage of a logical not (!) expression as a bitwise operator."
3435

cpp/ql/test/query-tests/Likely Bugs/Likely Typos/IncorrectNotOperatorUsage/IncorrectNotOperatorUsage.c

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ void C6317_negative(int i)
1313
if (i & ~FLAGS)
1414
{
1515
}
16+
17+
if (i && ~FLAGS)
18+
{
19+
}
20+
21+
if (i && !FLAGS)
22+
{
23+
}
1624
}
1725

1826
void bitwiseAndUsage(unsigned int l, unsigned int r)
@@ -48,33 +56,13 @@ void bitwiseOrUsage(unsigned int l, unsigned int r)
4856
x = !FLAGS || !!r; // Not a bug - logical or
4957
}
5058

51-
void bitwiseXorUsage(unsigned int l, unsigned int r)
52-
{
53-
unsigned int x;
54-
55-
x = l ^ !r; //BUG
56-
x = !FLAGS ^ r; //BUG
57-
x = !FLAGS ^ !!r; //BUG
58-
59-
x = !!l ^ r; // Not a bug - double negation
60-
x = !!!l ^ r; // Not a bug - double negation
61-
x = !!FLAGS ^ r; // Not a bug - double negation
62-
}
63-
64-
void shoftUsage(unsigned int val)
59+
void bitwiseOperatorsNotCovered(unsigned int l, unsigned int r)
6560
{
6661
unsigned int x;
6762

68-
x = !val << 1; //BUG
69-
x = !val >> 1; //BUG
70-
71-
x = !!val << 2; // Not a bug - double negation
72-
x = !!val >> 2; // Not a bug - double negation
73-
}
74-
75-
unsigned int bitWiseShiftUsage(unsigned int val)
76-
{
77-
return ((unsigned int)(!!val) << 4) + ((unsigned int)(!!val) >> 1); // Not a bug (double negation)
63+
x = l ^ !r;
64+
x = !l << 1;
65+
x = !l >> 1;
7866
}
7967

8068
void macroUsage(unsigned int arg1, unsigned int arg2)

cpp/ql/test/query-tests/Likely Bugs/Likely Typos/IncorrectNotOperatorUsage/IncorrectNotOperatorUsage.cpp

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ void C6317_negative(int i)
1313
if (i & ~FLAGS)
1414
{
1515
}
16+
17+
if (i && ~FLAGS)
18+
{
19+
}
20+
21+
if (i && !FLAGS)
22+
{
23+
}
1624
}
1725

1826
void bitwiseAndUsage(unsigned int l, unsigned int r)
@@ -48,33 +56,13 @@ void bitwiseOrUsage(unsigned int l, unsigned int r)
4856
x = !FLAGS || !!r; // Not a bug - logical or
4957
}
5058

51-
void bitwiseXorUsage(unsigned int l, unsigned int r)
52-
{
53-
unsigned int x;
54-
55-
x = l ^ !r; //BUG
56-
x = !FLAGS ^ r; //BUG
57-
x = !FLAGS ^ !!r; //BUG
58-
59-
x = !!l ^ r; // Not a bug - double negation
60-
x = !!!l ^ r; // Not a bug - double negation
61-
x = !!FLAGS ^ r; // Not a bug - double negation
62-
}
63-
64-
void shoftUsage(unsigned int val)
59+
void bitwiseOperatorsNotCovered(unsigned int l, unsigned int r)
6560
{
6661
unsigned int x;
6762

68-
x = !val << 1; //BUG
69-
x = !val >> 1; //BUG
70-
71-
x = !!val << 2; // Not a bug - double negation
72-
x = !!val >> 2; // Not a bug - double negation
73-
}
74-
75-
unsigned int bitWiseShiftUsage(unsigned int val)
76-
{
77-
return ((unsigned int)(!!val) << 4) + ((unsigned int)(!!val) >> 1); // Not a bug (double negation)
63+
x = l ^ !r;
64+
x = !l << 1;
65+
x = !l >> 1;
7866
}
7967

8068
void macroUsage(unsigned int arg1, unsigned int arg2)
Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
| IncorrectNotOperatorUsage.c:6:9:6:18 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |
2-
| IncorrectNotOperatorUsage.c:23:9:23:14 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |
3-
| IncorrectNotOperatorUsage.c:24:9:24:18 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |
4-
| IncorrectNotOperatorUsage.c:25:9:25:20 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |
5-
| IncorrectNotOperatorUsage.c:39:9:39:14 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
6-
| IncorrectNotOperatorUsage.c:40:9:40:18 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
7-
| IncorrectNotOperatorUsage.c:41:9:41:20 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
8-
| IncorrectNotOperatorUsage.c:55:9:55:14 | ... ^ ... | Usage of a logical not (!) expression as a bitwise operator. |
9-
| IncorrectNotOperatorUsage.c:56:9:56:18 | ... ^ ... | Usage of a logical not (!) expression as a bitwise operator. |
10-
| IncorrectNotOperatorUsage.c:57:9:57:20 | ... ^ ... | Usage of a logical not (!) expression as a bitwise operator. |
11-
| IncorrectNotOperatorUsage.c:68:9:68:17 | ... << ... | Usage of a logical not (!) expression as a bitwise operator. |
12-
| IncorrectNotOperatorUsage.c:69:9:69:17 | ... >> ... | Usage of a logical not (!) expression as a bitwise operator. |
13-
| IncorrectNotOperatorUsage.c:82:10:82:34 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
2+
| IncorrectNotOperatorUsage.c:31:9:31:14 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |
3+
| IncorrectNotOperatorUsage.c:32:9:32:18 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |
4+
| IncorrectNotOperatorUsage.c:33:9:33:20 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |
5+
| IncorrectNotOperatorUsage.c:47:9:47:14 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
6+
| IncorrectNotOperatorUsage.c:48:9:48:18 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
7+
| IncorrectNotOperatorUsage.c:49:9:49:20 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
8+
| IncorrectNotOperatorUsage.c:70:10:70:34 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
149
| IncorrectNotOperatorUsage.cpp:6:9:6:18 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |
15-
| IncorrectNotOperatorUsage.cpp:23:9:23:14 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |
16-
| IncorrectNotOperatorUsage.cpp:24:9:24:18 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |
17-
| IncorrectNotOperatorUsage.cpp:25:9:25:20 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |
18-
| IncorrectNotOperatorUsage.cpp:39:9:39:14 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
19-
| IncorrectNotOperatorUsage.cpp:40:9:40:18 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
20-
| IncorrectNotOperatorUsage.cpp:41:9:41:20 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
21-
| IncorrectNotOperatorUsage.cpp:55:9:55:14 | ... ^ ... | Usage of a logical not (!) expression as a bitwise operator. |
22-
| IncorrectNotOperatorUsage.cpp:56:9:56:18 | ... ^ ... | Usage of a logical not (!) expression as a bitwise operator. |
23-
| IncorrectNotOperatorUsage.cpp:57:9:57:20 | ... ^ ... | Usage of a logical not (!) expression as a bitwise operator. |
24-
| IncorrectNotOperatorUsage.cpp:68:9:68:17 | ... << ... | Usage of a logical not (!) expression as a bitwise operator. |
25-
| IncorrectNotOperatorUsage.cpp:69:9:69:17 | ... >> ... | Usage of a logical not (!) expression as a bitwise operator. |
26-
| IncorrectNotOperatorUsage.cpp:82:10:82:34 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
10+
| IncorrectNotOperatorUsage.cpp:31:9:31:14 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |
11+
| IncorrectNotOperatorUsage.cpp:32:9:32:18 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |
12+
| IncorrectNotOperatorUsage.cpp:33:9:33:20 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |
13+
| IncorrectNotOperatorUsage.cpp:47:9:47:14 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
14+
| IncorrectNotOperatorUsage.cpp:48:9:48:18 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
15+
| IncorrectNotOperatorUsage.cpp:49:9:49:20 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
16+
| IncorrectNotOperatorUsage.cpp:70:10:70:34 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |

0 commit comments

Comments
 (0)