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

Re: [PATCH 3/3] write_sha1_file_prepare: fix buffer overrun with extra-long object type

From
Junio C Hamano <gitster@pobox.com>
Date
May 4, 2015, 17:59 UTC
Message-ID
<xmqqzj5krzpe.fsf@gitster.dls.corp.google.com>
In-Reply-To
<xmqq4mnsteb8.fsf@gitster.dls.corp.google.com>

By the way, you would notice that "if we have returnsha1, then copy that in" bit is not in the new literally codepath but still is in write_sha1_file().

I do not think any caller passes a NULL as return_sha1 in today's code, which made me curious.

It turns out to be a remnant of d6d3f9d0 (This implements the new "recursive tree" write-tree., 2005-04-09); before that change, write_sha1_file() did not have an ability to tell the caller what object it wrote, and Linus made it optional for the callers when he added the return_sha1[] out parameter, but all of its callers did want the resulting object name.

So I think it is safe and sensible to do the following change regardless of "hash-object --literally" fix.

 sha1_file.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/sha1_file.c b/sha1_file.c
index c8ab069..96e813f 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -3002,9 +3002,8 @@ static int freshen_packed_object(const unsigned char *sha1)
 	return find_pack_entry(sha1, &e) && freshen_file(e.p->pack_name);
 }
 
-int write_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
+int write_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *sha1)
 {
-	unsigned char sha1[20];
 	char hdr[32];
 	int hdrlen;
 
@@ -3012,8 +3011,6 @@ int write_sha1_file(const void *buf, unsigned long len, const char *type, unsign
 	 * it out into .git/objects/??/?{38} file.
 	 */
 	write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
-	if (returnsha1)
-		hashcpy(returnsha1, sha1);
 	if (freshen_loose_object(sha1) || freshen_packed_object(sha1))
 		return 0;
 	return write_loose_object(sha1, hdr, hdrlen, buf, len, 0);
Previous: Junio C HamanoNext: Junio C Hamano
Message 6 of 15 in “hash-object crash fix and new doc and tests”
  1. 0/3 hash-object crash fix and new doc and testsEric Sunshine, May 4, 2015
  2. 1/3 git-hash-object.txt: document --literally optionEric Sunshine, May 4, 2015
  3. 2/3 t1007: add hash-object --literally testsEric Sunshine, May 4, 2015
  4. 3/3 write_sha1_file_prepare: fix buffer overrun with extra-long object typeEric Sunshine, May 4, 2015
  5. Junio C HamanoMay 4, 2015
  6. Junio C HamanoMay 4, 2015
  7. 0/4 "hash-object --literally" fixesJunio C Hamano, May 4, 2015
  8. 1/4 git-hash-object.txt: document --literally optionJunio C Hamano, May 4, 2015
  9. 2/4 write_sha1_file_prepare: fix buffer overrun with extra-long object typeJunio C Hamano, May 4, 2015
  10. Eric SunshineMay 5, 2015
  11. Junio C HamanoMay 5, 2015
  12. Junio C HamanoMay 5, 2015
  13. Eric SunshineMay 5, 2015
  14. 3/4 t1007: add hash-object --literally testsJunio C Hamano, May 4, 2015
  15. 4/4 write_sha1_file(): do not use a separate sha1[] arrayJunio C Hamano, May 4, 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.