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

Skip to content

Commit afa34b5

Browse files
committed
[CPP-434] Improvements to Qhelp; hashCons-ify query.
1 parent 872054a commit afa34b5

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<overview>
66
<p>
77
Testing for <code>signed</code> integer overflow by adding a
8-
value to a variable and then comparing the result to said variable
8+
value to a variable and then comparing the result to that variable
99
is not defined by the C or C++ standards. The comparison may
1010
produce an unintended result, or may be deleted by the compiler
1111
entirely.
@@ -27,7 +27,7 @@ evaluated using <code>signed values</code> and its value is therefore undefined.
2727
<sample src="SignedOverflowCheck-bad.cpp" />
2828
<p>
2929
In the next example, a value of type <code>signed int</code> is
30-
getting added to a value ot type <code>unsigned int</code>. Because
30+
added to a value of type <code>unsigned int</code>. Because
3131
the types are of the same size, C/C++ promotion rules dictate that
3232
<code>unsigned int</code> is chosen as the overall type of the addition
3333
operation. The entire expression is evaluated using <code>unsigned</code>
@@ -36,7 +36,7 @@ values, which is allowed and defined behavior per the C/C++ standard.
3636
<sample src="SignedOverflowCheck-good.cpp" />
3737
</example>
3838
<references>
39-
<li><a href="http://c-faq.com/expr/preservingrules.html">Preserving Rules</a></li>
40-
<li><a href="https://www.securecoding.cert.org/confluence/plugins/servlet/mobile#content/view/20086942">Understand integer conversion rules</a></li>
39+
<li><a href="http://c-faq.com/expr/preservingrules.html">comp.lang.c FAQ list · Question 3.19 (Preserving rules)</a></li>
40+
<li><a href="https://wiki.sei.cmu.edu/confluence/display/c/INT31-C.+Ensure+that+integer+conversions+do+not+result+in+lost+or+misinterpreted+data">INT31-C. Ensure that integer conversions do not result in lost or misinterpreted data</a></li>
4141
</references>
4242
</qhelp>

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212
*/
1313

1414
import cpp
15+
import semmle.code.cpp.valuenumbering.HashCons
16+
17+
private predicate sameAccess(VariableAccess va1, VariableAccess va2) {
18+
hashCons(va1) = hashCons(va2)
19+
}
1520

1621
from RelationalOperation ro, AddExpr add, VariableAccess va1, VariableAccess va2
1722
where
1823
ro.getAnOperand() = add and
1924
add.getAnOperand() = va1 and
2025
ro.getAnOperand() = va2 and
21-
va1.getTarget() = va2.getTarget() and
22-
(not exists(va1.getQualifier()) or va1.getQualifier() = va2.getQualifier()) and
23-
/*
24-
* if the addition (`add`) has been promoted to a signed type,
25-
* then the other operand (`va2`) must have been likewise promoted and so
26-
* have a signed comparison
27-
*/
28-
29-
add.getExplicitlyConverted().getType().(IntegralType).isSigned()
30-
select ro, "Testing for signed overflow may produce undefined results."
26+
sameAccess(va1, va2) and
27+
add.getExplicitlyConverted().getType().(IntegralType).isSigned() and
28+
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."

0 commit comments

Comments
 (0)