Patchwork Fix libcpp PCH related memory leaks (PR middle-end/56461)

login
register
mail settings
Submitter Jakub Jelinek
Date March 1, 2013, 8:17 p.m.
Message ID <20130301201713.GH12913@tucnak.redhat.com>
Download mbox | patch
Permalink /patch/224446/
State New
Headers show

Comments

Jakub Jelinek - March 1, 2013, 8:17 p.m.
Hi!

This patch fixes various memory leaks in libcpp during PCH writing.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2013-03-01  Jakub Jelinek  <jakub@redhat.com>

	PR middle-end/56461
	* files.c (_cpp_save_file_entries): Free result at the end.
	* pch.c (cpp_string_free): New function.
	(cpp_save_state): Use it in htab_create call.
	(cpp_write_pch_deps): Free ss->defs.  Destroy ss->definedhash.


	Jakub
Tom Tromey - March 1, 2013, 9:03 p.m.
>>>>> "Jakub" == Jakub Jelinek <jakub@redhat.com> writes:

Jakub> 2013-03-01  Jakub Jelinek  <jakub@redhat.com>
Jakub> 	PR middle-end/56461
Jakub> 	* files.c (_cpp_save_file_entries): Free result at the end.
Jakub> 	* pch.c (cpp_string_free): New function.
Jakub> 	(cpp_save_state): Use it in htab_create call.
Jakub> 	(cpp_write_pch_deps): Free ss->defs.  Destroy ss->definedhash.

Looks good to me.  This is ok, thanks.

Tom

Patch

--- libcpp/files.c.jj	2013-02-28 23:53:11.000000000 +0100
+++ libcpp/files.c	2013-03-01 16:36:09.849026241 +0100
@@ -1776,6 +1776,7 @@  _cpp_save_file_entries (cpp_reader *pfil
   struct pchf_data *result;
   size_t result_size;
   _cpp_file *f;
+  bool ret;
 
   for (f = pfile->all_files; f; f = f->next_file)
     ++count;
@@ -1832,7 +1833,9 @@  _cpp_save_file_entries (cpp_reader *pfil
   qsort (result->entries, result->count, sizeof (struct pchf_entry),
 	 pchf_save_compare);
 
-  return fwrite (result, result_size, 1, fp) == 1;
+  ret = fwrite (result, result_size, 1, fp) == 1;
+  free (result);
+  return ret;
 }
 
 /* Read the pchf_data structure from F.  */
--- libcpp/pch.c.jj	2013-01-15 09:04:55.000000000 +0100
+++ libcpp/pch.c	2013-03-01 17:05:59.048498975 +0100
@@ -187,6 +187,16 @@  cpp_string_eq (const void *a_p, const vo
 	  && memcmp (a->text, b->text, a->len) == 0);
 }
 
+/* Free memory associated with cpp_string.  */
+
+static void
+cpp_string_free (void *a_p)
+{
+  struct cpp_string *a = (struct cpp_string *) a_p;
+  free ((void *) a->text);
+  free (a);
+}
+
 /* Save the current definitions of the cpp_reader for dependency
    checking purposes.  When writing a precompiled header, this should
    be called at the same point in the compilation as cpp_valid_state
@@ -198,7 +208,7 @@  cpp_save_state (cpp_reader *r, FILE *f)
   /* Save the list of non-void identifiers for the dependency checking.  */
   r->savedstate = XNEW (struct cpp_savedstate);
   r->savedstate->definedhash = htab_create (100, cpp_string_hash,
-					    cpp_string_eq, NULL);
+					    cpp_string_eq, cpp_string_free);
   cpp_forall_identifiers (r, save_idents, r->savedstate);
 
   /* Write out the list of defined identifiers.  */
@@ -336,6 +346,8 @@  cpp_write_pch_deps (cpp_reader *r, FILE
       return -1;
     }
   free (ss->definedstrs);
+  free (ss->defs);
+  htab_delete (ss->definedhash);
 
   /* Free the saved state.  */
   free (ss);