Subject: Re: [PATCH] prefix_path(): Unconditionally free result of prefix_path

From: Jeff King <[email-masked]>
Date: Mon, 4 May 2015 23:21:59 -0400

On Mon, May 04, 2015 at 12:11:54PM -0700, Stefan Beller wrote:

> prefix_path() always returns a newly allocated string since
> d089eba (setup: sanitize absolute and funny paths in get_pathspec(),
> 2008-01-28)
> 
> Additionally the const is dropped from the pointers, so the call to
> free doesn't need a cast.
> 
> Signed-off-by: Stefan Beller <[email-masked]>
> ---
> 
> Notes:
>     Thanks for all the suggestions!
>     They are incorporated into this version of the patch.
> 
>  builtin/checkout-index.c | 10 ++++------
>  builtin/update-index.c   |  5 ++---
>  2 files changed, 6 insertions(+), 9 deletions(-)

Should we also squash in these sites? I think they are adequately
covered under the proposed log message.

Found by grepping for prefix_path calls. The only remainders are:

  1. in blame, we assign the result to a const char that may also point
     straight into to argv, but we never actually free either way

  2. test-path-utils does not free at all, but we probably don't care
     either way

diff --git a/builtin/update-index.c b/builtin/update-index.c
index a92eed2..0665b31 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -870,14 +870,14 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 		case PARSE_OPT_DONE:
 		{
 			const char *path = ctx.argv[0];
-			const char *p;
+			char *p;
 
 			setup_work_tree();
 			p = prefix_path(prefix, prefix_length, path);
 			update_one(p);
 			if (set_executable_bit)
 				chmod_path(set_executable_bit, p);
-			free((char *)p);
+			free(p);
 			ctx.argc--;
 			ctx.argv++;
 			break;
@@ -908,7 +908,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 
 		setup_work_tree();
 		while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
-			const char *p;
+			char *p;
 			if (line_termination && buf.buf[0] == '"') {
 				strbuf_reset(&nbuf);
 				if (unquote_c_style(&nbuf, buf.buf, NULL))
@@ -919,7 +919,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 			update_one(p);
 			if (set_executable_bit)
 				chmod_path(set_executable_bit, p);
-			free((char *)p);
+			free(p);
 		}
 		strbuf_release(&nbuf);
 		strbuf_release(&buf);