1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-12-22 10:19:57 +01:00

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@g10code.com>
This commit is contained in:
Justus Winter 2017-01-10 15:42:27 +01:00
parent c8cfc62125
commit 1f5caf90bf
3 changed files with 52 additions and 6 deletions

View File

@ -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. */ /* Engine specific support. */
static void static void
gpg_agent_runtime_change (int killflag) 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->flags & GC_OPT_FLAG_LIST))
{ {
if (option->value) if (option->value)
free (option->value); xfree (option->value);
option->value = opt_value; option->value = opt_value;
} }
else else
@ -2192,10 +2221,9 @@ retrieve_options_from_program (gc_component_t component, gc_backend_t backend)
option->value = opt_value; option->value = opt_value;
else else
{ {
char *opt_val = opt_value; char *old = option->value;
option->value = xasprintf ("%s,%s", old, opt_value);
option->value = xasprintf ("%s,%s", option->value, xfree (old);
opt_val);
xfree (opt_value); xfree (opt_value);
} }
} }
@ -2872,7 +2900,12 @@ change_options_program (gc_component_t component, gc_backend_t backend,
res = link (dest_filename, orig_filename); res = link (dest_filename, orig_filename);
#endif #endif
if (res < 0 && errno != ENOENT) if (res < 0 && errno != ENOENT)
{
xfree (dest_filename);
xfree (src_filename);
xfree (orig_filename);
return -1; return -1;
}
if (res < 0) if (res < 0)
{ {
xfree (orig_filename); xfree (orig_filename);
@ -3365,6 +3398,7 @@ gc_component_change_options (int component, estream_t in, estream_t out,
} }
if (err) if (err)
break; break;
xfree (src_filename[i]);
src_filename[i] = NULL; src_filename[i] = NULL;
} }
} }
@ -3434,10 +3468,17 @@ gc_component_change_options (int component, estream_t in, estream_t out,
unlink (backup_filename); unlink (backup_filename);
#endif /* HAVE_W32_SYSTEM */ #endif /* HAVE_W32_SYSTEM */
rename (orig_filename[backend], backup_filename); rename (orig_filename[backend], backup_filename);
xfree (backup_filename);
} }
leave: leave:
xfree (line); xfree (line);
for (backend = 0; backend < GC_BACKEND_NR; backend++)
{
xfree (src_filename[backend]);
xfree (dest_filename[backend]);
xfree (orig_filename[backend]);
}
} }

View File

@ -470,6 +470,7 @@ main (int argc, char **argv)
/* Make sure that our subsystems are ready. */ /* Make sure that our subsystems are ready. */
i18n_init(); i18n_init();
init_common_subsystems (&argc, &argv); init_common_subsystems (&argc, &argv);
gc_components_init ();
/* Parse the command line. */ /* Parse the command line. */
pargs.argc = &argc; pargs.argc = &argc;

View File

@ -38,6 +38,10 @@ struct
/*-- gpgconf-comp.c --*/ /*-- gpgconf-comp.c --*/
/* Initialize the components. */
void gc_components_init (void);
/* Percent-Escape special characters. The string is valid until the /* Percent-Escape special characters. The string is valid until the
next invocation of the function. */ next invocation of the function. */
char *gc_percent_escape (const char *src); char *gc_percent_escape (const char *src);