tidy - bugprone-misplaced-pointer-arithmetic-in-alloc — Extra Clang Tools 22.0.0git documentation (original) (raw)
Finds cases where an integer expression is added to or subtracted from the result of a memory allocation function (malloc(), calloc(),realloc(), alloca()) instead of its argument. The check detects error cases even if one of these functions is called by a constant function pointer.
Example code:
void bad_malloc(int n) { char p = (char) malloc(n) + 10; }
The suggested fix is to add the integer expression to the argument ofmalloc and not to its result. In the example above the fix would be
char p = (char) malloc(n + 10);