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

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

From
Stefan Beller <sbeller@google.com>
Date
May 4, 2015, 19:11 UTC
Message-ID
<1430766714-22368-1-git-send-email-sbeller@google.com>

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 <sbeller@google.com>
---
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(-)
diff --git a/builtin/checkout-index.c b/builtin/checkout-index.c
index 9ca2da1..8028c37 100644
--- a/builtin/checkout-index.c
+++ b/builtin/checkout-index.c
@@ -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);
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 6271b54..a92eed2 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -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
Next: Eric Sunshine
Message 1 of 8 in “prefix_path(): Unconditionally free result of prefix_path”
  1. prefix_path(): Unconditionally free result of prefix_pathStefan Beller, May 4, 2015
  2. Eric SunshineMay 4, 2015
  3. Junio C HamanoMay 4, 2015
  4. Jeff KingMay 5, 2015
  5. Stefan BellerMay 5, 2015
  6. Junio C HamanoMay 5, 2015
  7. Jeff KingMay 5, 2015
  8. Junio C HamanoMay 5, 2015

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.