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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Mar 21, 2017 at 11:14:23AM -0700, Junio C Hamano wrote:

> Jeff King <peff@xxxxxxxx> 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.

> 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



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]