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

[PATCH] sha1_file.c: make sure open_sha1_file does not open a directory

From
Kyle J. McKay <mackyle@gmail.com>
Date
Feb 8, 2015, 23:05 UTC
Message-ID
<b0993cc1fcac290d7506b24942af300@74d39fa044aa309eaea14b9f57fe79c>

Since "sha1_file: fix iterating loose alternate objects", it's possible for the base member of the alt_odb_list structure to be NUL terminated rather than ending with a '/' when open_sha1_file is called.

Unfortunately this causes a directory to be passed to git_open_noatime instead of a file which it happily opens and returns whereupon this open directory file handle is then passed to mmap.

mmap does not like this and fails with an invalid argument error at which point the pack-objects process dies prematurely.

To avoid this we preserve the last character of the base member, set it to '/', make the git_open_noatime call and then restore it to its previous value which allows pack-objects to function properly.

Signed-off-by: Kyle J. McKay <mackyle@gmail.com>
---
*****

While this patch can be applied without "sha1_file: fix iterating loose alternate objects" you cannot even get to the failure this fixes without first having that patch applied.

All this stuffing of a single char back and forth into a [-1] index seems awfully kludgy to me.

However, without this fix and the previous sha1_file fix I would need to jettison the latest stuff and revert back to 2.1.4.

I suggest that this (or another fix for the problem) go into maint together with the "sha1_file: fix iterating loose alternate objects" fix.

*****
 sha1_file.c | 3 +++
 1 file changed, 3 insertions(+)
diff --git a/sha1_file.c b/sha1_file.c
index f575b3cf..235f6ad8 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -1491,8 +1491,11 @@ static int open_sha1_file(const unsigned char *sha1)
 
 	prepare_alt_odb();
 	for (alt = alt_odb_list; alt; alt = alt->next) {
+		char c = alt->name[-1];
+		alt->name[-1] = '/';
 		fill_sha1_path(alt->name, sha1);
 		fd = git_open_noatime(alt->base);
+		alt->name[-1] = c;
 		if (fd >= 0)
 			return fd;
 		if (most_interesting_errno == ENOENT)
--
Next: Jeff King
Message 1 of 7 in “sha1_file.c: make sure open_sha1_file does not open a directory”
  1. sha1_file.c: make sure open_sha1_file does not open a directoryKyle J. McKay, Feb 8, 2015
  2. Jeff KingFeb 9, 2015
  3. Jeff KingFeb 9, 2015
  4. 1/2 for_each_loose_file_in_objdir: take an optional strbuf pathJeff King, Feb 9, 2015
  5. 2/2 sha1_file: fix iterating loose alternate objectsJeff King, Feb 9, 2015
  6. Kyle J. McKayFeb 9, 2015
  7. Junio C HamanoFeb 9, 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.