While it has been common practice to use integers and pointers interchangeably in C, pointer to integer and integer to pointer conversions are implementation-defined.
The only value that can be considered interchangeable between pointers and integers is the constant 0. Except in this case, conversions between integers and pointers may have undesired consequences depending on the implementation. According to C99 [[ISO/IEC 9899-1999]]:
An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.
Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
These issues arise because the mapping functions for converting a pointer to an integer or an integer to a pointer must be consistent with the addressing structure of the execution environment. For example, not all machines have a flat memory model.
It is sometimes necessary in low-level kernel or graphics code to access memory at a specific location, requiring a literal integer to pointer conversion such as the following:
unsigned int *ptr = 0xcfcfcfcf;
These conversions are machine dependent and should only be coded when absolutely necessary.
Non-Compliant Code Example
In this non-compliant code example, the pointer ptr
is converted to an integer value. Both a pointer and an int
are assumed to be 32 bits. The high-order nine bits of the number are used to hold a flag value, and the result is converted back into a pointer.
char *ptr; /* ... */ unsigned int number = ptr; number = (number & 0x7fffff) | (flag << 23); ptr = number;
A similar scheme was used in early versions of Emacs, limiting its portability and preventing the ability to edit files larger than 8MB.
Compliant Solution
Saving a few bits of storage is generally not as important as writing portable code. A struct
can be used to provide room for both the pointer and the flag value. This is portable to machines of different word sizes, both smaller and larger than 32 bits, and works even when pointers cannot be represented in any integer type.
struct ptrflag { char *pointer; unsigned int flag :9; } ptrflag; /* ... */ ptrflag.pointer = ptr; ptrflag.flag = flag;
Risk Analysis
Converting from pointer to integer or vice versa results in unportable code and may create unexpected pointers to invalid memory locations.
Recommendation |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
---|---|---|---|---|---|
INT15-A |
1 (low) |
2 (probable) |
1 (high) |
P2 |
L3 |
Automated Detection
The LDRA tool suite V 7.6.0 is able to detect violations of this recommendation.
References
[[ISO/IEC 9899-1999]] Section 6.3.2.3, "Pointers"
INT14-A. Avoid performing bitwise and arithmetic operations on the same data 04. Integers (INT)