From: Junio C Hamano <gitster@pobox.com>
To: Hans Jerry Illikainen <hji@dyntopia.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH v3 1/2] gpg-interface: refactor the free-and-xmemdupz pattern
Date: Fri, 22 Nov 2019 11:45:13 +0900 [thread overview]
Message-ID: <xmqqo8x47hom.fsf@gitster-ct.c.googlers.com> (raw)
In-Reply-To: <20191121234336.26300-2-hji@dyntopia.com> (Hans Jerry Illikainen's message of "Thu, 21 Nov 2019 23:43:35 +0000")
Hans Jerry Illikainen <hji@dyntopia.com> writes:
> This commit introduces a static replace_cstring() function. This
> function simplifies the continuous pattern of free-and-xmemdupz() for
> GPG status line parsing.
>
> The benefit of having it in a single helper function is that it helps
> avoid the need for duplicate code that does the same thing. It also
> helps avoid potential memleaks if parsing of new status lines are
> introduced in the future.
>
> Signed-off-by: Hans Jerry Illikainen <hji@dyntopia.com>
> ---
> gpg-interface.c | 23 ++++++++++++++++-------
> 1 file changed, 16 insertions(+), 7 deletions(-)
Looks quite cleanly done and clearly explained. Thanks.
> diff --git a/gpg-interface.c b/gpg-interface.c
> index d60115ca40..b4c4443287 100644
> --- a/gpg-interface.c
> +++ b/gpg-interface.c
> @@ -105,6 +105,17 @@ static struct {
> { 0, "VALIDSIG ", GPG_STATUS_FINGERPRINT },
> };
>
> +static void replace_cstring(const char **field, const char *line,
> + const char *next)
> +{
> + free(*field);
> +
> + if (line && next)
> + *field = xmemdupz(line, next - line);
> + else
> + *field = NULL;
> +}
> +
> static void parse_gpg_output(struct signature_check *sigc)
> {
> const char *buf = sigc->gpg_status;
> @@ -136,21 +147,18 @@ static void parse_gpg_output(struct signature_check *sigc)
> /* Do we have key information? */
> if (sigcheck_gpg_status[i].flags & GPG_STATUS_KEYID) {
> next = strchrnul(line, ' ');
> - free(sigc->key);
> - sigc->key = xmemdupz(line, next - line);
> + replace_cstring(&sigc->key, line, next);
> /* Do we have signer information? */
> if (*next && (sigcheck_gpg_status[i].flags & GPG_STATUS_UID)) {
> line = next + 1;
> next = strchrnul(line, '\n');
> - free(sigc->signer);
> - sigc->signer = xmemdupz(line, next - line);
> + replace_cstring(&sigc->signer, line, next);
> }
> }
> /* Do we have fingerprint? */
> if (sigcheck_gpg_status[i].flags & GPG_STATUS_FINGERPRINT) {
> next = strchrnul(line, ' ');
> - free(sigc->fingerprint);
> - sigc->fingerprint = xmemdupz(line, next - line);
> + replace_cstring(&sigc->fingerprint, line, next);
>
> /* Skip interim fields */
> for (j = 9; j > 0; j--) {
> @@ -162,7 +170,8 @@ static void parse_gpg_output(struct signature_check *sigc)
>
> next = strchrnul(line, '\n');
> free(sigc->primary_key_fingerprint);
> - sigc->primary_key_fingerprint = xmemdupz(line, next - line);
> + replace_cstring(&sigc->primary_key_fingerprint,
> + line, next);
> }
>
> break;
next prev parent reply other threads:[~2019-11-22 2:45 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-11-16 18:06 [PATCH 0/1] Limit search for primary key fingerprint Hans Jerry Illikainen
2019-11-16 18:06 ` [PATCH 1/1] gpg-interface: limit " Hans Jerry Illikainen
2019-11-18 5:40 ` Junio C Hamano
2019-11-21 23:19 ` Hans Jerry Illikainen
2019-11-22 2:39 ` Junio C Hamano
2019-11-22 3:44 ` Junio C Hamano
2019-11-22 20:23 ` Hans Jerry Illikainen
2019-11-23 0:18 ` Junio C Hamano
2019-11-16 19:49 ` [PATCH 0/1] Limit " Jonathan Nieder
2019-11-16 21:58 ` [PATCH v2 " Hans Jerry Illikainen
2019-11-16 21:58 ` [PATCH v2 1/1] gpg-interface: limit " Hans Jerry Illikainen
2019-11-21 23:43 ` [PATCH v3 0/2] gpg-interface: fix " Hans Jerry Illikainen
2019-11-21 23:43 ` [PATCH v3 1/2] gpg-interface: refactor the free-and-xmemdupz pattern Hans Jerry Illikainen
2019-11-22 2:45 ` Junio C Hamano [this message]
2019-11-21 23:43 ` [PATCH v3 2/2] gpg-interface: limit search for primary key fingerprint Hans Jerry Illikainen
2019-11-22 3:34 ` Junio C Hamano
2019-11-22 20:23 ` [PATCH v4 0/2] Limit search for primary fingerprint Hans Jerry Illikainen
2019-11-22 20:23 ` [PATCH v4 1/2] gpg-interface: refactor the free-and-xmemdupz pattern Hans Jerry Illikainen
2019-11-22 20:23 ` [PATCH v4 2/2] gpg-interface: limit search for primary key fingerprint Hans Jerry Illikainen
2019-11-23 0:22 ` [PATCH v4 0/2] Limit search for primary fingerprint Junio C Hamano
2019-11-18 4:45 ` [PATCH 0/1] Limit search for primary key fingerprint Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xmqqo8x47hom.fsf@gitster-ct.c.googlers.com \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=hji@dyntopia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).