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

Re: [PATCH] builtin-remote: (get_one_entry): use strbuf

From
Jeff King <peff@peff.net>
Date
Jun 22, 2009, 21:32 UTC
Message-ID
<20090622213204.GB19364@coredump.intra.peff.net>
In-Reply-To
<1245659059-11852-1-git-send-email-bert.wesarg@googlemail.com>
On Mon, Jun 22, 2009 at 10:24:19AM +0200, Bert Wesarg wrote:
Show 24 quoted lines
> --- a/builtin-remote.c
> +++ b/builtin-remote.c
> @@ -1276,17 +1276,17 @@ static int update(int argc, const char **argv)
>  static int get_one_entry(struct remote *remote, void *priv)
>  {
>  	struct string_list *list = priv;
> +	struct strbuf url_buf = STRBUF_INIT;
> +	const char *url_str = NULL;
>  	const char **url;
>  	int i, url_nr;
> -	void **utilp;
>  
>  	if (remote->url_nr > 0) {
> -		utilp = &(string_list_append(remote->name, list)->util);
> -		*utilp = xmalloc(strlen(remote->url[0])+strlen(" (fetch)")+1);
> -		strcpy((char *) *utilp, remote->url[0]);
> -		strcat((char *) *utilp, " (fetch)");
> -	} else
> -		string_list_append(remote->name, list)->util = NULL;
> +		strbuf_addf(&url_buf, "%s (fetch)", remote->url[0]);
> +		url_str = strbuf_detach(&url_buf, NULL);
> +	}
> +	string_list_append(remote->name, list)->util = url_str;
> +

This causes const warnings due to the 'const' on url_str. One solution is just s/const char/char/.

However, I think it is actually more readable to do away with url_str entirely. The original if/else logic was much easier to follow than realizing that url_str is initialized to NULL, and then never changed. IOW:

  if (remote->url_nr > 0) {
          strbuf_addf(&url_buf, "%s (fetch)", remote->url[0]);
          string_list_append(remote->name, list)->util =
                  strbuf_detach(&url_buf, NULL);
  }
  else
          string_list_append(remote->name, list)->util = NULL;
Show 12 quoted lines
> @@ -1296,10 +1296,9 @@ static int get_one_entry(struct remote *remote, void *priv)
>  	}
>  	for (i = 0; i < url_nr; i++)
>  	{
> -		utilp = &(string_list_append(remote->name, list)->util);
> -		*utilp = xmalloc(strlen(url[i])+strlen(" (push)")+1);
> -		strcpy((char *) *utilp, url[i]);
> -		strcat((char *) *utilp, " (push)");
> +		strbuf_addf(&url_buf, "%s (push)", url[i]);
> +		url_str = strbuf_detach(&url_buf, NULL);
> +		string_list_append(remote->name, list)->util = url_str;
>  	}

And then this one is re-using url_str for a totally unrelated thing, but could just be:

  string_list_append(remote->name, list)->util =
          strbuf_detach(&url_buf, NULL);

But that is somewhat nit-picking. As long as the const-warning goes away, I will be happy enough.

-Peff
Previous: Bert WesargNext: Bert Wesarg
Message 8 of 9 in “avoid NULL dereference on failed malloc”
  1. avoid NULL dereference on failed mallocJim Meyering, Jun 14, 2009
  2. Michael J GruberJun 15, 2009
  3. builtin-remote: (get_one_entry): use strbufBert Wesarg, Jun 15, 2009
  4. Michael J GruberJun 16, 2009
  5. Bert WesargJun 16, 2009
  6. Michael J GruberJun 16, 2009
  7. builtin-remote: (get_one_entry): use strbufBert Wesarg, Jun 22, 2009
  8. Jeff KingJun 22, 2009
  9. builtin-remote: (get_one_entry): use strbufBert Wesarg, Jun 22, 2009

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.