C++: Add a test of unsigned count-down loops.

This commit is contained in:
Geoffrey White
2020-08-06 18:28:41 +01:00
parent 205dd1aead
commit a7564c9e0e
2 changed files with 24 additions and 0 deletions

View File

@@ -177,4 +177,21 @@ void FalseNegativeTestCases()
for (int i = 100; i > 0; i += 2) {}
// For comparison
for (int i = 100; i > 0; i ++ ) {} // BUG
}
void IntendedOverflow()
{
const unsigned char m = 10;
unsigned char i;
signed char s;
for (i = 63; i < 64; i--) {} // GOOD (legitimate way to count down with an unsigned) [FALSE POSITIVE]
for (i = 63; i < 128; i--) {} // DUBIOUS (could still be a typo?)
for (i = 63; i < 255; i--) {} // GOOD [FALSE POSITIVE]
for (i = m - 1; i < m; i--) {} // GOOD [FALSE POSITIVE]
for (i = m - 1; i < m; i--) {} // DUBIOUS
for (i = m - 1; i < m; i--) {} // GOOD [FALSE POSITIVE]
for (s = 63; s < 64; s--) {} // BAD (signed numbers don't wrap at 0 / at all)
}

View File

@@ -20,3 +20,10 @@
| 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). |
| 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). |
| 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). |
| 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). |
| 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). |
| 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). |
| 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). |
| 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). |
| 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). |
| 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). |