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

Skip to content

Commit 33cd6de

Browse files
committed
[CPP-434] Improved query and test suite.
1 parent 1ab9657 commit 33cd6de

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@
1414
import cpp
1515
import semmle.code.cpp.valuenumbering.GlobalValueNumbering
1616

17+
private predicate isSignedWithoutUnsignedCast(Expr e) {
18+
e.getType().getUnspecifiedType().(IntegralType).isSigned() and
19+
not e.getExplicitlyConverted().getType().getUnspecifiedType().(IntegralType).isUnsigned()
20+
}
21+
1722
from RelationalOperation ro, AddExpr add, VariableAccess va1, VariableAccess va2
1823
where
1924
ro.getAnOperand() = add and
2025
add.getAnOperand() = va1 and
2126
ro.getAnOperand() = va2 and
2227
globalValueNumber(va1) = globalValueNumber(va2) and
23-
add.getFullyConverted().getType().getUnspecifiedType().(IntegralType).isSigned() and
24-
not add.getExplicitlyConverted().getType().getUnspecifiedType().(IntegralType).isUnsigned()
28+
isSignedWithoutUnsignedCast(add) and
29+
isSignedWithoutUnsignedCast(va2)
2530
select ro, "Testing for signed overflow may produce undefined results."

cpp/ql/test/query-tests/Likely Bugs/Arithmetic/SignedOverflowCheck/SignedOverflowCheck.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,22 @@ bool func1(se *so) {
8383
bool checkOverflow3(unsigned int a, unsigned short b) {
8484
return (a + b < a); // GOOD
8585
}
86+
87+
struct C {
88+
unsigned int length;
89+
};
90+
91+
int checkOverflow4(unsigned int ioff, C c) {
92+
// not deleted by gcc or clang
93+
if ((int)(ioff + c.length) < (int)ioff) return 0; // GOOD
94+
return 1;
95+
}
96+
97+
#define AV_INPUT_BUFFER_PADDING_SIZE 64
98+
99+
int overflow12(int codecdata_length) {
100+
if(codecdata_length + AV_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length) { // GOOD
101+
return -1;
102+
}
103+
return 1;
104+
}

0 commit comments

Comments
 (0)