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

Skip to content

Commit 1f82ea7

Browse files
committed
[zlaski/pointer-overflow-check] Refine query to exclude macros (other than 'assert').
1 parent 0df3d2c commit 1f82ea7

3 files changed

Lines changed: 22 additions & 12 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
bool check_pointer_overflow(P *ptr, P *ptr_end) {
2-
return ptr + 4 >= ptr_end; // GOOD
2+
return ptr_end - ptr >= 4; // GOOD
33
}

cpp/ql/src/Likely Bugs/Memory Management/PointerOverflowCheck.qhelp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<qhelp>
55
<overview>
66
<p>
7-
When checking for out-of-range pointer values, one might write tests like
7+
When checking for out-of-range pointer values, we might write tests like
88
<code>p + a &lt; p</code> and check if the value "wraps around".
99
Such a test is wrong in that it relies on the overflow of <code>p + a</code>,
1010
which has undefined behavior. In fact, many optimizing compilers will remove
@@ -18,13 +18,12 @@ the operating system) or nonexistent.
1818
<recommendation>
1919
<p>
2020
When checking for an out-of-range pointer, compare the pointer
21-
value <code>p</code> against a known value <code>p_max</code> representing
22-
the highest allowable memory address. Ideally, <code>p_max</code> should
23-
point just past the end of a data structure, such as an array or a vector.
24-
For lower-level work, it should point at the highest address in the program
25-
heap. It is also important that <code>p + a</code> points at a valid object
26-
and that <code>a</code> is small enough so that the expression <code>p + a</code>
27-
does not itself overflow.
21+
value <code>p</code> against a known pointer value <code>p_max</code> representing
22+
the highest allowable memory address. The <code>p_max</code> pointer should
23+
point inside or just past the end of a defined memory region, such that
24+
<code>p + a &lt;= p_max</code>. Since we do not wish to evaluate <code>p + a</code>
25+
(which may result in undefined behavior), we rewrite the inequality as
26+
<code>p_max - p &gt; a</code>.
2827
</p>
2928
</recommendation>
3029
<example>

cpp/ql/src/Likely Bugs/Memory Management/PointerOverflowCheck.ql

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @description Testing for out-of-range pointers by adding a value to a pointer
44
* to see if it "wraps around" is dangerous because it relies
55
* on undefined behavior and may lead to attempted use of
6-
* nonexistent or inaccessible memory locations
6+
* nonexistent or inaccessible memory locations.
77
* @kind problem
88
* @problem.severity warning
99
* @precision high
@@ -14,12 +14,23 @@
1414

1515
import cpp
1616
private import semmle.code.cpp.valuenumbering.GlobalValueNumbering
17-
private import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
17+
18+
class AssertMacro extends Macro {
19+
AssertMacro() {
20+
getName() = "assert" or
21+
getName() = "_ASSERT" or
22+
getName() = "_ASSERTE" or
23+
getName() = "_ASSERT_EXPR"
24+
}
25+
}
1826

1927
from RelationalOperation ro, PointerAddExpr add, Expr expr1, Expr expr2
2028
where
2129
ro.getAnOperand() = add and
2230
add.getAnOperand() = expr1 and
2331
ro.getAnOperand() = expr2 and
24-
globalValueNumber(expr1) = globalValueNumber(expr2)
32+
globalValueNumber(expr1) = globalValueNumber(expr2) and
33+
not exists(MacroInvocation mi |
34+
mi.getAnAffectedElement() = add and not mi.getMacro() instanceof AssertMacro
35+
)
2536
select ro, "Pointer out-of-range check relying on pointer overflow is undefined."

0 commit comments

Comments
 (0)