This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH v2] Fix memory leak in sysinfo


New in v2: try to fix all the leaks.  I made sure that sysinfo was actually
built with Asan while testing.

I am trying to build the binutils-gdb repo with address sanitizer, but
the build fails because sysinfo (executed during the build) leaks, which
fails its execution and interrupts the Makefile.

Direct leak of 7122 byte(s) in 755 object(s) allocated from:
    #0 0x7f050664e602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0x403bca in yylex /home/emaisin/src/binutils-gdb/binutils/syslex.l:51
    #2 0x4016f9 in yyparse /home/emaisin/build/binutils-gdb/binutils/sysinfo.c:1179
    #3 0x4034b2 in main /home/emaisin/src/binutils-gdb/binutils/sysinfo.y:420
    #4 0x7f050620d82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

SUMMARY: AddressSanitizer: 7122 byte(s) leaked in 755 allocation(s).

One solution would have been to find a way to not pass
-fsanitize=address when building sysinfo, but it's not clear how to omit
the flag for this program only.

To fix the leaks, we need to free the memory allocated by the token NAME
whenever we are done with it.

binutils/ChangeLog:

	* sysinfo.y: Free memory allocated by token NAME.
---
 binutils/sysinfo.y | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/binutils/sysinfo.y b/binutils/sysinfo.y
index 62758de..1c8f1ff 100644
--- a/binutils/sysinfo.y
+++ b/binutils/sysinfo.y
@@ -21,6 +21,7 @@
 %{
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 static char writecode;
 static char *it;
@@ -159,6 +160,8 @@ it:
   case 'c':
     printf("}\n");
   }
+
+  free (it);
 }
 ;
 
@@ -204,6 +207,8 @@ repeat_it_field: '(' REPEAT NAME
 	 it_field_list ')'
 
 	{
+	  free (repeat);
+
 	  repeat = oldrepeat;
 	  oldrepeat =0;
 	  rdepth--;
@@ -230,6 +235,8 @@ cond_it_field: '(' COND NAME
 	      printf("\tif (%s) {\n", $3);
 	      break;
 	    }
+
+	  free ($3);
 	}
 
 	 it_field_list ')'
@@ -348,6 +355,9 @@ char *ptr = pnames[rdepth];
 	      else abort();
 		  break;
 		}
+
+	  free (desc);
+	  free (id);
 	}
 
 	;
@@ -371,7 +381,7 @@ attr_size:
 
 attr_id:
 		'(' NAME ')'	{ $$ = $2; }
-	|	{ $$ = "dummy";}
+	|	{ $$ = strdup ("dummy");}
 	;
 
 enums:
@@ -388,6 +398,9 @@ enum_list:
 	    case 'c':
 		printf("if (ptr->%s%s == %s) { tabout(); printf(\"%s\\n\");}\n", name, names[rdepth],$4,$3);
 	    }
+
+	  free ($3);
+	  free ($4);
 	}
 
 	;
-- 
2.7.4


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]