]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
RISC-V: Free the returned string of riscv_arch_str if we call it multiple times
authorNelson Chu <nelson@rivosinc.com>
Thu, 13 Mar 2025 02:31:35 +0000 (10:31 +0800)
committerNelson Chu <nelson@rivosinc.com>
Tue, 18 Mar 2025 04:15:11 +0000 (12:15 +0800)
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

index 06e99401229675af2f7454a96c172ba2f6b5d1fd..873e2688ce00ca50b8002f0a316707a55fec1962 100644 (file)
@@ -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,9 @@ riscv_merge_arch_attr_info (bfd *ibfd, char *in_arch, char *out_arch)
       return NULL;
     }
 
+  /* Free the previous merged_arch_str which called xmalloc.  */
+  free (merged_arch_str);
+
   merged_arch_str = riscv_arch_str (ARCH_SIZE, &merged_subsets);
 
   /* Release the subset lists.  */