@@ -21,7 +21,9 @@ Solutions to this problem can be thought of as falling into one of two
2121categories: (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.
24+ </p >
2425
26+ <p >
2527Below we list examples of expressions where signed overflow may
2628occur, along with proposed solutions. The list should not be
2729considered exhaustive.
@@ -31,21 +33,29 @@ considered exhaustive.
3133Given <code >unsigned short i, delta</code > and <code >i + delta < i</code >,
3234it is possible to rewrite it as <code >(unsigned short)(i + delta) < i</code >.
3335Note that <code >i + delta</code >does not actually overflow, due to <code >int</code > promotion
36+ </p >
3437
38+ <p >
3539Given <code >unsigned short i, delta</code > and <code >i + delta < i</code >,
3640it is also possible to rewrite it as <code >USHORT_MAX - delta</code >. It must be true
3741that <code >delta > 0</code > and the <code >limits.h</code > or <code >climits</code >
3842header has been included.
43+ </p >
3944
45+ <p >
4046Given <code >int i, delta</code > and <code >i + delta < i</code >,
4147it is possible to rewrite it as <code >INT_MAX - delta</code >. It must be true
4248that <code >delta > 0</code > and the <code >limits.h</code > or <code >climits</code >
4349header has been included.
50+ </p >
4451
52+ <p >
4553Given <code >int i, delta</code > and <code >i + delta < i</code >,
4654it is also possible to rewrite it as <code >(unsigned)i + delta < i</code >.
4755Note that program semantics are affected by this change.
56+ </p >
4857
58+ <p >
4959Given <code >int i, delta</code > and <code >i + delta < i</code >,
5060it is also possible to rewrite it as <code >unsigned int i, delta</code > and
5161<code >i + delta < i</code >. Note that program semantics are
0 commit comments