This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC 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: Fix memory leaks in the v2 demangler


I finally found time to fix my own PR... there were a lot of memory
leaks in the demangler, most of which are caused by the fact that
do_type calls string_init () on its RESULT argument.  With a lot of
help from valgrind, they weren't too hard to track down.  This patch
fixes 2MB worth of memory leaks over about 9MB worth of mangled names.

Is this OK?

-- 
Daniel Jacobowitz
MontaVista Software                         Debian GNU/Linux Developer

2003-01-11  Daniel Jacobowitz  <drow@mvista.com>

	Fix PR c++/7612.
	* cplus-dem.c (demangle_signature): Call string_delete.
	Remove extra string_init.
	(demangle_arm_hp_template): Call string_delete instead of
	string_clear.  Add missing string_delete call.
	(demangle_qualified): Add missing string_delete call.
	(do_type): Remove unused variable btype.  Add missing string_delete
	call.  Call string_delete instead of string_clear.
	(demangle_fund_type): Move variable btype inside of the switch
	statement.  Add missing string_delete call.
	(do_arg): Call string_delete instead of string_clear.  Remove extra
	string_init.
	(demangle_nested_args): Free work->previous_argument.

Index: cplus-dem.c
===================================================================
RCS file: /cvs/src/src/libiberty/cplus-dem.c,v
retrieving revision 1.35
diff -u -p -r1.35 cplus-dem.c
--- cplus-dem.c	6 Oct 2002 20:21:01 -0000	1.35
+++ cplus-dem.c	12 Jan 2003 00:21:36 -0000
@@ -1429,6 +1429,7 @@ demangle_signature (work, mangled, declp
 	      {
 		string_append (&s, SCOPE_STRING (work));
 		string_prepends (declp, &s);
+		string_delete (&s);
 	      }
 	    oldmangled = NULL;
 	    expect_func = 1;
@@ -1508,7 +1509,6 @@ demangle_signature (work, mangled, declp
 	    {
 	      /* Read the return type. */
 	      string return_type;
-	      string_init (&return_type);
 
 	      (*mangled)++;
 	      success = do_type (work, mangled, &return_type);
@@ -2321,7 +2321,7 @@ demangle_arm_hp_template (work, mangled,
       string_append (declp, "<");
       while (1)
         {
-          string_clear (&arg);
+          string_delete (&arg);
           switch (**mangled)
             {
               case 'T':
@@ -2378,7 +2378,7 @@ demangle_arm_hp_template (work, mangled,
       string_append (declp, "<");
       /* should do error checking here */
       while (args < e) {
-	string_clear (&arg);
+	string_delete (&arg);
 
 	/* Check for type or literal here */
 	switch (*args)
@@ -2393,6 +2393,7 @@ demangle_arm_hp_template (work, mangled,
 	      goto cfront_template_args_done;
             string_append (&arg, "(");
             string_appends (&arg, &type_str);
+            string_delete (&type_str);
             string_append (&arg, ")");
             if (*args != 'L')
               goto cfront_template_args_done;
@@ -3350,6 +3351,7 @@ demangle_qualified (work, mangled, resul
             }
           else
             {
+              string_delete (&last_name);
               success = do_type (work, mangled, &last_name);
               if (!success)
                 break;
@@ -3492,10 +3494,8 @@ do_type (work, mangled, result)
   string decl;
   const char *remembered_type;
   int type_quals;
-  string btype;
   type_kind_t tk = tk_none;
 
-  string_init (&btype);
   string_init (&decl);
   string_init (result);
 
@@ -3613,6 +3613,7 @@ do_type (work, mangled, result)
 		string temp;
 		do_type (work, mangled, &temp);
 		string_prepends (&decl, &temp);
+		string_delete (&temp);
 	      }
 	    else if (**mangled == 't')
 	      {
@@ -3623,7 +3624,7 @@ do_type (work, mangled, result)
 		if (success)
 		  {
 		    string_prependn (&decl, temp.b, temp.p - temp.b);
-		    string_clear (&temp);
+		    string_delete (&temp);
 		  }
 		else
 		  break;
@@ -3803,11 +3804,8 @@ demangle_fund_type (work, mangled, resul
   int success = 1;
   char buf[10];
   unsigned int dec = 0;
-  string btype;
   type_kind_t tk = tk_integral;
 
-  string_init (&btype);
-
   /* First pick off any type qualifiers.  There can be more than one.  */
 
   while (!done)
@@ -3979,8 +3977,11 @@ demangle_fund_type (work, mangled, resul
       }
     case 't':
       {
+        string btype;
+        string_init (&btype);
         success = demangle_template (work, mangled, &btype, 0, 1, 1);
         string_appends (result, &btype);
+        string_delete (&btype);
         break;
       }
     default:
@@ -4182,12 +4183,9 @@ do_arg (work, mangled, result)
      do not want to add additional types to the back-referenceable
      type vector when processing a repeated type.  */
   if (work->previous_argument)
-    string_clear (work->previous_argument);
+    string_delete (work->previous_argument);
   else
-    {
-      work->previous_argument = (string*) xmalloc (sizeof (string));
-      string_init (work->previous_argument);
-    }
+    work->previous_argument = (string*) xmalloc (sizeof (string));
 
   if (!do_type (work, mangled, work->previous_argument))
     return 0;
@@ -4551,7 +4549,10 @@ demangle_nested_args (work, mangled, dec
 
   /* Restore the previous_argument field.  */
   if (work->previous_argument)
-    string_delete (work->previous_argument);
+    {
+      string_delete (work->previous_argument);
+      free ((char *) work->previous_argument);
+    }
   work->previous_argument = saved_previous_argument;
   --work->forgetting_types;
   work->nrepeats = saved_nrepeats;


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