Check results of bfd_alloc calls in elf.c
Binutils elf.c now checks for allocation failures to prevent crashes.
This commit enhances error handling in binutils’ elf.c by checking the return values of bfd_zalloc and bfd_alloc. Previously, the code did not explicitly check if these memory allocation calls returned NULL, which could lead to crashes if memory allocation failed. The change also simplifies some code by removing unnecessary type casts and temporary variables.
In Details
This patch revises error handling in bfd/elf.c for bfd_alloc and bfd_zalloc. It adds NULL checks for these memory allocation functions in assign_section_numbers and elfobj_grok_stapsdt_note_1, addressing a potential NULL pointer dereference. Unnecessary casts and a temporary variable are also removed to clean up the code.
- bfd_alloc
- A memory allocation function within the BFD library, used for allocating memory for BFD structures. It returns a pointer to the allocated memory or NULL on failure.
- bfd_zalloc
- Similar to
bfd_alloc, but also zeroes the allocated memory. Used in BFD for allocating and initializing memory structures. - elf.c
- A source file within the Binutils project that handles the reading and writing of ELF (Executable and Linkable Format) object files.
- NULL pointer dereference
- An error that occurs when a program attempts to access memory at a NULL address. This typically leads to a segmentation fault or program crash.