]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
s390: s390_machine leak
authorJens Remus <jremus@linux.ibm.com>
Mon, 27 Jan 2025 15:43:39 +0000 (16:43 +0100)
committerJens Remus <jremus@linux.ibm.com>
Mon, 27 Jan 2025 15:43:39 +0000 (16:43 +0100)
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
"<cpu>[+<extension>...]" 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 <jremus@linux.ibm.com>
gas/config/tc-s390.c
gas/testsuite/gas/s390/machine-parsing-1.l
gas/testsuite/gas/s390/machine-parsing-1.s

index 113b9f41a1ece0493590b191103f85af59a09f73..b651acc3cf90bec4d9e267dba82e7dd7c3ac2111 100644 (file)
@@ -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 ();
 }
 
index 1a83ec81897d2da3be449565db09b4075524dcac..8850eff5432086c56d12debb60e18e4db56a4baf 100644 (file)
@@ -1,2 +1,3 @@
 .*: Assembler messages:
-.*:5: Error: invalid machine .foo.*
+.*:7: Error: invalid machine .foo.*
+.*:8: Error: invalid machine .foo.*
index ea402bca793fa35a060a8227c61c159bb2d20657..aa2a206a5fc1dd0a9ff723e2267aab86ce05bb86 100644 (file)
@@ -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"