clang-tidy - bugprone-misplaced-pointer-arithmetic-in-alloc — Extra Clang Tools 15.0.7 documentation

# Extra Clang Tools 15.0.7 documentation

## clang-tidy - bugprone-misplaced-pointer-arithmetic-in-alloc

# bugprone-misplaced-pointer-arithmetic-in-alloc

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 of `malloc` and not to its result. In the example above the fix would be

    char *p = (char*) malloc(n + 10);