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

Re: [PATCH 4/6] prefix_filename: return newly allocated string

From
Jeff King <peff@peff.net>
Date
Mar 21, 2017, 18:23 UTC
Message-ID
<20170321182335.3yrjkhk4mxhso73j@sigill.intra.peff.net>
In-Reply-To
<xmqqinn2qyyo.fsf@gitster.mtv.corp.google.com>
On Tue, Mar 21, 2017 at 11:14:23AM -0700, Junio C Hamano wrote:
Show 28 quoted lines
> Jeff King <peff@peff.net> writes:
> 
> > diff --git a/worktree.c b/worktree.c
> > index 42dd3d52b..2520fc65c 100644
> > --- a/worktree.c
> > +++ b/worktree.c
> > @@ -250,16 +250,19 @@ struct worktree *find_worktree(struct worktree **list,
> >  {
> >  	struct worktree *wt;
> >  	char *path;
> > +	char *to_free;
> >  
> >  	if ((wt = find_worktree_by_suffix(list, arg)))
> >  		return wt;
> >  
> > -	arg = prefix_filename(prefix, arg);
> > +	if (prefix)
> > +		arg = to_free = prefix_filename(prefix, arg);
> >  	path = real_pathdup(arg, 1);
> >  	for (; *list; list++)
> >  		if (!fspathcmp(path, real_path((*list)->path)))
> >  			break;
> >  	free(path);
> > +	free(to_free);
> >  	return *list;
> >  }
> 
> worktree.c:265:6: error: to_free may be used uninitialized in this function

Doh. I had originally written it without the "if (prefix)" and added it as a micro-optimization at the end.

Still, the whole thing compiles fine for me. I find it odd that neither gcc nor clang notices the problem on my system. It's quite obviously wrong.

Show 10 quoted lines
> diff --git a/worktree.c b/worktree.c
> index 2520fc65cc..bae787cf8d 100644
> --- a/worktree.c
> +++ b/worktree.c
> @@ -250,7 +250,7 @@ struct worktree *find_worktree(struct worktree **list,
>  {
>  	struct worktree *wt;
>  	char *path;
> -	char *to_free;
> +	char *to_free = NULL;
Yep, this is the right fix. Thanks.
-Peff
Previous: Junio C HamanoNext: Jeff King
Message 7 of 11 in “prefix_filename cleanups”
  1. 0/6 prefix_filename cleanupsJeff King, Mar 21, 2017
  2. 1/6 hash-object: fix buffer reuse with --path in a subdirectoryJeff King, Mar 21, 2017
  3. 2/6 prefix_filename: move docstring to header fileJeff King, Mar 21, 2017
  4. 3/6 prefix_filename: drop length parameterJeff King, Mar 21, 2017
  5. 4/6 prefix_filename: return newly allocated stringJeff King, Mar 21, 2017
  6. Junio C HamanoMar 21, 2017
  7. Jeff KingMar 21, 2017
  8. Jeff KingMar 21, 2017
  9. 5/6 prefix_filename: simplify windows #ifdefJeff King, Mar 21, 2017
  10. 6/6 bundle: use prefix_filename with bundle pathJeff King, Mar 21, 2017
  11. Junio C HamanoMar 21, 2017

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.