[Pkg-gnupg-commit] [gnupg2] 53/116: tools: Fix memory leaks and improve error handling.

Daniel Kahn Gillmor dkg at fifthhorseman.net
Tue Jan 24 04:40:53 UTC 2017


This is an automated email from the git hooks/post-receive script.

dkg pushed a commit to branch master
in repository gnupg2.

commit 1f5caf90bfaaaf7b9d8c06c12087aeeae3748032
Author: Justus Winter <justus at g10code.com>
Date:   Tue Jan 10 15:42:27 2017 +0100

    tools: Fix memory leaks and improve error handling.
    
    * tools/gpgconf-comp.c (gc_option_free): New function.
    (gc_components_free): Likewise.
    (gc_components_init): Likewise.
    (retrieve_options_from_program): Use 'xfree', fix memory leak.
    (change_options_program): Improve error handling.
    (gc_component_change_options): Fix memory leaks.
    * tools/gpgconf.c (main): Initialize components.
    * tools/gpgconf.h (gc_components_init): New prototype.
    
    Signed-off-by: Justus Winter <justus at g10code.com>
---
 tools/gpgconf-comp.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++------
 tools/gpgconf.c      |  1 +
 tools/gpgconf.h      |  4 ++++
 3 files changed, 52 insertions(+), 6 deletions(-)

diff --git a/tools/gpgconf-comp.c b/tools/gpgconf-comp.c
index 925f1cf..2dcf075 100644
--- a/tools/gpgconf-comp.c
+++ b/tools/gpgconf-comp.c
@@ -1102,6 +1102,35 @@ struct error_line_s
 
 
 

+
+/* Initialization and finalization.  */
+
+static void
+gc_option_free (gc_option_t *o)
+{
+  if (o == NULL || o->name == NULL)
+    return;
+
+  xfree (o->value);
+  gc_option_free (o + 1);
+}
+
+static void
+gc_components_free (void)
+{
+  int i;
+  for (i = 0; i < DIM (gc_component); i++)
+    gc_option_free (gc_component[i].options);
+}
+
+void
+gc_components_init (void)
+{
+  atexit (gc_components_free);
+}
+
+

+
 /* Engine specific support.  */
 static void
 gpg_agent_runtime_change (int killflag)
@@ -2183,7 +2212,7 @@ retrieve_options_from_program (gc_component_t component, gc_backend_t backend)
 	      if (!(option->flags & GC_OPT_FLAG_LIST))
 		{
 		  if (option->value)
-		    free (option->value);
+		    xfree (option->value);
 		  option->value = opt_value;
 		}
 	      else
@@ -2192,10 +2221,9 @@ retrieve_options_from_program (gc_component_t component, gc_backend_t backend)
 		    option->value = opt_value;
 		  else
 		    {
-		      char *opt_val = opt_value;
-
-		      option->value = xasprintf ("%s,%s", option->value,
-						 opt_val);
+		      char *old = option->value;
+		      option->value = xasprintf ("%s,%s", old, opt_value);
+		      xfree (old);
 		      xfree (opt_value);
 		    }
 		}
@@ -2872,7 +2900,12 @@ change_options_program (gc_component_t component, gc_backend_t backend,
   res = link (dest_filename, orig_filename);
 #endif
   if (res < 0 && errno != ENOENT)
-    return -1;
+    {
+      xfree (dest_filename);
+      xfree (src_filename);
+      xfree (orig_filename);
+      return -1;
+    }
   if (res < 0)
     {
       xfree (orig_filename);
@@ -3365,6 +3398,7 @@ gc_component_change_options (int component, estream_t in, estream_t out,
 		}
 	      if (err)
 		break;
+	      xfree (src_filename[i]);
 	      src_filename[i] = NULL;
 	    }
 	}
@@ -3434,10 +3468,17 @@ gc_component_change_options (int component, estream_t in, estream_t out,
 	unlink (backup_filename);
 #endif /* HAVE_W32_SYSTEM */
 	rename (orig_filename[backend], backup_filename);
+	xfree (backup_filename);
       }
 
  leave:
   xfree (line);
+  for (backend = 0; backend < GC_BACKEND_NR; backend++)
+    {
+      xfree (src_filename[backend]);
+      xfree (dest_filename[backend]);
+      xfree (orig_filename[backend]);
+    }
 }
 
 
diff --git a/tools/gpgconf.c b/tools/gpgconf.c
index af65424..a1034e6 100644
--- a/tools/gpgconf.c
+++ b/tools/gpgconf.c
@@ -470,6 +470,7 @@ main (int argc, char **argv)
   /* Make sure that our subsystems are ready.  */
   i18n_init();
   init_common_subsystems (&argc, &argv);
+  gc_components_init ();
 
   /* Parse the command line. */
   pargs.argc  = &argc;
diff --git a/tools/gpgconf.h b/tools/gpgconf.h
index 39d34b6..d6d7627 100644
--- a/tools/gpgconf.h
+++ b/tools/gpgconf.h
@@ -38,6 +38,10 @@ struct
 
 
 /*-- gpgconf-comp.c --*/
+
+/* Initialize the components.  */
+void gc_components_init (void);
+
 /* Percent-Escape special characters.  The string is valid until the
    next invocation of the function.  */
 char *gc_percent_escape (const char *src);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-gnupg/gnupg2.git



More information about the Pkg-gnupg-commit mailing list