File tree Expand file tree Collapse file tree
cpp/ql/src/Likely Bugs/Arithmetic Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -18,29 +18,39 @@ optimizing compiler.
1818<recommendation >
1919<p >
2020Solutions to this problem can be thought of as falling into one of two
21- categories: (1) rewrite the signed expression so that overflow cannot occur
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.
21+ categories:
2422</p >
2523
24+ <ol >
25+ <li >Rewrite the signed expression so that overflow cannot occur
26+ but the signedness remains.</li >
27+ <li >Change the variables and all their uses to be unsigned.</li >
28+ </ol >
29+
2630<p >
31+ The following cases all fall into the first category.
32+ </p >
33+
34+ <ol >
35+ <li >
2736Given <code >unsigned short n1, delta</code > and <code >n1 + delta < n1</code >,
2837it is possible to rewrite it as <code >(unsigned short)(n1 + delta) < n1</code >.
2938Note that <code >n1 + delta</code > does not actually overflow, due to <code >int</code > promotion.
30- </p >
39+ </li >
3140
32- <p >
41+ <li >
3342Given <code >unsigned short n1, delta</code > and <code >n1 + delta < n1</code >,
3443it is also possible to rewrite it as <code >n1 > USHORT_MAX - delta</code >. The
3544<code >limits.h</code > or <code >climits</code > header must then be included.
36- </p >
45+ </li >
3746
38- <p >
47+ <li >
3948Given <code >int n1, delta</code > and <code >n1 + delta < n1</code >,
4049it is possible to rewrite it as <code >n1 > INT_MAX - delta</code >. It must be true
4150that <code >delta > = 0</code > and the <code >limits.h</code > or <code >climits</code >
4251header has been included.
43- </p >
52+ </li >
53+ </ol >
4454</recommendation >
4555
4656<example >
You can’t perform that action at this time.
0 commit comments