|
| 1 | +<!DOCTYPE qhelp PUBLIC |
| 2 | + "-//Semmle//qhelp//EN" |
| 3 | + "qhelp.dtd"> |
| 4 | +<qhelp> |
| 5 | +<overview> |
| 6 | +<p> |
| 7 | +The expression <code>ptr + a < ptr</code> is equivalent to <code>a < |
| 8 | +0</code>, and an optimizing compiler is likely to make that replacement, |
| 9 | +thereby removing a range check that might have been necessary for security. |
| 10 | +If <code>a</code> is known to be non-negative, the compiler can even replace <code>ptr + |
| 11 | +a < ptr</code> with <code>false</code>. |
| 12 | +</p> |
| 13 | + |
| 14 | +<p> |
| 15 | +The reason is that pointer arithmetic overflow in C/C++ is undefined |
| 16 | +behavior. The optimizing compiler can assume that the program has no |
| 17 | +undefined behavior, which means that adding a positive number to <code>ptr</code> cannot |
| 18 | +produce a pointer less than <code>ptr</code>. |
| 19 | +</p> |
| 20 | +</overview> |
| 21 | +<recommendation> |
| 22 | +<p> |
| 23 | +To check whether an index <code>a</code> is less than the length of an array, |
| 24 | +simply compare these two numbers as unsigned integers: <code>a < ARRAY_LENGTH</code>. |
| 25 | +If the length of the array is defined as the difference between two pointers |
| 26 | +<code>ptr</code> and <code>p_end</code>, write <code>a < p_end - ptr</code>. |
| 27 | +If a is <code>signed</code>, cast it to <code>unsigned</code> |
| 28 | +in order to guard against negative <code>a</code>. For example, write |
| 29 | +<code>(size_t)a < p_end - ptr</code>. |
| 30 | +</p> |
| 31 | +</recommendation> |
| 32 | +<example> |
| 33 | +<p> |
| 34 | +An invalid check for pointer overflow is most often seen as part of checking |
| 35 | +whether a number <code>a</code> is too large by checking first if adding the |
| 36 | +number to <code>ptr</code> goes past the end of an allocation and then |
| 37 | +checking if adding it to <code>ptr</code> creates a pointer so large that it |
| 38 | +overflows and wraps around. |
| 39 | +</p> |
| 40 | + |
| 41 | +<sample src="PointerOverflow-bad.cpp" /> |
| 42 | + |
| 43 | +<p> |
| 44 | +In both of these checks, the operations are performed in the wrong order. |
| 45 | +First, an expression that may cause undefined behavior is evaluated |
| 46 | +(<code>ptr + a</code>), and then the result is checked for being in range. |
| 47 | +But once undefined behavior has happened in the pointer addition, it cannot |
| 48 | +be recovered from: it's too late to perform the range check after a possible |
| 49 | +pointer overflow. |
| 50 | +</p> |
| 51 | + |
| 52 | +<p> |
| 53 | +While it's not the subject of this query, the expression <code>ptr + a < |
| 54 | +ptr_end</code> is also an invalid range check. It's undefined behavor in |
| 55 | +C/C++ to create a pointer that points more than one past the end of an |
| 56 | +allocation. |
| 57 | +</p> |
| 58 | + |
| 59 | +<p> |
| 60 | +The next example shows how to portably check whether an unsigned number is outside the |
| 61 | +range of an allocation between <code>ptr</code> and <code>ptr_end</code>. |
| 62 | +</p> |
| 63 | +<sample src="PointerOverflow-good.cpp" /> |
| 64 | +</example> |
| 65 | +<references> |
| 66 | +<li>Embedded in Academia: <a href="https://blog.regehr.org/archives/1395">Pointer Overflow Checking</a>.</li> |
| 67 | +<li>LWN: <a href="https://lwn.net/Articles/278137/">GCC and pointer overflows</a>.</li> |
| 68 | +</references> |
| 69 | +</qhelp> |
0 commit comments