binutils Newspaper
JUNE 15, 2026
risc-v Ready to Land

RISC-V: Avoid strdup()

Replaces ``strdup()`` with ``xstrdup()`` in several places within the RISC-V backend.

This patch replaces instances of strdup() with xstrdup() in the RISC-V backend. xstrdup() is a safer version of strdup() that handles memory allocation failures by calling xmalloc(), ensuring the program exits if memory allocation fails. This change improves the robustness of the code by preventing potential crashes due to out-of-memory conditions.

In the Thread 1 participant
  1. Jiawei <jiawei@iscas.ac.cn> proposer

    States that ``xstrdup()`` should be used along with ``xmalloc()``.

    “Along with xmalloc(), xstrdup() wants using.”

In Details

The RISC-V backend uses string duplication in several places, including riscv_copy_subset_list in bfd/elfxx-riscv.c and riscv_set_arch in gas/config/tc-riscv.c. The patch replaces strdup() with xstrdup(), which is expected to handle memory allocation failures more gracefully. The callers of these functions do not explicitly check the return value of strdup for NULL.

For Context

In C, strdup() is a function that duplicates a string by allocating memory and copying the string into the new memory. If memory allocation fails, strdup() returns NULL, and the program should check for this. However, if the program doesn't check, it can crash. This patch replaces strdup() with xstrdup(), which automatically handles the case where memory allocation fails, preventing potential crashes.

Filed Under: risc-vmemory managementstrdupxstrdup