[PATCH 2/3] RISC-V: Free the returned string of riscv_arch_str if we call it multiple times

Nelson Chu nelson@rivosinc.com
Thu Mar 13 02:31:35 GMT 2025


The string returned from riscv_arch_str is allocated by xmalloc, so once we
called it multiple times, we should keep the newest one for the output elf
architecture attribute, but free the remaining unused strings.
---
 bfd/elfnn-riscv.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index 06e99401229..85dcb924f14 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -3940,7 +3940,7 @@ static char *
 riscv_merge_arch_attr_info (bfd *ibfd, char *in_arch, char *out_arch)
 {
   riscv_subset_t *in, *out;
-  char *merged_arch_str;
+  static char *merged_arch_str = NULL;
 
   unsigned xlen_in, xlen_out;
   merged_subsets.head = NULL;
@@ -4001,6 +4001,10 @@ riscv_merge_arch_attr_info (bfd *ibfd, char *in_arch, char *out_arch)
       return NULL;
     }
 
+  /* Free the previous merged_arch_str which called xmalloc.  */
+  if (merged_arch_str != NULL)
+    free ((void *) merged_arch_str);
+
   merged_arch_str = riscv_arch_str (ARCH_SIZE, &merged_subsets);
 
   /* Release the subset lists.  */
-- 
2.39.3 (Apple Git-146)



More information about the Binutils mailing list