Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7485cc7

Browse files
committed
C++: Edit Recommendation section
1. The two last examples were misleading at best. The first of those two recommended casting to non-negative `int`s to `unsigned int` and then checking if their addition would overflow, but overflow was impossible because their sum (on 32-bit two's complement) could be at most 2^32 - 2. The second example could lead to the wrong condition (unsigned overflow) being checked if taken literally. Instead of keeping that example, I reworeded the first paragraph of the Recommendation section. 2. The assumptions about `delta` being positive was relaxed to non-negative. 3. There was no need to assume that an unsigned short was non-negative. 4. Some of the suggestions were missing `i >`.
1 parent 73d9cc2 commit 7485cc7

1 file changed

Lines changed: 7 additions & 27 deletions

File tree

cpp/ql/src/Likely Bugs/Arithmetic/SignedOverflowCheck.qhelp

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,28 @@ optimizing compiler.
1919
<p>
2020
Solutions to this problem can be thought of as falling into one of two
2121
categories: (1) rewrite the signed expression so that overflow cannot occur
22-
but the signedness remains, or (2) rewrite (or cast) the signed expression
23-
into unsigned form.
24-
</p>
25-
26-
<p>
27-
Below we list examples of expressions where signed overflow may
28-
occur, along with proposed solutions. The list should not be
29-
considered exhaustive.
22+
but the signedness remains, or (2) change the variables and all their uses to
23+
be unsigned. The following cases all fall into the first category.
3024
</p>
3125

3226
<p>
3327
Given <code>unsigned short i, delta</code> and <code>i + delta &lt; i</code>,
3428
it is possible to rewrite it as <code>(unsigned short)(i + delta)&nbsp;&lt;&nbsp;i</code>.
35-
Note that <code>i + delta</code>does not actually overflow, due to <code>int</code> promotion
29+
Note that <code>i + delta</code> does not actually overflow, due to <code>int</code> promotion.
3630
</p>
3731

3832
<p>
3933
Given <code>unsigned short i, delta</code> and <code>i + delta &lt; i</code>,
40-
it is also possible to rewrite it as <code>USHORT_MAX - delta</code>. It must be true
41-
that <code>delta &gt; 0</code> and the <code>limits.h</code> or <code>climits</code>
42-
header has been included.
34+
it is also possible to rewrite it as <code>i &gt; USHORT_MAX - delta</code>. The
35+
<code>limits.h</code> or <code>climits</code> header must then be included.
4336
</p>
4437

4538
<p>
4639
Given <code>int i, delta</code> and <code>i + delta &lt; i</code>,
47-
it is possible to rewrite it as <code>INT_MAX - delta</code>. It must be true
48-
that <code>delta &gt; 0</code> and the <code>limits.h</code> or <code>climits</code>
40+
it is possible to rewrite it as <code>i &gt; INT_MAX - delta</code>. It must be true
41+
that <code>delta &gt;= 0</code> and the <code>limits.h</code> or <code>climits</code>
4942
header has been included.
5043
</p>
51-
52-
<p>
53-
Given <code>int i, delta</code> and <code>i + delta &lt; i</code>,
54-
it is also possible to rewrite it as <code>(unsigned)i + delta &lt; i</code>.
55-
Note that program semantics are affected by this change.
56-
</p>
57-
58-
<p>
59-
Given <code>int i, delta</code> and <code>i + delta &lt; i</code>,
60-
it is also possible to rewrite it as <code>unsigned int i, delta</code> and
61-
<code>i + delta &lt; i</code>. Note that program semantics are
62-
affected by this change.
63-
</p>
6444
</recommendation>
6545

6646
<example>

0 commit comments

Comments
 (0)