The result of realloc is assigned to the pointer it was given, which is lost if it fails
What it finds
p = realloc(p, n): when realloc fails it returns NULL and leaves the old block alone, but the assignment has already overwritten the only pointer to it, so the block leaks and the data in it is lost. Assigning to a second variable first keeps it.
The target and the first argument must be the same tokens without calls or side effects, and the function the C library's realloc.
From the rule's source, realloc_same_pointer.rs.
#include <stdlib.h> struct buf { char *data; int size; }; int grow(char *p, struct buf *b, int n){ char *q; p = realloc(p, n);tx0 reports: if realloc fails, p becomes NULL and the old block leaks b->data = realloc(b->data, n);tx0 reports: if realloc fails, b->data becomes NULL q = realloc(p, n * 2); return q != NULL;} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/c:realloc-same-pointer" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/c:realloc-same-pointer] reason="..."tx0 explain tx0/c:realloc-same-pointer shows the same in the terminal. Found a false positive? Report it.