Skip to contentSkip to conversation

prefix_path(): Unconditionally free result of prefix_path

PATCH In review Stefan Beller 4 participants 8 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 prefix_path(): Unconditionally free result of prefix_path

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.

---

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(-)
builtin/checkout-index.c +4 -6
index 9ca2da1..8028c37 100644
@@ -241,7 +241,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 	/* Check out named files first */
 	for (i = 0; i < argc; i++) {
 		const char *arg = argv[i];
-		const char *p;
+		char *p;
 
 		if (all)
 			die("git checkout-index: don't mix '--all' and explicit filenames");
@@ -249,8 +249,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 			die("git checkout-index: don't mix '--stdin' and explicit filenames");
 		p = prefix_path(prefix, prefix_length, arg);
 		checkout_file(p, prefix);
-		if (p < arg || p > arg + strlen(arg))
-			free((char *)p);
+		free(p);
 	}
 
 	if (read_from_stdin) {
@@ -260,7 +259,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 			die("git checkout-index: don't mix '--all' and '--stdin'");
 
 		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))
@@ -269,8 +268,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 			}
 			p = prefix_path(prefix, prefix_length, buf.buf);
 			checkout_file(p, prefix);
-			if (p < buf.buf || p > buf.buf + buf.len)
-				free((char *)p);
+			free(p);
 		}
 		strbuf_release(&nbuf);
 		strbuf_release(&buf);
builtin/update-index.c +2 -3
index 6271b54..a92eed2 100644
@@ -532,10 +532,9 @@ static int do_unresolve(int ac, const char **av,
 
 	for (i = 1; i < ac; i++) {
 		const char *arg = av[i];
-		const char *p = prefix_path(prefix, prefix_length, arg);
+		char *p = prefix_path(prefix, prefix_length, arg);
 		err |= unresolve_one(p);
-		if (p < arg || p > arg + strlen(arg))
-			free((char *)p);
+		free(p);
 	}
 	return err;
 }
Signature · 1 line
2.4.0.rc3.16.g0ab00b9
Reviewed-by on v1 1/1
#1 lore raw
Quoted 1 line · Stefan Beller

prefix_path(): Unconditionally free result of prefix_path

Slightly redundant mention of "prefix_path". Also, prevailing custom is to drop capitalization.

Quoted 3 lines

prefix_path() always returns a newly allocated string since d089eba (setup: sanitize absolute and funny paths in get_pathspec(), 2008-01-28)

I'd probably turn this sentence fragment into a proper sentence:

As of d089eba (...), prefix_path() always returns a newly allocated string, so free its result unconditionally.

Quoted 2 lines

Additionally the const is dropped from the pointers, so the call to free doesn't need a cast.

Imperative mood:

Additionally, drop the const from variables to which the prefix_path() result is assigned so they can be free()'d without having to cast-away constness.

Quoted 6 lines

---

Notes:
Thanks for all the suggestions!
They are incorporated into this version of the patch.

Thanks, this version looks much better.

FWIW, with or without addressing the very minor nits above:

Quoted 61 lines
builtin/checkout-index.c +4 -6
index 9ca2da1..8028c37 100644
@@ -241,7 +241,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
        /* Check out named files first */
        for (i = 0; i < argc; i++) {
                const char *arg = argv[i];
-               const char *p;
+               char *p;
 
                if (all)
                        die("git checkout-index: don't mix '--all' and explicit filenames");
@@ -249,8 +249,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
                        die("git checkout-index: don't mix '--stdin' and explicit filenames");
                p = prefix_path(prefix, prefix_length, arg);
                checkout_file(p, prefix);
-               if (p < arg || p > arg + strlen(arg))
-                       free((char *)p);
+               free(p);
        }
 
        if (read_from_stdin) {
@@ -260,7 +259,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
                        die("git checkout-index: don't mix '--all' and '--stdin'");
 
                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))
@@ -269,8 +268,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
                        }
                        p = prefix_path(prefix, prefix_length, buf.buf);
                        checkout_file(p, prefix);
-                       if (p < buf.buf || p > buf.buf + buf.len)
-                               free((char *)p);
+                       free(p);
                }
                strbuf_release(&nbuf);
                strbuf_release(&buf);
builtin/update-index.c +2 -4
index 6271b54..a92eed2 100644
@@ -532,10 +532,9 @@ static int do_unresolve(int ac, const char **av,
 
        for (i = 1; i < ac; i++) {
                const char *arg = av[i];
-               const char *p = prefix_path(prefix, prefix_length, arg);
+               char *p = prefix_path(prefix, prefix_length, arg);
                err |= unresolve_one(p);
-               if (p < arg || p > arg + strlen(arg))
-                       free((char *)p);
+               free(p);
        }
        return err;
 }
--

2.4.0.rc3.16.g0ab00b9

#2 lore raw
Quoted 31 lines · Eric Sunshine
Quoted 1 line · Stefan Beller

prefix_path(): Unconditionally free result of prefix_path

Slightly redundant mention of "prefix_path". Also, prevailing custom is to drop capitalization.

Quoted 3 lines

prefix_path() always returns a newly allocated string since d089eba (setup: sanitize absolute and funny paths in get_pathspec(), 2008-01-28)

I'd probably turn this sentence fragment into a proper sentence:

As of d089eba (...), prefix_path() always returns a newly allocated string, so free its result unconditionally.

Quoted 2 lines

Additionally the const is dropped from the pointers, so the call to free doesn't need a cast.

Imperative mood:

Additionally, drop the const from variables to which the prefix_path() result is assigned so they can be free()'d without having to cast-away constness.

Quoted 6 lines

---

Notes:
Thanks for all the suggestions!
They are incorporated into this version of the patch.

Quoted 5 lines

Thanks, this version looks much better.

FWIW, with or without addressing the very minor nits above:

Thanks, both.

To save a round-trip, I'll munge the log message myself stealing Eric's suggestions.

#3 lore raw
In reply to Stefan Beller
Quoted 10 lines · Stefan Beller

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.

---

Quoted 7 lines

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

builtin/update-index.c +4 -4
index a92eed2..0665b31 100644
@@ -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);
#4 lore raw
Quoted 16 lines · Jeff King
Quoted 14 lines · Stefan Beller

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.

---

Notes:
Thanks for all the suggestions!
They are incorporated into this version of the patch.

Quoted 6 lines
Quoted 3 lines
 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.

That sounds good to me.

Quoted 48 lines

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

builtin/update-index.c +4 -4
index a92eed2..0665b31 100644
@@ -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);
Reviewed-by Eric Sunshine
#5 lore raw
Quoted 9 lines · Stefan Beller
Quoted 2 lines

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

That sounds good to me.

Quoted 5 lines

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

The return value from add_prefix() that is what prefix_path() returned eventually becomes scoreboard.path that needs to be kept during the lifetime of the process, and I think there isn't much point doing the "free() immediately before exiting".

Quoted 2 lines
Quoted 2 lines

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

Anyway, here is what I'd queue for now.

-- >8 --
From: Stefan Beller <[email protected]>
Date: Mon, 4 May 2015 12:11:54 -0700
Subject: [PATCH] prefix_path(): unconditionally free results in the callers

As of d089ebaa (setup: sanitize absolute and funny paths in get_pathspec(), 2008-01-28), prefix_path() always returns a newly allocated string, so callers should free its result.

Additionally, drop the const from variables to which the result of the prefix_path() is assigned, so they can be free()'d without having to cast-away the constness.

---
 builtin/checkout-index.c | 10 ++++------
 builtin/update-index.c   | 13 ++++++-------
 2 files changed, 10 insertions(+), 13 deletions(-)
builtin/checkout-index.c +4 -6
index 9ca2da1..8028c37 100644
@@ -241,7 +241,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 	/* Check out named files first */
 	for (i = 0; i < argc; i++) {
 		const char *arg = argv[i];
-		const char *p;
+		char *p;
 
 		if (all)
 			die("git checkout-index: don't mix '--all' and explicit filenames");
@@ -249,8 +249,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 			die("git checkout-index: don't mix '--stdin' and explicit filenames");
 		p = prefix_path(prefix, prefix_length, arg);
 		checkout_file(p, prefix);
-		if (p < arg || p > arg + strlen(arg))
-			free((char *)p);
+		free(p);
 	}
 
 	if (read_from_stdin) {
@@ -260,7 +259,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 			die("git checkout-index: don't mix '--all' and '--stdin'");
 
 		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))
@@ -269,8 +268,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 			}
 			p = prefix_path(prefix, prefix_length, buf.buf);
 			checkout_file(p, prefix);
-			if (p < buf.buf || p > buf.buf + buf.len)
-				free((char *)p);
+			free(p);
 		}
 		strbuf_release(&nbuf);
 		strbuf_release(&buf);
builtin/update-index.c +6 -7
index 6271b54..0665b31 100644
@@ -532,10 +532,9 @@ static int do_unresolve(int ac, const char **av,
 
 	for (i = 1; i < ac; i++) {
 		const char *arg = av[i];
-		const char *p = prefix_path(prefix, prefix_length, arg);
+		char *p = prefix_path(prefix, prefix_length, arg);
 		err |= unresolve_one(p);
-		if (p < arg || p > arg + strlen(arg))
-			free((char *)p);
+		free(p);
 	}
 	return err;
 }
@@ -871,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;
@@ -909,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))
@@ -920,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);
Signature · 1 line
2.4.0-311-gf1d9b8d
#6 lore raw
Quoted 9 lines · Junio C Hamano
Quoted 7 lines · Stefan Beller
Quoted 2 lines

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

That sounds good to me.

Quoted 3 lines

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

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines

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

The return value from add_prefix() that is what prefix_path() returned eventually becomes scoreboard.path that needs to be kept during the lifetime of the process, and I think there isn't much point doing the "free() immediately before exiting".

Yeah, sorry, I meant to say that more explicitly, but clearly didn't. I think it is fine as-is.

Quoted 1 line

Anyway, here is what I'd queue for now.

Looks good, thanks.

-Peff

#7 lore raw
Quoted 10 lines · Jeff King
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines

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

The return value from add_prefix() that is what prefix_path() returned eventually becomes scoreboard.path that needs to be kept during the lifetime of the process, and I think there isn't much point doing the "free() immediately before exiting".

Yeah, sorry, I meant to say that more explicitly, but clearly didn't. I think it is fine as-is.

Yeah, sorry, I didn't mean "You are wrong and here is why". I was merely agreeing with you.

Thanks for running grep over the codebase to check possible remaining problems.