[BUG] incorrect error message in "git fsck" for empty loose objects

2 messages, 2 authors, 2016-06-15 · open the first message on its own page

[BUG] incorrect error message in "git fsck" for empty loose objects

From: Matthieu Moy <hidden>
Date: 2016-06-15 22:52:51

Hi,

If a loose object is empty, "git fsck" reports it as corrupt (which is
good), but with the following error message:

$ git fsck
Checking object directories: 100% (256/256), done.
fatal: failed to read object c1738f6288c9e5d5e58da00ced34d284ae93976c: Invalid argument
$ cat .git/objects/c1/738f6288c9e5d5e58da00ced34d284ae93976c 
$ ls -l .git/objects/c1/738f6288c9e5d5e58da00ced34d284ae93976c
-r--r--r-- 1 moy synchron 0 Jan 20 16:20 .git/objects/c1/738f6288c9e5d5e58da00ced34d284ae93976c

"Invalid argument" is really not the error message one would expect.
Before that, git used to say:

   fatal: loose object c1738f6288c9e5d5e58da00ced34d284ae93976c (stored in
   .git/objects/c1/738f6288c9e5d5e58da00ced34d28
   4ae93976c) is corrupt

Which was far better.

This bisects back to 3ba7a065527a (A loose object is not corrupt if it
cannot be read due to EMFILE), which essentially boils down to:
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -2090,16 +2090,21 @@ void *read_sha1_file_repl(const unsigned char *sha1,
                          const unsigned char **replacement)
[...]
+       errno = 0;
+       data = read_object(repl, type, size);
        if (data) {
                if (replacement)
                        *replacement = repl;
                return data;
        }
 
+       if (errno != ENOENT)
+               die_errno("failed to read object %s", sha1_to_hex(sha1));
+

Thanks,

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

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

From: Matthieu Moy <hidden>
Date: 2016-06-15 22:53:00

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 <redacted>
---
 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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help