Re: [PATCH] objtool: Fix memory leak in elf_alloc_reloc() on realloc failure
From: Peter Zijlstra
Date: Mon Jan 19 2026 - 07:38:21 EST
On Sun, Jan 18, 2026 at 06:56:43AM +0000, Weigang He wrote:
> When realloc() fails in elf_alloc_reloc(), the original buffer pointer
> is overwritten with NULL before the failure is detected. This causes
> the original buffer to become unreachable, resulting in a memory leak.
>
> Fix this by using a temporary variable to hold the realloc() result.
> If realloc() fails, free the original buffer and set d_buf to NULL to
> maintain the expected error state before returning -1.
>
> This bug is found by my static analysis tool and my code review.
Yeah, except that the moment this error is actually hit, the tool will
exit, freeing all memory.