Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bd87d7b

Browse files
committed
[CPP-434] Switch to global value numbering (GVN). Improve qlhelp doc.
1 parent afa34b5 commit bd87d7b

4 files changed

Lines changed: 18 additions & 7 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bool bar(unsigned short n1, unsigned short delta) {
2+
return n1 + delta < n1; // BAD
3+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
bool bar(int n1, unsigned int delta) {
1+
bool baz(int n1, unsigned int delta) {
22
return n1 + delta < n1; // GOOD
33
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,19 @@ In the following example, even though <code>delta</code> has been declared
2222
<code>unsigned short</code>, C/C++ type promotion rules require that its
2323
type is promoted to the larger type used in the addition and comparison,
2424
namely a <code>signed int</code>. As a result, the entire expression is
25-
evaluated using <code>signed values</code> and its value is therefore undefined.
25+
evaluated using <code>signed</code> values and its value is therefore undefined.
2626
</p>
2727
<sample src="SignedOverflowCheck-bad.cpp" />
2828
<p>
29+
In the following example, even though both <code>n</code> and <code>delta</code>
30+
have been declared <code>unsigned short</code>, C/C++ type promotion rules
31+
require that both parameters be promoted to the next bigger <code>signed</code>
32+
integer type (in this case <code>signed int</code>) before being added together
33+
so as to avoid overflows or underflows. As a result, the entire expression is
34+
evaluated using <code>signed</code> values and its value is therefore undefined.
35+
</p>
36+
<sample src="SignedOverflowCheck-bad2.cpp" />
37+
<p>
2938
In the next example, a value of type <code>signed int</code> is
3039
added to a value of type <code>unsigned int</code>. Because
3140
the types are of the same size, C/C++ promotion rules dictate that

cpp/ql/src/Likely Bugs/Arithmetic/SignedOverflowCheck.ql

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
* `unsigned` integer values.
66
* @kind problem
77
* @problem.severity warning
8-
* @precision medium
8+
* @precision high
99
* @id cpp/signed-overflow-check
1010
* @tags reliability
1111
* security
1212
*/
1313

1414
import cpp
15-
import semmle.code.cpp.valuenumbering.HashCons
15+
import semmle.code.cpp.valuenumbering.GlobalValueNumbering
1616

1717
private predicate sameAccess(VariableAccess va1, VariableAccess va2) {
18-
hashCons(va1) = hashCons(va2)
18+
globalValueNumber(va1) = globalValueNumber(va2)
1919
}
2020

2121
from RelationalOperation ro, AddExpr add, VariableAccess va1, VariableAccess va2
@@ -26,5 +26,4 @@ where
2626
sameAccess(va1, va2) and
2727
add.getExplicitlyConverted().getType().(IntegralType).isSigned() and
2828
va2.getExplicitlyConverted().getType().(IntegralType).isSigned()
29-
select va1, va1.getQualifier().getAQlClass(), va2, va2.getQualifier().getAQlClass(), ro,
30-
"Testing for signed overflow may produce undefined results."
29+
select ro, "Testing for signed overflow may produce undefined results."

0 commit comments

Comments
 (0)