|
2 | 2 | "-//Semmle//qhelp//EN" |
3 | 3 | "qhelp.dtd"> |
4 | 4 | <qhelp> |
5 | | - <overview> |
6 | | - <p> |
7 | | - Checking for overflow of integer addition needs to be done with |
8 | | - care, because automatic type promotion can prevent the check |
9 | | - from working as intended, with the same value (<code>true</code> |
10 | | - or <code>false</code>) always being returned. |
11 | | - </p> |
12 | | - </overview> |
13 | | - <recommendation> |
14 | | - <p> |
15 | | - Use an explicit cast to make sure that the result of the addition is |
16 | | - not implicitly converted to a larger type. |
17 | | - </p> |
18 | | - </recommendation> |
19 | | - <example> |
20 | | - <sample src="BadAdditionOverflowCheckExample1.cpp" /> |
21 | | - <p> |
22 | | - On a typical architecture where <code>short</code> is 16 bits |
23 | | - and <code>int</code> is 32 bits, the operands of the addition are |
24 | | - automatically promoted to <code>int</code>, so it cannot overflow |
25 | | - and the result of the comparison is always false. |
26 | | - </p> |
27 | | - <p> |
28 | | - The code below implements the check correctly, by using an |
29 | | - explicit cast to make sure that the result of the addition |
30 | | - is <code>unsigned short</code> (which may overflow, in which case |
31 | | - the comparison would evaluate to <code>true</code>). |
32 | | - </p> |
33 | | - <sample src="BadAdditionOverflowCheckExample2.cpp" /> |
34 | | - </example> |
35 | | - <references> |
36 | | - <li><a href="http://c-faq.com/expr/preservingrules.html">Preserving Rules</a></li> |
37 | | - <li><a href="https://www.securecoding.cert.org/confluence/plugins/servlet/mobile#content/view/20086942">Understand integer conversion rules</a></li> |
38 | | - </references> |
| 5 | + |
| 6 | +<overview> |
| 7 | +<p> |
| 8 | +Checking for overflow of integer addition needs to be done with |
| 9 | +care, because automatic type promotion can prevent the check |
| 10 | +from working as intended, with the same value (<code>true</code> |
| 11 | +or <code>false</code>) always being returned. |
| 12 | +</p> |
| 13 | +</overview> |
| 14 | +<recommendation> |
| 15 | +<p> |
| 16 | +Use an explicit cast to make sure that the result of the addition is |
| 17 | +not implicitly converted to a larger type. |
| 18 | +</p> |
| 19 | +</recommendation> |
| 20 | +<example> |
| 21 | +<sample src="BadAdditionOverflowCheckExample1.cpp" /> |
| 22 | +<p> |
| 23 | +On a typical architecture where <code>short</code> is 16 bits |
| 24 | +and <code>int</code> is 32 bits, the operands of the addition are |
| 25 | +automatically promoted to <code>int</code>, so it cannot overflow |
| 26 | +and the result of the comparison is always false. |
| 27 | +</p> |
| 28 | +<p> |
| 29 | +The code below implements the check correctly, by using an |
| 30 | +explicit cast to make sure that the result of the addition |
| 31 | +is <code>unsigned short</code> (which may overflow, in which case |
| 32 | +the comparison would evaluate to <code>true</code>). |
| 33 | +</p> |
| 34 | +<sample src="BadAdditionOverflowCheckExample2.cpp" /> |
| 35 | +</example> |
| 36 | +<references> |
| 37 | +<li><a href="http://c-faq.com/expr/preservingrules.html">Preserving Rules</a></li> |
| 38 | +<li><a href="https://www.securecoding.cert.org/confluence/plugins/servlet/mobile#content/view/20086942">Understand integer conversion rules</a></li> |
| 39 | +</references> |
39 | 40 | </qhelp> |
0 commit comments