Skip to content

Commit 562f628

Browse files
author
Robert Marsh
committed
C++: rename variables in PointerOverflow examples
1 parent c6d848c commit 562f628

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
bool not_in_range(T *ptr, T *ptr_end, size_t a) {
2-
return ptr + a >= ptr_end || ptr + a < ptr; // BAD
1+
bool not_in_range(T *ptr, T *ptr_end, size_t i) {
2+
return ptr + i >= ptr_end || ptr + i < ptr; // BAD
33
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
bool not_in_range(T *ptr, T *ptr_end, size_t a) {
2-
return a >= ptr_end - ptr; // GOOD
1+
bool not_in_range(T *ptr, T *ptr_end, size_t i) {
2+
return i >= ptr_end - ptr; // GOOD
33
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
<overview>
66
<p>
77
When checking for integer overflow, you may often write tests like
8-
<code>a + b &lt; a</code>. This works fine if <code>a</code> and
9-
<code>b</code> are unsigned integers, since any overflow in the addition
8+
<code>p + i &lt; p</code>. This works fine if <code>p</code> and
9+
<code>i</code> are unsigned integers, since any overflow in the addition
1010
will cause the value to simply "wrap around." However, using this pattern when
11-
<code>a</code> is a pointer is problematic because pointer overflow has
11+
<code>p</code> is a pointer is problematic because pointer overflow has
1212
undefined behavior according to the C and C++ standards. If the addition
1313
overflows and has an undefined result, the comparison will likewise be
1414
undefined; it may produce an unintended result, or may be deleted entirely by an
@@ -18,13 +18,13 @@ optimizing compiler.
1818
</overview>
1919
<recommendation>
2020
<p>
21-
To check whether an index <code>a</code> is less than the length of an array,
22-
simply compare these two numbers as unsigned integers: <code>a &lt; ARRAY_LENGTH</code>.
21+
To check whether an index <code>i</code> is less than the length of an array,
22+
simply compare these two numbers as unsigned integers: <code>i &lt; ARRAY_LENGTH</code>.
2323
If the length of the array is defined as the difference between two pointers
24-
<code>ptr</code> and <code>p_end</code>, write <code>a &lt; p_end - ptr</code>.
25-
If a is <code>signed</code>, cast it to <code>unsigned</code>
26-
in order to guard against negative <code>a</code>. For example, write
27-
<code>(size_t)a &lt; p_end - ptr</code>.
24+
<code>ptr</code> and <code>p_end</code>, write <code>i &lt; p_end - ptr</code>.
25+
If i is <code>signed</code>, cast it to <code>unsigned</code>
26+
in order to guard against negative <code>i</code>. For example, write
27+
<code>(size_t)i &lt; p_end - ptr</code>.
2828
</p>
2929
</recommendation>
3030
<example>
@@ -41,14 +41,14 @@ overflows and wraps around.
4141
<p>
4242
In both of these checks, the operations are performed in the wrong order.
4343
First, an expression that may cause undefined behavior is evaluated
44-
(<code>ptr + a</code>), and then the result is checked for being in range.
44+
(<code>ptr + i</code>), and then the result is checked for being in range.
4545
But once undefined behavior has happened in the pointer addition, it cannot
4646
be recovered from: it's too late to perform the range check after a possible
4747
pointer overflow.
4848
</p>
4949

5050
<p>
51-
While it's not the subject of this query, the expression <code>ptr + a &lt;
51+
While it's not the subject of this query, the expression <code>ptr + i &lt;
5252
ptr_end</code> is also an invalid range check. It's undefined behavor in
5353
C/C++ to create a pointer that points more than one past the end of an
5454
allocation.

0 commit comments

Comments
 (0)