diff --git a/cpp/ql/src/Likely Bugs/Arithmetic/SignedOverflowCheck.qhelp b/cpp/ql/src/Likely Bugs/Arithmetic/SignedOverflowCheck.qhelp index 302fbd4ed23..a6f8f4dc373 100644 --- a/cpp/ql/src/Likely Bugs/Arithmetic/SignedOverflowCheck.qhelp +++ b/cpp/ql/src/Likely Bugs/Arithmetic/SignedOverflowCheck.qhelp @@ -22,85 +22,36 @@ categories: (1) rewrite the signed expression so that overflow cannot occur but the signedness remains, or (2) rewrite (or cast) the signed expression into unsigned form. -The table below lists various expressions where signed overflow may +The bullet list below lists various expressions where signed overflow may occur, along with proposed rewritings. It should not be considered exhaustive.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Original ConstructAlternate Construct(s)Notes
- - - - - -
unsigned short i, delta;
i + delta < i
- - - - - -
unsigned short i, delta;
(unsigned short)(i + delta) < i
i + deltadoes not actually overflow due to int promotion
  - - - - - -
unsigned short i, delta;
i > USHORT_MAX - delta
Must include limits.h or climits; delta > 0
- - - - - -
int i, delta;
i + delta < i
- - - - - -
int i, delta;
i > INT_MAX - delta
Must include limits.h or climits; delta > 0
  - - - - - -
int i, delta;
(unsigned)i + delta < i
Change in program semantics
  - - - - - -
unsigned int i, delta;
i + delta < i
Change in program semantics
+ +
  • Given unsigned short i, delta and i + delta < i, +it is possible to rewrite it as (unsigned short)(i + delta) < i. +Note that i + deltadoes not actually overflow, due to int promotion
  • + +
  • Given unsigned short i, delta and i + delta < i, +it is also possible to rewrite it as USHORT_MAX - delta. It must be true +that delta > 0 and the limits.h or climits +header has been included.
  • + +
  • Given int i, delta and i + delta < i, +it is possible to rewrite it as INT_MAX - delta. It must be true +that delta > 0 and the limits.h or climits +header has been included.
  • + +
  • Given int i, delta and i + delta < i, +it is also possible to rewrite it as (unsigned)i + delta < i. +Note that program semantics are affected by this change.
  • + +
  • Given int i, delta and i + delta < i, +it is also possible to rewrite it as unsigned int i, delta and +i + delta < i. Note that program semantics are +affected by this change.
  • + +

    In the following example, even though delta has been declared @@ -142,6 +93,7 @@ so that unsigned short "wrap around" may now be observed. Furthermore, since the left-hand side is now of type unsigned short, the right-hand side does not need to be promoted to a signed int.

    +