Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

While it used to be common practice to use integers and pointers interchangeably in C (although it was not considered good style), the C99 standard has mandated that pointer to integer and integer to pointer conversions are implementation defined. By definition of the standard, the only value which can be considered interchangeable between pointers and integers is the constant 0. This means that less the exception of 0, mixing integers and pointers may have undesired consequences depending on the implementation which is used.

Universal Integer/Pointer Storage

Wiki Markup
According to C99 \[[ISO/IEC 9899-1999:TC2|AA. C References#ISO/IEC 9899-1999TC2]\], the only value which can be considered interchangeable between pointers and integers is the constant 0. This means that less the exception of 0, mixing integers and pointers may have undesired consequences depending on the implementation which is used:

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.

This is because the mapping functions for converting a pointer to an integer or an integer to a pointer are intended to
be consistent with the addressing structure of the execution environment.

Non-Compliant Code Example

...

In this non-compliant code, the pointer ptr is used in an arithmetic operation that is eventually casted as converted to an integer , as stated abovevalue. As previously stated, the actual result for both of this assignment and following assignment to ptr2 are implementation defined.

...

A union can be used to give raw memory access to both an integer and a pointer and will take up . This is an efficient approach as the structure only requires as much size as its largest element which will mitigate a loss of datastorage as the larger of the two fields.

Code Block
bgColor#ccccff
union intpoint {
	  unsigned int *pointer;
	  unsigned int number;
} intpoint;
...
intpoint mydata = 0xcfcfcfcf;
...
unsigned int num = mydata.number + 1;
unsigned int *ptr = mydata.pointer;

Accessing Memory-Mapped Addresses*

Note: This is a bad idea. The following is a description of how to more properly execute a bad idea.

Non-Compliant Code Example

It is sometimes necessary Oftentimes in low level kernel or graphics code you will need to access memory at a specific location . Thus, you are going to have to make requiring a literal integer to pointer to conversion.

Non-Compliant Code Example

In this non-compliant code, a pointer is set directly to an integer constant, where it is unknown whether the result will be as intended.

Code Block
bgColor#FFcccc
unsigned int *ptr = 0xcfcfcfcf;

Because integers are no longer pointers this could have drastic consequencesThe result of this assignment is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.

Compliant Solution

Adding in an explicit cast may help the compiler format and store convert the integer value as expected into the a valid pointer.

Code Block
bgColor#ccccff
unsigned int *ptr = (unsigned int *) 0xcfcfcfcf;

Risk Analysis

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

INT14-A

2

2(unlikely)

3(medium)

P2

L3

...