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

[PATCH 1/3] read_packed_refs: use a strbuf for reading lines

From
Jeff King <peff@peff.net>
Date
Dec 10, 2014, 10:40 UTC
Message-ID
<20141210104007.GA24514@peff.net>
In-Reply-To
<20141210103907.GA22186@peff.net>

We currently used a fixed PATH_MAX-sized buffer for reading packed-refs lines. This is a reasonable guess, in the sense that git generally cannot work with refs larger than PATH_MAX. However, there are a few cases where it is not great:

  1. Some systems may have a low value of PATH_MAX, but can
     actually handle larger paths in practice. Fixing this
     code path probably isn't enough to make them work
     completely with long refs, but it is a step in the
     right direction.
  2. We use fgets, which will happily give us half a line on
     the first read, and then the rest of the line on the
     second. This is probably OK in practice, because our
     refline parser is careful enough to look for the
     trailing newline on the first line. The second line may
     look like a peeled line to us, but since "^" is illegal
     in refnames, it is not likely to come up.
     Still, it does not hurt to be more careful.
Signed-off-by: Jeff King <peff@peff.net>
---
 refs.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
diff --git a/refs.c b/refs.c
index 5ff457e..6f31935 100644
--- a/refs.c
+++ b/refs.c
@@ -1126,16 +1126,16 @@ static const char *parse_ref_line(char *line, unsigned char *sha1)
 static void read_packed_refs(FILE *f, struct ref_dir *dir)
 {
 	struct ref_entry *last = NULL;
-	char refline[PATH_MAX];
+	struct strbuf line = STRBUF_INIT;
 	enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
 
-	while (fgets(refline, sizeof(refline), f)) {
+	while (strbuf_getwholeline(&line, f, '\n') != EOF) {
 		unsigned char sha1[20];
 		const char *refname;
 		static const char header[] = "# pack-refs with:";
 
-		if (!strncmp(refline, header, sizeof(header)-1)) {
-			const char *traits = refline + sizeof(header) - 1;
+		if (!strncmp(line.buf, header, sizeof(header)-1)) {
+			const char *traits = line.buf + sizeof(header) - 1;
 			if (strstr(traits, " fully-peeled "))
 				peeled = PEELED_FULLY;
 			else if (strstr(traits, " peeled "))
@@ -1144,7 +1144,7 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
 			continue;
 		}
 
-		refname = parse_ref_line(refline, sha1);
+		refname = parse_ref_line(line.buf, sha1);
 		if (refname) {
 			int flag = REF_ISPACKED;
 
@@ -1160,10 +1160,10 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
 			continue;
 		}
 		if (last &&
-		    refline[0] == '^' &&
-		    strlen(refline) == PEELED_LINE_LENGTH &&
-		    refline[PEELED_LINE_LENGTH - 1] == '\n' &&
-		    !get_sha1_hex(refline + 1, sha1)) {
+		    line.buf[0] == '^' &&
+		    line.len == PEELED_LINE_LENGTH &&
+		    line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
+		    !get_sha1_hex(line.buf + 1, sha1)) {
 			hashcpy(last->u.value.peeled, sha1);
 			/*
 			 * Regardless of what the file header said,
@@ -1173,6 +1173,8 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
 			last->flag |= REF_KNOWS_PEELED;
 		}
 	}
+
+	strbuf_release(&line);
 }
 
 /*
-- 
2.2.0.454.g7eca6b7
Previous: Jeff KingNext: Jeff King
Message 11 of 20 in “pkt-line: allow writing of LARGE_PACKET_MAX buffers”
  1. pkt-line: allow writing of LARGE_PACKET_MAX buffersJeff King, Dec 9, 2014
  2. Jeff KingDec 9, 2014
  3. Junio C HamanoDec 9, 2014
  4. Michael BlumeDec 10, 2014
  5. pkt-line: allow writing of LARGE_PACKET_MAX buffersJeff King, Dec 10, 2014
  6. Eric SunshineDec 10, 2014
  7. Eric SunshineDec 10, 2014
  8. Eric SunshineDec 10, 2014
  9. Jeff KingDec 10, 2014
  10. 0/3 convert read_packed_refs to use strbufJeff King, Dec 10, 2014
  11. 1/3 read_packed_refs: use a strbuf for reading linesJeff King, Dec 10, 2014
  12. 2/3 read_packed_refs: pass strbuf to parse_ref_lineJeff King, Dec 10, 2014
  13. 3/3 read_packed_refs: use skip_prefix instead of static arrayJeff King, Dec 10, 2014
  14. Junio C HamanoDec 10, 2014
  15. pkt-line: allow writing of LARGE_PACKET_MAX buffersJeff King, Dec 10, 2014
  16. Eric SunshineDec 10, 2014
  17. Eric SunshineDec 10, 2014
  18. Jeff KingDec 10, 2014
  19. Johannes SixtDec 9, 2014
  20. Jeff KingDec 9, 2014

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.