Skip to content

Commit 9c8945f

Browse files
authored
Merge pull request #16403 from geoffw0/qhelp1
C++: Improve qhelp for IncorrectNotOperatorUsage.
2 parents 0338ffd + 73cc211 commit 9c8945f

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22

33
void f_warning(int i)
44
{
5-
// The usage of the logical not operator in this case is unlikely to be correct
5+
// BAD: 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
{
16-
if (i & ~FLAGS) // Changing the logical not operator for the bit-wise not operator would fix this logic
15+
if (i & ~FLAGS) // GOOD: Changing the logical not operator for the bit-wise not operator would fix this logic
1716
{
1817
// code
1918
}
20-
}
19+
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616
<p>Carefully inspect the flagged expressions. Consider the intent in the code logic, and decide whether it is necessary to change the not operator.</p>
1717
</recommendation>
1818

19-
<example><sample src="IncorrectNotOperatorUsage.cpp" /></example>
19+
<example>
20+
<p>Here is an example of this issue and how it can be fixed:</p>
21+
22+
<sample src="IncorrectNotOperatorUsage.cpp" />
23+
24+
<p>In other cases, particularly when the expressions have <code>bool</code> type, the fix may instead be of the form <code>a &amp;&amp; !b</code>.</p>
25+
</example>
2026

2127
<references>
2228
<li>

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)