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

[PATCH 2/3] validate_headref: use skip_prefix for symref parsing

From
Jeff King <peff@peff.net>
Date
Sep 27, 2017, 06:17 UTC
Message-ID
<20170927061726.gat3ykqx4fdstzq6@sigill.intra.peff.net>
In-Reply-To
<20170927061644.gwlhxaiairiy2njp@sigill.intra.peff.net>

Since the previous commit guarantees that our symref buffer is NUL-terminated, we can just use skip_prefix() and friends to parse it. This is shorter and saves us having to deal with magic numbers and keeping the "len" counter up to date.

While we're at it, let's name the rather obscure "buf" to "refname", since that is the thing we are parsing with it.

Signed-off-by: Jeff King <peff@peff.net>
---
 path.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
diff --git a/path.c b/path.c
index 3e4d7505ef..2ef3a86613 100644
--- a/path.c
+++ b/path.c
@@ -637,7 +637,8 @@ void strbuf_git_common_path(struct strbuf *sb,
 int validate_headref(const char *path)
 {
 	struct stat st;
-	char *buf, buffer[256];
+	char buffer[256];
+	const char *refname;
 	unsigned char sha1[20];
 	int fd;
 	ssize_t len;
@@ -669,14 +670,10 @@ int validate_headref(const char *path)
 	/*
 	 * Is it a symbolic ref?
 	 */
-	if (len < 4)
-		return -1;
-	if (!memcmp("ref:", buffer, 4)) {
-		buf = buffer + 4;
-		len -= 4;
-		while (len && isspace(*buf))
-			buf++, len--;
-		if (len >= 5 && !memcmp("refs/", buf, 5))
+	if (skip_prefix(buffer, "ref:", &refname)) {
+		while (isspace(*refname))
+			refname++;
+		if (starts_with(refname, "refs/"))
 			return 0;
 	}
 
-- 
2.14.2.988.g01c8b37dde
Previous: Junio C HamanoNext: Junio C Hamano
Message 6 of 8 in “validate_headref: avoid reading uninitialized bytes”
  1. 0/3 validate_headref: avoid reading uninitialized bytesJeff King, Sep 27, 2017
  2. 1/3 validate_headref: NUL-terminate HEAD bufferJeff King, Sep 27, 2017
  3. Junio C HamanoSep 27, 2017
  4. Jeff KingSep 27, 2017
  5. Junio C HamanoSep 27, 2017
  6. 2/3 validate_headref: use skip_prefix for symref parsingJeff King, Sep 27, 2017
  7. Junio C HamanoSep 27, 2017
  8. 3/3 validate_headref: use get_oid_hex for detached HEADsJeff King, Sep 27, 2017

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.