core: Use "goto leave" pattern to clean up resources

* src/engine-gpg.c (build_argv): Initialize fd_data_map and argv. Goto
leave on error. Free fd_data_map and argv on error.
--

This will make it easier to add an additional "--with-gpg-args" option
where needed.

GnuPG-bug-id: 6342
This commit is contained in:
Ingo Klöcker 2023-01-17 09:29:05 +01:00
parent 984ff7e3a2
commit fbce7deb3b
No known key found for this signature in database
GPG Key ID: F5A5D1692277A1E9

View File

@ -871,9 +871,9 @@ build_argv (engine_gpg_t gpg, const char *pgmname)
{ {
gpgme_error_t err; gpgme_error_t err;
struct arg_and_data_s *a; struct arg_and_data_s *a;
struct fd_data_map_s *fd_data_map; struct fd_data_map_s *fd_data_map = NULL;
size_t datac=0, argc=0, allocated_argc=0; size_t datac=0, argc=0, allocated_argc=0;
char **argv; char **argv = NULL;
int need_special = 0; int need_special = 0;
int use_agent = 0; int use_agent = 0;
char *p; char *p;
@ -952,19 +952,18 @@ build_argv (engine_gpg_t gpg, const char *pgmname)
fd_data_map = calloc (datac + 1, sizeof *fd_data_map); fd_data_map = calloc (datac + 1, sizeof *fd_data_map);
if (!fd_data_map) if (!fd_data_map)
{ {
int saved_err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
free_argv (argv); if (err)
return saved_err; goto leave;
} }
argc = datac = 0; argc = datac = 0;
argv[argc] = strdup (_gpgme_get_basename (pgmname)); /* argv[0] */ argv[argc] = strdup (_gpgme_get_basename (pgmname)); /* argv[0] */
if (!argv[argc]) if (!argv[argc])
{ {
int saved_err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
free (fd_data_map); if (err)
free_argv (argv); goto leave;
return saved_err;
} }
argc++; argc++;
if (need_special) if (need_special)
@ -972,10 +971,9 @@ build_argv (engine_gpg_t gpg, const char *pgmname)
argv[argc] = strdup ("--enable-special-filenames"); argv[argc] = strdup ("--enable-special-filenames");
if (!argv[argc]) if (!argv[argc])
{ {
int saved_err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
free (fd_data_map); if (err)
free_argv (argv); goto leave;
return saved_err;
} }
argc++; argc++;
} }
@ -984,10 +982,9 @@ build_argv (engine_gpg_t gpg, const char *pgmname)
argv[argc] = strdup ("--use-agent"); argv[argc] = strdup ("--use-agent");
if (!argv[argc]) if (!argv[argc])
{ {
int saved_err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
free (fd_data_map); if (err)
free_argv (argv); goto leave;
return saved_err;
} }
argc++; argc++;
} }
@ -1000,10 +997,9 @@ build_argv (engine_gpg_t gpg, const char *pgmname)
gpg->request_origin, NULL); gpg->request_origin, NULL);
if (!argv[argc]) if (!argv[argc])
{ {
int saved_err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
free (fd_data_map); if (err)
free_argv (argv); goto leave;
return saved_err;
} }
argc++; argc++;
} }
@ -1013,10 +1009,9 @@ build_argv (engine_gpg_t gpg, const char *pgmname)
argv[argc] = strdup (gpg->auto_key_locate); argv[argc] = strdup (gpg->auto_key_locate);
if (!argv[argc]) if (!argv[argc])
{ {
int saved_err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
free (fd_data_map); if (err)
free_argv (argv); goto leave;
return saved_err;
} }
argc++; argc++;
} }
@ -1026,10 +1021,9 @@ build_argv (engine_gpg_t gpg, const char *pgmname)
argv[argc] = strdup (gpg->trust_model); argv[argc] = strdup (gpg->trust_model);
if (!argv[argc]) if (!argv[argc])
{ {
int saved_err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
free (fd_data_map); if (err)
free_argv (argv); goto leave;
return saved_err;
} }
argc++; argc++;
} }
@ -1039,10 +1033,9 @@ build_argv (engine_gpg_t gpg, const char *pgmname)
argv[argc] = strdup ("--no-symkey-cache"); argv[argc] = strdup ("--no-symkey-cache");
if (!argv[argc]) if (!argv[argc])
{ {
int saved_err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
free (fd_data_map); if (err)
free_argv (argv); goto leave;
return saved_err;
} }
argc++; argc++;
} }
@ -1052,10 +1045,9 @@ build_argv (engine_gpg_t gpg, const char *pgmname)
argv[argc] = strdup ("--ignore-mdc-error"); argv[argc] = strdup ("--ignore-mdc-error");
if (!argv[argc]) if (!argv[argc])
{ {
int saved_err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
free (fd_data_map); if (err)
free_argv (argv); goto leave;
return saved_err;
} }
argc++; argc++;
} }
@ -1065,10 +1057,9 @@ build_argv (engine_gpg_t gpg, const char *pgmname)
argv[argc] = strdup ("--disable-dirmngr"); argv[argc] = strdup ("--disable-dirmngr");
if (!argv[argc]) if (!argv[argc])
{ {
int saved_err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
free (fd_data_map); if (err)
free_argv (argv); goto leave;
return saved_err;
} }
argc++; argc++;
} }
@ -1078,10 +1069,9 @@ build_argv (engine_gpg_t gpg, const char *pgmname)
argv[argc] = strdup ("--no-auto-check-trustdb"); argv[argc] = strdup ("--no-auto-check-trustdb");
if (!argv[argc]) if (!argv[argc])
{ {
int saved_err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
free (fd_data_map); if (err)
free_argv (argv); goto leave;
return saved_err;
} }
argc++; argc++;
} }
@ -1102,10 +1092,9 @@ build_argv (engine_gpg_t gpg, const char *pgmname)
argv[argc] = strdup (s); argv[argc] = strdup (s);
if (!argv[argc]) if (!argv[argc])
{ {
int saved_err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
free (fd_data_map); if (err)
free_argv (argv); goto leave;
return saved_err;
} }
argc++; argc++;
} }
@ -1116,10 +1105,9 @@ build_argv (engine_gpg_t gpg, const char *pgmname)
argv[argc] = strdup ("--batch"); argv[argc] = strdup ("--batch");
if (!argv[argc]) if (!argv[argc])
{ {
int saved_err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
free (fd_data_map); if (err)
free_argv (argv); goto leave;
return saved_err;
} }
argc++; argc++;
} }
@ -1140,10 +1128,9 @@ build_argv (engine_gpg_t gpg, const char *pgmname)
if (_gpgme_io_pipe (fds, fd_data_map[datac].inbound ? 1 : 0) if (_gpgme_io_pipe (fds, fd_data_map[datac].inbound ? 1 : 0)
== -1) == -1)
{ {
int saved_err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
free (fd_data_map); if (err)
free_argv (argv); goto leave;
return saved_err;
} }
if (_gpgme_io_set_close_notify (fds[0], if (_gpgme_io_set_close_notify (fds[0],
close_notify_handler, gpg) close_notify_handler, gpg)
@ -1191,10 +1178,9 @@ build_argv (engine_gpg_t gpg, const char *pgmname)
argv[argc] = malloc (buflen); argv[argc] = malloc (buflen);
if (!argv[argc]) if (!argv[argc])
{ {
int saved_err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
free (fd_data_map); if (err)
free_argv (argv); goto leave;
return saved_err;
} }
ptr = argv[argc]; ptr = argv[argc];
@ -1216,10 +1202,9 @@ build_argv (engine_gpg_t gpg, const char *pgmname)
argv[argc] = strdup (a->arg); argv[argc] = strdup (a->arg);
if (!argv[argc]) if (!argv[argc])
{ {
int saved_err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
free (fd_data_map); if (err)
free_argv (argv); goto leave;
return saved_err;
} }
argc++; argc++;
} }
@ -1230,9 +1215,18 @@ build_argv (engine_gpg_t gpg, const char *pgmname)
allocated array like ccparray in gnupg. */ allocated array like ccparray in gnupg. */
assert (argc <= allocated_argc); assert (argc <= allocated_argc);
gpg->argv = argv; leave:
gpg->fd_data_map = fd_data_map; if (err)
return 0; {
free (fd_data_map);
free_argv (argv);
}
else
{
gpg->argv = argv;
gpg->fd_data_map = fd_data_map;
}
return err;
} }