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

[PATCH 1/4] fast-import: use xsnprintf for writing sha1s

From
Jeff King <peff@peff.net>
Date
Mar 24, 2017, 17:25 UTC
Message-ID
<20170324172522.l4sawf2u42mrnswk@sigill.intra.peff.net>
In-Reply-To
<20170324172246.fy5drvhzqxghu44a@sigill.intra.peff.net>

When we have to write a sha1 with a newline, we do so by copying both into a single buffer, so that we can issue a single write() call.

We use snprintf but don't bother to check the output, since we know it will fit. However, we should use xsnprintf() in such a case so that we're notified if our assumption turns out to be wrong (and to make it easier to audit for unchecked snprintf calls).

Signed-off-by: Jeff King <peff@peff.net>
---
This is ready for conversion to oid_to_hex, too, but I avoided that
here. The buffer would need to be allocated with the new GIT_MAX_HEXSZ,
which is not yet available. So I figured it was better to leave it than
half-convert it and leave brian wondering whether it's really supposed
to be GIT_MAX_HEXSZ or GIT_SHA1_HEXSZ.
 fast-import.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/fast-import.c b/fast-import.c
index 41a539f97..4e0f3f5dd 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -3003,7 +3003,7 @@ static void parse_get_mark(const char *p)
 	if (!oe)
 		die("Unknown mark: %s", command_buf.buf);
 
-	snprintf(output, sizeof(output), "%s\n", sha1_to_hex(oe->idx.sha1));
+	xsnprintf(output, sizeof(output), "%s\n", sha1_to_hex(oe->idx.sha1));
 	cat_blob_write(output, 41);
 }
 
-- 
2.12.1.843.g1937c56c2
Previous: Jeff KingNext: Jeff King
Message 2 of 5 in “a few minor buffer cleanups in fast-import”
  1. 0/4 a few minor buffer cleanups in fast-importJeff King, Mar 24, 2017
  2. 1/4 fast-import: use xsnprintf for writing sha1sJeff King, Mar 24, 2017
  3. 4/4 pack.h: define largest possible encoded object sizeJeff King, Mar 24, 2017
  4. 3/4 encode_in_pack_object_header: respect output buffer lengthJeff King, Mar 24, 2017
  5. 2/4 fast-import: use xsnprintf for formatting headersJeff King, Mar 24, 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.