Ensure riscv_set_arch() handles failure gracefully
The RISC-V architecture setter `riscv_set_arch()` now prevents null pointer dereferences by setting defaults on failure.
The riscv_set_arch() function, which configures the RISC-V architecture for the assembler, has been hardened to prevent null pointer dereferences upon failure. Previously, if the function failed to parse an architecture string, subsequent operations might encounter uninitialized riscv_rps_as.subset_list or file_arch_str pointers, leading to crashes. Now, in such error scenarios, the function correctly returns or falls back to a safe default architecture string, ensuring stability and preventing undefined behavior when a valid architecture cannot be determined.
In Details
The riscv_set_arch() function in gas/config/tc-riscv.c is responsible for setting the target RISC-V architecture and its supported instruction set extensions based on user input (e.g., -march flag). This commit addresses a critical flaw: if riscv_set_arch() failed to parse an architecture string, it could return without properly initializing riscv_rps_as.subset_list or file_arch_str. Subsequent calls attempting to use these uninitialized pointers would lead to null dereferences and crashes. The fix ensures that on parsing failure or empty input, the function either explicitly retu…
For Context
When you use the assembler (gas) for RISC-V, you specify which specific RISC-V processor features (architecture) you want to compile for. This is often done with a command-line flag like -march. The riscv_set_arch() function is the part of the assembler that takes this specification and configures everything accordingly. This commit is a safety improvement: previously, if you provided an invalid or empty architecture string, this function might fail to set up correctly. This could lead to the assembler trying to use uninitialized data later on, potentially causing it to crash. The fix ensures that if something goes wrong during this setup, the assembler either stops gracefully or uses a standard, known-good default architecture, preventing crashes and making the tool more robust.