Skip to content

Commit 4b27b2a

Browse files
authored
Merge pull request #2173 from zlaski-semmle/zlaski/bad-addition-qhelp-reword
Reword and reformat Qhelp for BadAdditionOverflowCheck query
2 parents fc8c1e1 + ac7a123 commit 4b27b2a

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

cpp/ql/src/Likely Bugs/Arithmetic/BadAdditionOverflowCheck.qhelp

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,39 @@
22
"-//Semmle//qhelp//EN"
33
"qhelp.dtd">
44
<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 correctly.
10-
</p>
11-
</overview>
12-
<recommendation>
13-
<p>
14-
Use an explicit cast to make sure that the result of the addition is
15-
not implicitly converted to a larger type.
16-
</p>
17-
</recommendation>
18-
<example>
19-
<sample src="BadAdditionOverflowCheckExample1.cpp" />
20-
<p>
21-
On a typical architecture where <tt>short</tt> is 16 bits
22-
and <tt>int</tt> is 32 bits, the operands of the addition are
23-
automatically promoted to <tt>int</tt>, so it cannot overflow
24-
and the result of the comparison is always false.
25-
</p>
26-
<p>
27-
The code below implements the check correctly, by using an
28-
explicit cast to make sure that the result of the addition
29-
is <tt>unsigned short</tt>.
30-
</p>
31-
<sample src="BadAdditionOverflowCheckExample2.cpp" />
32-
</example>
33-
<references>
34-
<li><a href="http://c-faq.com/expr/preservingrules.html">Preserving Rules</a></li>
35-
<li><a href="https://www.securecoding.cert.org/confluence/plugins/servlet/mobile#content/view/20086942">Understand integer conversion rules</a></li>
36-
</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>
3740
</qhelp>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
bool checkOverflow(unsigned short x, unsigned short y) {
2-
return (x + y < x); // BAD: x and y are automatically promoted to int.
2+
// BAD: comparison is always false due to type promotion
3+
return (x + y < x);
34
}

0 commit comments

Comments
 (0)