RISC-V: Fix memory leaks in arch attribute merging
Resolves memory leaks in the RISC-V linker's arch attribute merging logic, preventing resource exhaustion.
The RISC-V linker’s architecture attribute merging function (riscv_merge_arch_attr_info) previously leaked memory on error paths. Subset lists (in_subsets, out_subsets, merged_subsets) were not released when the function exited prematurely due to parsing errors. This commit introduces a unified cleanup path to ensure all allocated subset nodes are freed, preventing both memory leaks and the carry-over of stale data into subsequent merge operations.
In Details
The riscv_merge_arch_attr_info function in bfd/elfxx-riscv.c handles the merging of RISC-V architecture attributes from multiple input files. Before this change, error returns on parsing inputs or XLEN mismatches failed to release the dynamically allocated nodes within in_subsets, out_subsets, and merged_subsets, leading to leaks. The fix consolidates error exit paths to guarantee cleanup via a goto cleanup label that frees these lists.
- memory leak
- A type of resource leak that occurs when a computer program incorrectly manages memory allocations. Inadvertently, the program retains a pointer to a block of memory that it no longer needs, making that block unavailable for future allocations.
- subset lists
- In the context of RISC-V architecture attributes, these lists track the specific extensions and features that are enabled or implied. They are used during the merging process to manage compatibility and derive the final architecture configuration.
- linker
- A program that takes one or more object files (produced by the compiler) and combines them into a single executable or library. It resolves symbol references between files and applies relocations.