Skip to content

Commit 1343e4c

Browse files
committed
C++: Add another 'good' example for cpp/unsigned-difference-expression-compared-zero.
1 parent 7abece4 commit 1343e4c

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
unsigned limit = get_limit();
2-
unsigned total = 0;
1+
uint32_t limit = get_limit();
2+
uint32_t total = 0;
33

44
while (limit - total > 0) { // BAD: if `total` is greater than `limit` this will underflow and continue executing the loop.
55
total += get_data();
66
}
77

8-
while (total < limit) { // GOOD: never underflows.
8+
while (total < limit) { // GOOD: never underflows here because there is no arithmetic.
9+
total += get_data();
10+
}
11+
12+
while ((int64_t)limit - total > 0) { // GOOD: never underflows here because the result always fits in an `int64_t`.
913
total += get_data();
1014
}

0 commit comments

Comments
 (0)