Skip to contentSkip to conversation

archive: simplify refname handling

PATCH In review René Scharfe 4 participants 5 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 archive: simplify refname handling

There is no need to build a copy of the relevant part of the string just to make sure we have a NUL-terminated string. We can simply pass the length of the interesting part to dwim_ref() instead.

---
 archive.c |   17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)
archive.c +5 -12
index cd083ea..ecc1ff0 100644
@@ -254,18 +254,11 @@ static void parse_treeish_arg(const char **argv,
 	/* Remotes are only allowed to fetch actual refs */
 	if (remote) {
 		char *ref = NULL;
-		const char *refname, *colon = NULL;
-
-		colon = strchr(name, ':');
-		if (colon)
-			refname = xstrndup(name, colon - name);
-		else
-			refname = name;
-
-		if (!dwim_ref(refname, strlen(refname), sha1, &ref))
-			die("no such ref: %s", refname);
-		if (refname != name)
-			free((void *)refname);
+		const char *colon = strchr(name, ':');
+		size_t refnamelen = colon ? colon - name : strlen(name);
+
+		if (!dwim_ref(name, refnamelen, sha1, &ref))
+			die("no such ref: %.*s", refnamelen, name);
 		free(ref);
 	}
 
Signature · 1 line
1.7.10.2
#1 lore raw

[+cc Carlos, as this is his code]

Quoted 25 lines · René Scharfe

There is no need to build a copy of the relevant part of the string just to make sure we have a NUL-terminated string. We can simply pass the length of the interesting part to dwim_ref() instead.

---
 archive.c |   17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)
archive.c +0 -8
index cd083ea..ecc1ff0 100644
@@ -254,18 +254,11 @@ static void parse_treeish_arg(const char **argv,
 	/* Remotes are only allowed to fetch actual refs */
 	if (remote) {
 		char *ref = NULL;
-		const char *refname, *colon = NULL;
-
-		colon = strchr(name, ':');
-		if (colon)
-			refname = xstrndup(name, colon - name);
-		else
-			refname = name;
-

archive.c static void parse_treeish_arg(const char **argv, · line 262

Quoted 10 lines
archive.c +5 -4
-		if (!dwim_ref(refname, strlen(refname), sha1, &ref))
-			die("no such ref: %s", refname);
-		if (refname != name)
-			free((void *)refname);
+		const char *colon = strchr(name, ':');
+		size_t refnamelen = colon ? colon - name : strlen(name);
+
+		if (!dwim_ref(name, refnamelen, sha1, &ref))
+			die("no such ref: %.*s", refnamelen, name);
 		free(ref);

Looks obviously correct to me.

-Peff

#2 lore raw
Quoted 33 lines · Jeff King

[+cc Carlos, as this is his code]

Quoted 29 lines · René Scharfe

There is no need to build a copy of the relevant part of the string just to make sure we have a NUL-terminated string. We can simply pass the length of the interesting part to dwim_ref() instead.

---
 archive.c |   17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)
archive.c +0 -12
index cd083ea..ecc1ff0 100644
@@ -254,18 +254,11 @@ static void parse_treeish_arg(const char **argv,
 	/* Remotes are only allowed to fetch actual refs */
 	if (remote) {
 		char *ref = NULL;
-		const char *refname, *colon = NULL;
-
-		colon = strchr(name, ':');
-		if (colon)
-			refname = xstrndup(name, colon - name);
-		else
-			refname = name;
-
-		if (!dwim_ref(refname, strlen(refname), sha1, &ref))
-			die("no such ref: %s", refname);
-		if (refname != name)
-			free((void *)refname);
Quoted 8 lines

archive.c static void parse_treeish_arg(const char **argv, · line 262

Quoted 6 lines
archive.c +5 -0
+		const char *colon = strchr(name, ':');
+		size_t refnamelen = colon ? colon - name : strlen(name);
+
+		if (!dwim_ref(name, refnamelen, sha1, &ref))
+			die("no such ref: %.*s", refnamelen, name);
 		free(ref);

Looks obviously correct to me.

Yep, that's clearly a much better way of doing it.

cmn

#4 lore raw
In reply to René Scharfe

Review

Quoted 7 lines · René Scharfe
(quoted diff) +5 -0
+		const char *colon = strchr(name, ':');
+		size_t refnamelen = colon ? colon - name : strlen(name);
+
+		if (!dwim_ref(name, refnamelen, sha1, &ref))
+			die("no such ref: %.*s", refnamelen, name);
 		free(ref);
 	}

Both dwim_ref()'s second parameter the vararg that corresponds to the length part of "%.*s" in die(), expect "int", not "size_t", so I needed this to get it compile.

 archive.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
archive.c +1 -1
index 081ecb5..1e7156d 100644
@@ -261,7 +261,7 @@ static void parse_treeish_arg(const char **argv,
 	if (remote) {
 		char *ref = NULL;
 		const char *colon = strchr(name, ':');
-		size_t refnamelen = colon ? colon - name : strlen(name);
+		int refnamelen = colon ? colon - name : strlen(name);
 
 		if (!dwim_ref(name, refnamelen, sha1, &ref))
 			die("no such ref: %.*s", refnamelen, name);