Skip to content

Commit f4e4e23

Browse files
committed
C++: Add test cases for IncorrectNotOperatorUsage.ql.
1 parent 541effb commit f4e4e23

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ void f_warning(int i)
44
{
55
// The usage of the logical not operator in this case is unlikely to be correct
66
// as the output is being used as an operator for a bit-wise and operation
7-
if (i & !FLAGS)
7+
if (i & !FLAGS)
88
{
99
// code
1010
}
1111
}
1212

13-
1413
void f_fixed(int i)
1514
{
1615
if (i & ~FLAGS) // Changing the logical not operator for the bit-wise not operator would fix this logic
1716
{
1817
// code
1918
}
20-
}
19+
}

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
void C6317_positive(int i)
55
{
6-
if (i & !FLAGS) // BUG
6+
if (i & !FLAGS) // BUG
77
{
88
}
99
}
@@ -71,3 +71,22 @@ void macroUsage(unsigned int arg1, unsigned int arg2)
7171

7272
}
7373
}
74+
75+
void bool_examples(bool a, bool b)
76+
{
77+
if (a & !b) // dubious (confusing intent, but shouldn't produce a wrong result)
78+
{
79+
}
80+
81+
if (a & ~b)
82+
{
83+
}
84+
85+
if (a && ~b)
86+
{
87+
}
88+
89+
if (a && !b)
90+
{
91+
}
92+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
| IncorrectNotOperatorUsage.cpp:48:9:48:18 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
1515
| IncorrectNotOperatorUsage.cpp:49:9:49:20 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
1616
| IncorrectNotOperatorUsage.cpp:70:10:70:34 | ... \| ... | Usage of a logical not (!) expression as a bitwise operator. |
17+
| IncorrectNotOperatorUsage.cpp:77:9:77:14 | ... & ... | Usage of a logical not (!) expression as a bitwise operator. |

0 commit comments

Comments
 (0)