...
Wiki Markup |
---|
The C99 standard \[[ISO/IEC 9899:1999|AA. References#ISO/IEC 9899-1999]\] identifies four distinct situations in which undefined behavior (UB) may arise as a result of invalid pointer operations: |
UB | Description | Example Code | |||
---|---|---|---|---|---|
Addition or subtraction of a pointer into, or just beyond, an array object and an integer type produces a result that does not point into, or just beyond, the same array object. | |||||
Addition or subtraction of a pointer into, or just beyond, an array object and an integer type produces a result that points just beyond the array object and is used as the operand of a unary | |||||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="cece2b2c135992fe-f2572f49-4c724fc6-b09799f1-b994b9c3f67e24ea49436a49"><ac:plain-text-body><![CDATA[ | [46 | CC. Undefined Behavior#ub_46] | An array subscript is out of range, even if an object is apparently accessible with the given subscript (as in the lvalue expression | [#Apparently Accessible Out Of Range Index] | ]]></ac:plain-text-body></ac:structured-macro> |
An attempt is made to access, or generate a pointer to just past, a flexible array member of a structure when the referenced object provides no elements for that array. |
Anchor | ||||
---|---|---|---|---|
|
Noncompliant Code Example (Forming Out Of Bounds Pointer)
...
Code Block | ||
---|---|---|
| ||
enum { TABLESIZE = 100 }; static int table[TABLESIZE]; int* f(size_t index) { if (index < TABLESIZE) return table + index; return NULL; } |
Anchor | ||||
---|---|---|---|---|
|
Noncompliant Code Example (Dereferencing Out Of Bounds Pointer)
...
This compliant solution is for illustrative purposes and is not necessarily the solution implemented by Microsoft. This particular "solution" may not be correct, because there is no guarantee that a L'
is found.
'
Anchor | ||||
---|---|---|---|---|
|
Noncompliant Code Example (Apparently Accessible Out Of Range Index)
...
Code Block | ||
---|---|---|
| ||
static const size_t COLS = 5; static const size_t ROWS = 7; static int matrix[ROWS][COLS]; void init_matrix(int x) { for (size_t i = 0; i != ROWS; ++i) for (size_t j = 0; j != COLS; ++j) matrix[i][j] = x; } |
Anchor | ||||
---|---|---|---|---|
|
Noncompliant Code Example (Pointer Past Flexible Array Member)
...