Lossy pointer cast¶
ID: cpp/lossy-pointer-castKind: problemSecurity severity: Severity: warningPrecision: highTags: - reliability - correctness - typesQuery suites: - cpp-security-and-quality.qls
Click to see the query in the CodeQL repository
This rule finds expressions of pointer type which are (implicitly or explicitly) converted to an integer type of smaller size. This results in truncation of the most significant bits of the larger integer type.
Such conversions are highly non-portable, since the relative size of integer and pointer types may differ between architectures. For example, while on a 32-bit architecture both typeint and typechar* are four bytes wide, the latter occupies eight bytes on a 64-bit machine.
Recommendation¶
Avoid converting between pointer types and integer types.
Example¶
voidf(char*p){intmy_ptr=p;//Wrong: pointer assigned to int, would be incorrect if sizeof(char*)//is larger than sizeof(int)//...}
References¶
MSDN Library:Type Conversions and Type Safety.
Cplusplus.com:Type conversions.