Subject: Re: [PATCH v7 1/3] tree-walk: learn get_tree_entry_follow_symlinks

From: Jeff King <[email-masked]>
Date: Wed, 13 May 2015 11:39:45 -0400

On Tue, May 12, 2015 at 09:49:57PM -0400, [email-masked] wrote:

> +		} else if (S_ISLNK(*mode)) {
> +			/* Follow a symlink */
> +			unsigned long link_len;
> +			size_t len;
> +			char *contents, *contents_start;
> [...]
> +			contents = read_sha1_file(current_tree_sha1, &type,
> +						  &link_len);

Here we have potentially allocated a buffer for contents...

> +
> +			if (!contents)
> +				goto done;

No need to free here, it's NULL...

> +
> +			if (contents[0] == '/') {
> +				strbuf_addstr(result_path, contents);
> +				*mode = 0;
> +				retval = FOUND;
> +				goto done;
> +			}

But here we leak it on "goto done".

> +			if (remainder)
> +				len = first_slash - namebuf.buf;
> +			else
> +				len = namebuf.len;
> +
> +			contents_start = contents;
> +
> +			parent = &parents[parents_nr - 1];
> +			init_tree_desc(&t, parent->tree, parent->size);
> +			strbuf_splice(&namebuf, 0, len,
> +				      contents_start, link_len);
> +			if (remainder)
> +				namebuf.buf[link_len] = '/';
> +			free(contents);
> +		}

And this code path calls free(), so it is good.

So I think you just need a free() in the conditional above.

-Peff