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 can have undesired consequences depending on the implementation. According to the C standard, Section 6.3.2.3 [ISO/IEC 9899:2011],
...
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.
See also undefined behavior 24 of Annex J.
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.
...
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
| 94 S | Fully implemented | |||||||
Compass/ROSE | |||||||||
PRQA QA-C |
| 0309 (U) | Partially implemented |
Related Vulnerabilities
...
ISE/IEC TR 17961 (Draft) Converting a pointer to integer or integer to pointer [intptrconv]
ISO/IEC TR 24772 "HFC Pointer casting and pointer type changes"
...