...
Code Block | ||
---|---|---|
| ||
long i = /* some expression that evaluates to the value 32767 */; /* ... */ /* No test is necessary; i is known not to overflow. */ /* expression involving i + 1 */ |
Non-Compliant Code Example
Increasingly optimization techniques are being adopted that ignore the possibility of integer overflow to produce faster code. For example, in gcc versions 4.2 and later, code that performs length checks similar to the following:
Code Block | ||
---|---|---|
| ||
char *buf;
int len = 1<<30;
/* ... */
if (buf+len < buf) { /* length check */
/* perform some manipulation on len */
}
|
Wiki Markup |
---|
are optimized away; no object code to perform the check will appear in the resulting executable program \[[VU#162289|AA. C References#VU#162289]\]. |
Risk Assessment
Out of range integer values can result in fetches or stores from arbitrary memory locations and the execution of arbitrary code.
...
Wiki Markup |
---|
\[[Lions 96|AA. C References#Lions 96]\]
\[[VU#162289|AA. C References#VU#162289]\] |
...
INT07-A. Use only explicitly signed or unsigned char type for numeric values 04. Integers (INT) INT09-A. Ensure enumeration constants map to unique values