From d56b3bc1cfa128d0246cfac59411adaad2579bc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingo=20Kl=C3=B6cker?= Date: Wed, 18 Jan 2023 10:54:39 +0100 Subject: [PATCH] core: Defer adding --status-fd and --logger-fd to argument list * src/engine-gpg.c (_append_to_arglist, _prepend_to_arglist): New. (_add_arg): Use _append_to_arglist and _prepend_to_arglist. (add_data_ext): New. Extends add_data. (add_data): Uses add_data_ext. (gpg_new): Do not add --status-fd and --logger-fd to argument list. (start): Prepend --logger-fd and --status-fd to the argument list. -- This change makes it possible to handle those two arguments differently if gpgtar is used instead of gpg. GnuPG-bug-id: 6342 --- src/engine-gpg.c | 101 ++++++++++++++++++++++++++++------------------- 1 file changed, 60 insertions(+), 41 deletions(-) diff --git a/src/engine-gpg.c b/src/engine-gpg.c index 2f61ed4b..0ef4f309 100644 --- a/src/engine-gpg.c +++ b/src/engine-gpg.c @@ -222,6 +222,27 @@ close_notify_handler (int fd, void *opaque) } } +static void +_append_to_arglist (engine_gpg_t gpg, struct arg_and_data_s *a) +{ + a->next = NULL; + *gpg->argtail = a; + gpg->argtail = &a->next; +} + +static void +_prepend_to_arglist (engine_gpg_t gpg, struct arg_and_data_s *a) +{ + a->next = gpg->arglist; + if (!gpg->arglist) + { + /* If this is the first argument, we need to update the tail + pointer. */ + gpg->argtail = &a->next; + } + gpg->arglist = a; +} + /* If FRONT is true, push at the front of the list. Use this for options added late in the process. */ static gpgme_error_t @@ -247,22 +268,9 @@ _add_arg (engine_gpg_t gpg, const char *prefix, const char *arg, size_t arglen, memcpy (a->arg + prefixlen, arg, arglen); a->arg[prefixlen + arglen] = 0; if (front) - { - a->next = gpg->arglist; - if (!gpg->arglist) - { - /* If this is the first argument, we need to update the tail - pointer. */ - gpg->argtail = &a->next; - } - gpg->arglist = a; - } + _prepend_to_arglist (gpg, a); else - { - a->next = NULL; - *gpg->argtail = a; - gpg->argtail = &a->next; - } + _append_to_arglist (gpg, a); return 0; } @@ -301,7 +309,7 @@ add_arg_len (engine_gpg_t gpg, const char *prefix, static gpgme_error_t -add_data (engine_gpg_t gpg, gpgme_data_t data, int dup_to, int inbound) +add_data_ext (engine_gpg_t gpg, gpgme_data_t data, int dup_to, int inbound, int front) { struct arg_and_data_s *a; @@ -311,7 +319,6 @@ add_data (engine_gpg_t gpg, gpgme_data_t data, int dup_to, int inbound) a = malloc (offsetof (struct arg_and_data_s, arg)); if (!a) return gpg_error_from_syserror (); - a->next = NULL; a->data = data; a->inbound = inbound; a->arg_locp = NULL; @@ -326,12 +333,22 @@ add_data (engine_gpg_t gpg, gpgme_data_t data, int dup_to, int inbound) a->print_fd = 0; a->dup_to = dup_to; } - *gpg->argtail = a; - gpg->argtail = &a->next; + + if (front) + _prepend_to_arglist (gpg, a); + else + _append_to_arglist (gpg, a); + return 0; } +static gpgme_error_t +add_data (engine_gpg_t gpg, gpgme_data_t data, int dup_to, int inbound) +{ + return add_data_ext (gpg, data, dup_to, inbound, 0); +} + /* Return true if the engine's version is at least VERSION. */ static int have_gpg_version (engine_gpg_t gpg, const char *version) @@ -547,18 +564,6 @@ gpg_new (void **engine, const char *file_name, const char *home_dir, goto leave; } - rc = add_arg (gpg, "--status-fd"); - if (rc) - goto leave; - - { - char buf[25]; - _gpgme_io_fd2str (buf, sizeof (buf), gpg->status.fd[1]); - rc = add_arg_with_locp (gpg, buf, &gpg->status.arg_loc); - if (rc) - goto leave; - } - rc = add_arg (gpg, "--no-tty"); if (!rc) rc = add_arg (gpg, "--charset"); @@ -632,16 +637,6 @@ gpg_new (void **engine, const char *file_name, const char *home_dir, } } - rc = gpgme_data_new (&gpg->diagnostics); - if (rc) - goto leave; - - rc = add_arg (gpg, "--logger-fd"); - if (rc) - goto leave; - - rc = add_data (gpg, gpg->diagnostics, -2, 1); - leave: if (rc) gpg_release (gpg); @@ -1547,6 +1542,30 @@ start (engine_gpg_t gpg) if (!gpg->file_name && !_gpgme_get_default_gpg_name ()) return trace_gpg_error (GPG_ERR_INV_ENGINE); + rc = gpgme_data_new (&gpg->diagnostics); + if (rc) + return rc; + + rc = add_data_ext (gpg, gpg->diagnostics, -2, 1, 1); + if (rc) + return rc; + + rc = add_arg_ext (gpg, "--logger-fd", 1); + if (rc) + return rc; + + { + char buf[25]; + _gpgme_io_fd2str (buf, sizeof (buf), gpg->status.fd[1]); + rc = add_arg_with_locp (gpg, buf, &gpg->status.arg_loc, 1); + if (rc) + return rc; + } + + rc = add_arg_ext (gpg, "--status-fd", 1); + if (rc) + return rc; + if (gpg->lc_ctype) { rc = add_arg_ext (gpg, gpg->lc_ctype, 1);