@@ -22,85 +22,36 @@ categories: (1) rewrite the signed expression so that overflow cannot occur
2222but the signedness remains, or (2) rewrite (or cast) the signed expression
2323into unsigned form.
2424
25- The table below lists various expressions where signed overflow may
25+ The bullet list below lists various expressions where signed overflow may
2626occur, along with proposed rewritings. It should not be
2727considered exhaustive.
2828</p >
29- <table >
30- <thead ><tr >
31- <th >Original Construct</th >
32- <th >Alternate Construct(s)</th >
33- <th >Notes</th >
34- </tr ></thead >
35- <tbody ><tr >
36- <td ><tt ><table >
37- <tbody ><tr >
38- <td >unsigned short i, delta;</td >
39- </tr ><tr >
40- <td >i + delta < i</td >
41- </tr ></tbody >
42- </table ></tt ></td >
43- <td ><tt ><table >
44- <tbody ><tr >
45- <td >unsigned short i, delta;</td >
46- </tr ><tr >
47- <td >(unsigned short)(i + delta) < i</td >
48- </tr ></tbody >
49- </table ></tt ></td >
50- <td ><tt >i + delta</tt >does not actually overflow due to <tt >int</tt > promotion</td >
51- </tr >
52- <tr >
53- <td > </td >
54- <td ><tt ><table >
55- <tbody ><tr >
56- <td >unsigned short i, delta;</td >
57- </tr ><tr >
58- <td >i > USHORT_MAX - delta</td >
59- </tr ></tbody >
60- </table ></tt ></td >
61- <td >Must include <tt >limits.h</tt > or <tt >climits</tt >; <tt >delta > 0</tt ></td >
62- </tr >
63- <tr >
64- <td ><tt ><table >
65- <tbody ><tr >
66- <td >int i, delta;</td >
67- </tr ><tr >
68- <td >i + delta < i</td >
69- </tr ></tbody >
70- </table ></tt ></td >
71- <td ><tt ><table >
72- <tbody ><tr >
73- <td >int i, delta;</td >
74- </tr ><tr >
75- <td >i > INT_MAX - delta</td >
76- </tr ></tbody >
77- </table ></tt ></td >
78- <td >Must include <tt >limits.h</tt > or <tt >climits</tt >; <tt >delta > 0</tt ></td >
79- </tr >
80- <tr >
81- <td > </td >
82- <td ><tt ><table >
83- <tbody ><tr >
84- <td >int i, delta;</td >
85- </tr ><tr >
86- <td >(unsigned)i + delta < i</td >
87- </tr ></tbody >
88- </table ></tt ></td >
89- <td >Change in program semantics</td >
90- </tr >
91- <tr >
92- <td > </td >
93- <td ><tt ><table >
94- <tbody ><tr >
95- <td >unsigned int i, delta;</td >
96- </tr ><tr >
97- <td >i + delta < i</td >
98- </tr ></tbody >
99- </table ></tt ></td >
100- <td >Change in program semantics</td >
101- </tr ></tbody >
102- </table >
29+
30+ <li >Given <code >unsigned short i, delta</code > and <code >i + delta < i</code >,
31+ it is possible to rewrite it as <code >(unsigned short)(i + delta) < i</code >.
32+ Note that <code >i + delta</code >does not actually overflow, due to <code >int</code > promotion</li >
33+
34+ <li >Given <code >unsigned short i, delta</code > and <code >i + delta < i</code >,
35+ it is also possible to rewrite it as <code >USHORT_MAX - delta</code >. It must be true
36+ that <code >delta > 0</code > and the <code >limits.h</code > or <code >climits</code >
37+ header has been included.</li >
38+
39+ <li >Given <code >int i, delta</code > and <code >i + delta < i</code >,
40+ it is possible to rewrite it as <code >INT_MAX - delta</code >. It must be true
41+ that <code >delta > 0</code > and the <code >limits.h</code > or <code >climits</code >
42+ header has been included.</li >
43+
44+ <li >Given <code >int i, delta</code > and <code >i + delta < i</code >,
45+ it is also possible to rewrite it as <code >(unsigned)i + delta < i</code >.
46+ Note that program semantics are affected by this change.</li >
47+
48+ <li >Given <code >int i, delta</code > and <code >i + delta < i</code >,
49+ it is also possible to rewrite it as <code >unsigned int i, delta</code > and
50+ <code >i + delta < i</code >. Note that program semantics are
51+ affected by this change.</li >
52+
10353</recommendation >
54+
10455<example >
10556<p >
10657In the following example, even though <code >delta</code > has been declared
@@ -142,6 +93,7 @@ so that <code>unsigned short</code> "wrap around" may now be observed.
14293Furthermore, since the left-hand side is now of type <code >unsigned short</code >,
14394the right-hand side does not need to be promoted to a <code >signed int</code >.
14495</p >
96+
14597<sample src =" SignedOverflowCheck-good2.cpp" />
14698</example >
14799<references >
0 commit comments