Skip to content

Commit 8f152b7

Browse files
authored
Merge pull request github#5877 from MathiasVP/detect-more-abs-in-overflow-library
C++: Detect more uses of `abs`
2 parents fc121e1 + 948f1d8 commit 8f152b7

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm
2+
* The `cpp/tainted-arithmetic`, `cpp/arithmetic-with-extreme-values`, and `cpp/uncontrolled-arithmetic` queries now recognize more functions as returning the absolute value of their input. As a result, they produce fewer false positives.

cpp/ql/src/semmle/code/cpp/security/Overflow.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import semmle.code.cpp.rangeanalysis.RangeAnalysisUtils
1212
* Holds if the value of `use` is guarded using `abs`.
1313
*/
1414
predicate guardedAbs(Operation e, Expr use) {
15-
exists(FunctionCall fc | fc.getTarget().getName() = "abs" |
15+
exists(FunctionCall fc | fc.getTarget().getName() = ["abs", "labs", "llabs", "imaxabs"] |
1616
fc.getArgument(0).getAChild*() = use and
1717
guardedLesser(e, fc)
1818
)

cpp/ql/test/query-tests/Security/CWE/CWE-190/semmle/tainted/test5.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,25 @@ void useTaintedInt()
1818
y = getTaintedInt();
1919
y = y * 1024; // BAD: arithmetic on a tainted value
2020
}
21+
22+
typedef long long int intmax_t;
23+
24+
intmax_t imaxabs(intmax_t j);
25+
26+
void useTaintedIntWithGuard() {
27+
int tainted = getTaintedInt();
28+
29+
if(imaxabs(tainted) <= 100) {
30+
int product = tainted * tainted; // GOOD: can't underflow/overflow
31+
}
32+
}
33+
34+
#define INTMAX_MIN (-0x7fffffffffffffff - 1)
35+
36+
void useTaintedIntWithGuardIntMaxMin() {
37+
intmax_t tainted = getTaintedInt();
38+
39+
if(imaxabs(tainted) <= INTMAX_MIN) {
40+
int product = tainted * tainted; // BAD: imaxabs(INTMAX_MIN) == INTMAX_MIN [NOT DETECTED]
41+
}
42+
}

0 commit comments

Comments
 (0)