[gcc r11-9744] RISC-V: Fix use-after-free error in `parse_multiletter_ext'

Kito Cheng kito@gcc.gnu.org
Wed Mar 30 09:03:11 GMT 2022


https://gcc.gnu.org/g:7a35044979672ace7d8b2ea132903ab5f0cc180b

commit r11-9744-g7a35044979672ace7d8b2ea132903ab5f0cc180b
Author: Maciej W. Rozycki <macro@embecosm.com>
Date:   Tue Jan 18 19:39:13 2022 +0000

    RISC-V: Fix use-after-free error in `parse_multiletter_ext'
    
    Avoid undefined arithmetic involving a pointer to a heap allocation that
    has been freed and move a problematic calculation ahead of the following
    call to `free' in `riscv_subset_list::parse_multiletter_ext', removing a
    compilation error:
    
    .../gcc/common/config/riscv/riscv-common.c: In member function 'const char* riscv_subset_list::parse_multiletter_ext(const char*, const char*, const char*)':
    .../gcc/common/config/riscv/riscv-common.cc:905:27: error: pointer 'subset' used after 'void free(void*)' [-Werror=use-after-free]
      905 |       p += end_of_version - subset;
          |            ~~~~~~~~~~~~~~~^~~~~~~~
    .../gcc/common/config/riscv/riscv-common.cc:904:12: note: call to 'void free(void*)' here
      904 |       free (subset);
          |       ~~~~~^~~~~~~~
    cc1plus: all warnings being treated as errors
    make[2]: *** [Makefile:2428: riscv-common.o] Error 1
    
    and a build regression from commit 671a283636de ("Add -Wuse-after-free
    [PR80532].").
    
            gcc/
            * common/config/riscv/riscv-common.c
            (riscv_subset_list::parse_multiletter_ext): Move pointer
            arithmetic ahead of `free'.
    
    (cherry picked from commit dad495e30135904b0d0305eab8c0ce5f838440d4)

Diff:
---
 gcc/common/config/riscv/riscv-common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gcc/common/config/riscv/riscv-common.c b/gcc/common/config/riscv/riscv-common.c
index 8f222d17233..dff11cd3dc0 100644
--- a/gcc/common/config/riscv/riscv-common.c
+++ b/gcc/common/config/riscv/riscv-common.c
@@ -791,8 +791,8 @@ riscv_subset_list::parse_multiletter_ext (const char *p,
 	}
 
       add (subset, major_version, minor_version, explicit_version_p, false);
-      free (subset);
       p += end_of_version - subset;
+      free (subset);
 
       if (*p != '\0' && *p != '_')
 	{


More information about the Gcc-cvs mailing list