git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v3 1/2] gpg-interface: refactor the free-and-xmemdupz pattern

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 22, 2019, 02:45 UTC
Message-ID
<xmqqo8x47hom.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<20191121234336.26300-2-hji@dyntopia.com>
Hans Jerry Illikainen <hji@dyntopia.com> writes:
Show 13 quoted lines
> 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.
Show 57 quoted lines
> 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;
Previous: Hans Jerry IllikainenNext: Hans Jerry Illikainen
Message 14 of 21 in “Limit search for primary key fingerprint”
  1. 0/1 Limit search for primary key fingerprintHans Jerry Illikainen, Nov 16, 2019
  2. 1/1 gpg-interface: limit search for primary key fingerprintHans Jerry Illikainen, Nov 16, 2019
  3. Junio C HamanoNov 18, 2019
  4. Hans Jerry IllikainenNov 21, 2019
  5. Junio C HamanoNov 22, 2019
  6. Junio C HamanoNov 22, 2019
  7. Hans Jerry IllikainenNov 22, 2019
  8. Junio C HamanoNov 23, 2019
  9. Jonathan NiederNov 16, 2019
  10. 0/1 Limit search for primary key fingerprintHans Jerry Illikainen, Nov 16, 2019
  11. 1/1 gpg-interface: limit search for primary key fingerprintHans Jerry Illikainen, Nov 16, 2019
  12. 0/2 gpg-interface: fix search for primary key fingerprintHans Jerry Illikainen, Nov 21, 2019
  13. 1/2 gpg-interface: refactor the free-and-xmemdupz patternHans Jerry Illikainen, Nov 21, 2019
  14. Junio C HamanoNov 22, 2019
  15. 2/2 gpg-interface: limit search for primary key fingerprintHans Jerry Illikainen, Nov 21, 2019
  16. Junio C HamanoNov 22, 2019
  17. 0/2 Limit search for primary fingerprintHans Jerry Illikainen, Nov 22, 2019
  18. 1/2 gpg-interface: refactor the free-and-xmemdupz patternHans Jerry Illikainen, Nov 22, 2019
  19. 2/2 gpg-interface: limit search for primary key fingerprintHans Jerry Illikainen, Nov 22, 2019
  20. Junio C HamanoNov 23, 2019
  21. Junio C HamanoNov 18, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.