From c622993a154e6134b6dae0c31455e30cd96e4fa7 Mon Sep 17 00:00:00 2001 From: Jens Remus Date: Mon, 27 Jan 2025 16:43:39 +0100 Subject: [PATCH] s390: s390_machine leak Simplify the .machine directive parsing logic, so that cpu_string is always xstrdup'd and can therefore always be xfree'd before returning to the caller. This resolves the following memory leak reported by ASAN: Direct leak of 13 byte(s) in 3 object(s) allocated from: #0 0x3ff8aafbb1d in malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69 #1 0x2aa338861cf in xmalloc ../../libiberty/xmalloc.c:149 #2 0x2aa338868ff in xstrdup ../../libiberty/xstrdup.c:34 #3 0x2aa320253cb in s390_machine ../../gas/config/tc-s390.c:2172 #4 0x2aa31fddc7b in read_a_source_file ../../gas/read.c:1293 #5 0x2aa31f4f7bf in perform_an_assembly_pass ../../gas/as.c:1223 #6 0x2aa31f4f7bf in main ../../gas/as.c:1436 #7 0x3ff8a02be35 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58 #8 0x3ff8a02bf33 in __libc_start_main_impl ../csu/libc-start.c:360 #9 0x2aa31f5758f (/home/jremus/git/binutils/build-asan/gas/as-new+0x2d5758f) (BuildId: ...) While at it add tests with double quoted .machine "[+...]" values. gas/ * config/tc-s390.c (s390_machine): Simplify parsing and free cpu_string before returning. gas/testsuite/ * gas/s390/machine-parsing-1.l: Add tests with double quoted values. * gas/s390/machine-parsing-1.s: Likewise. Signed-off-by: Jens Remus --- gas/config/tc-s390.c | 38 ++++++++-------------- gas/testsuite/gas/s390/machine-parsing-1.l | 3 +- gas/testsuite/gas/s390/machine-parsing-1.s | 3 ++ 3 files changed, 19 insertions(+), 25 deletions(-) diff --git a/gas/config/tc-s390.c b/gas/config/tc-s390.c index 113b9f41a1e..b651acc3cf9 100644 --- a/gas/config/tc-s390.c +++ b/gas/config/tc-s390.c @@ -2146,32 +2146,21 @@ s390_machine (int ignore ATTRIBUTE_UNUSED) SKIP_WHITESPACE (); - if (*input_line_pointer == '"') - { - int len; - cpu_string = demand_copy_C_string (&len); - } - else - { - char c; - - cpu_string = input_line_pointer; - do - { - char * str; + { + char c; - c = get_symbol_name (&str); - c = restore_line_pointer (c); - if (c == '+') - ++ input_line_pointer; - } - while (c == '+'); + c = get_symbol_name (&cpu_string); + while (c == '+') + { + char *str; - c = *input_line_pointer; - *input_line_pointer = 0; - cpu_string = xstrdup (cpu_string); - (void) restore_line_pointer (c); - } + c = restore_line_pointer (c); + input_line_pointer++; + c = get_symbol_name (&str); + } + cpu_string = xstrdup (cpu_string); + (void) restore_line_pointer (c); + } if (cpu_string != NULL) { @@ -2217,6 +2206,7 @@ s390_machine (int ignore ATTRIBUTE_UNUSED) } } + xfree (cpu_string); demand_empty_rest_of_line (); } diff --git a/gas/testsuite/gas/s390/machine-parsing-1.l b/gas/testsuite/gas/s390/machine-parsing-1.l index 1a83ec81897..8850eff5432 100644 --- a/gas/testsuite/gas/s390/machine-parsing-1.l +++ b/gas/testsuite/gas/s390/machine-parsing-1.l @@ -1,2 +1,3 @@ .*: Assembler messages: -.*:5: Error: invalid machine .foo.* +.*:7: Error: invalid machine .foo.* +.*:8: Error: invalid machine .foo.* diff --git a/gas/testsuite/gas/s390/machine-parsing-1.s b/gas/testsuite/gas/s390/machine-parsing-1.s index ea402bca793..aa2a206a5fc 100644 --- a/gas/testsuite/gas/s390/machine-parsing-1.s +++ b/gas/testsuite/gas/s390/machine-parsing-1.s @@ -1,5 +1,8 @@ .text foo: .machine z13 + .machine "z13" .machine z13+vx+novx+htm+nohtm+vx+novx+htm+nohtm + .machine "z13+vx+novx+htm+nohtm+vx+novx+htm+nohtm" .machine foo + .machine "foo" -- 2.39.5