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

[PATCH] fsck: give accurate error message on empty loose object files

From
Matthieu Moy <matthieu.moy@imag.fr>
Date
Feb 6, 2012, 16:24 UTC
Message-ID
<1328545492-13429-1-git-send-email-Matthieu.Moy@imag.fr>
In-Reply-To
<vpqfwf6en6e.fsf@bauges.imag.fr>

Since 3ba7a065527a (A loose object is not corrupt if it cannot be read due to EMFILE), "git fsck" on a repository with an empty loose object file complains with the error message

  fatal: failed to read object <sha1>: Invalid argument

This comes from a failure of mmap on this empty file, which sets errno to EINVAL. Instead of calling xmmap on empty file, we display a clean error message ourselves, and return a NULL pointer. The new message is

  error: object file .git/objects/09/<rest-of-sha1> is empty
  fatal: loose object <sha1> (stored in .git/objects/09/<rest-of-sha1>) is corrupt

The second line was already there before the regression in 3ba7a065527a, and the first is an additional message, that should help diagnosing the problem for the user.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---
 sha1_file.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
diff --git a/sha1_file.c b/sha1_file.c
index 88f2151..fafc187 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -1202,6 +1202,11 @@ void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
 
 		if (!fstat(fd, &st)) {
 			*size = xsize_t(st.st_size);
+			if (*size == 0) {
+				/* mmap() is forbidden on empty files */
+				error("object file %s is empty", sha1_file_name(sha1));
+				return NULL;
+			}
 			map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
 		}
 		close(fd);
-- 
1.7.9.111.gf3fb0.dirty
Previous: Matthieu Moy
Message 2 of 2 in “[BUG] incorrect error message in "git fsck" for empty loose objects”
  1. Matthieu MoyJan 23, 2012
  2. fsck: give accurate error message on empty loose object filesMatthieu Moy, Feb 6, 2012

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.