rspec/rules/S860/rule.adoc
2021-01-27 13:42:22 +01:00

32 lines
1.2 KiB
Plaintext

Converting an integer type to a pointer generally leads to unspecified behavior. There are several cases where it might be legitimate:
* Converting the integral literal ``++0++`` to the null pointer (but you should use ``++nullptr++`` instead, see S4962),
* Converting back to a pointer a pointer value that was converted to a large enough integer (see S1767),
* On embedded devices, device drivers... converting a hard-coded address to a pointer to read some specific memory (this often goes together with the use of ``++volatile++``, since such memory values can change from the outside of the program).
Since even legitimate cases are corner cases that require to be reviewed carefully, this rule simply reports all places where an integer is cast into a pointer (except the literal ``++0++``).
== Noncompliant Code Example
----
struct S {
int i;
int j;
};
void f(void* a);
void g(int i) {
S* s1 = (S*)i; // Noncompliant
f((void*)i); // Noncompliant
}
----
== See
* MISRA {cpp} 2008, 5-2-8 - An object with integer type or pointer to void type shall not be converted to an object with pointer type.
* https://wiki.sei.cmu.edu/confluence/x/0dUxBQ[CERT, INT36-C.] - Converting a pointer to integer or integer to pointer