Skip to content

Commit a7564c9

Browse files
committed
C++: Add a test of unsigned count-down loops.
1 parent 205dd1a commit a7564c9

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,21 @@ void FalseNegativeTestCases()
177177
for (int i = 100; i > 0; i += 2) {}
178178
// For comparison
179179
for (int i = 100; i > 0; i ++ ) {} // BUG
180+
}
181+
182+
void IntendedOverflow()
183+
{
184+
const unsigned char m = 10;
185+
unsigned char i;
186+
signed char s;
187+
188+
for (i = 63; i < 64; i--) {} // GOOD (legitimate way to count down with an unsigned) [FALSE POSITIVE]
189+
for (i = 63; i < 128; i--) {} // DUBIOUS (could still be a typo?)
190+
for (i = 63; i < 255; i--) {} // GOOD [FALSE POSITIVE]
191+
192+
for (i = m - 1; i < m; i--) {} // GOOD [FALSE POSITIVE]
193+
for (i = m - 1; i < m; i--) {} // DUBIOUS
194+
for (i = m - 1; i < m; i--) {} // GOOD [FALSE POSITIVE]
195+
196+
for (s = 63; s < 64; s--) {} // BAD (signed numbers don't wrap at 0 / at all)
180197
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,10 @@
2020
| inconsistentLoopDirection.cpp:140:5:142:5 | for(...;...;...) ... | Ill-defined for-loop: a loop using variable "i" counts upward from a value (200), but the terminal condition is lower (0). |
2121
| inconsistentLoopDirection.cpp:175:5:175:36 | for(...;...;...) ... | Ill-defined for-loop: a loop using variable "i" counts downward from a value (0), but the terminal condition is higher (10). |
2222
| inconsistentLoopDirection.cpp:179:5:179:38 | for(...;...;...) ... | Ill-defined for-loop: a loop using variable "i" counts upward from a value (100), but the terminal condition is lower (0). |
23+
| inconsistentLoopDirection.cpp:188:5:188:32 | for(...;...;...) ... | Ill-defined for-loop: a loop using variable "i" counts downward from a value (63), but the terminal condition is higher (64). |
24+
| inconsistentLoopDirection.cpp:189:5:189:33 | for(...;...;...) ... | Ill-defined for-loop: a loop using variable "i" counts downward from a value (63), but the terminal condition is higher (128). |
25+
| inconsistentLoopDirection.cpp:190:5:190:33 | for(...;...;...) ... | Ill-defined for-loop: a loop using variable "i" counts downward from a value (63), but the terminal condition is higher (255). |
26+
| inconsistentLoopDirection.cpp:192:5:192:34 | for(...;...;...) ... | Ill-defined for-loop: a loop using variable "i" counts downward from a value (... - ...), but the terminal condition is higher (m). |
27+
| inconsistentLoopDirection.cpp:193:5:193:34 | for(...;...;...) ... | Ill-defined for-loop: a loop using variable "i" counts downward from a value (... - ...), but the terminal condition is higher (m). |
28+
| inconsistentLoopDirection.cpp:194:5:194:34 | for(...;...;...) ... | Ill-defined for-loop: a loop using variable "i" counts downward from a value (... - ...), but the terminal condition is higher (m). |
29+
| inconsistentLoopDirection.cpp:196:5:196:32 | for(...;...;...) ... | Ill-defined for-loop: a loop using variable "s" counts downward from a value (63), but the terminal condition is higher (64). |

0 commit comments

Comments
 (0)