{"thread":{"id":"29428","subject":"[BUG] incorrect error message in \"git fsck\" for empty loose objects","startedAt":"2012-01-23T10:19:21Z","lastAt":"2012-02-06T16:24:52Z","messageCount":2,"participants":["Matthieu Moy"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"182957","messageId":"vpqfwf6en6e.fsf@bauges.imag.fr","threadId":"29428","inReplyTo":null,"subject":"[BUG] incorrect error message in \"git fsck\" for empty loose objects","fromName":"Matthieu Moy","fromEmail":"matthieu.moy@grenoble-inp.fr","sentAt":"2012-01-23T10:19:21Z","receivedAt":"2012-01-23T10:19:21Z","isPatch":false,"sender":{"key":"matthieu.moy@grenoble-inp.fr","avatar":"https://gravatar.com/avatar/72c8a2705971a25dfaff23cece15130d405685845d911aedd5667ace277f3fc5?d=mp&s=160"},"body":"Hi,\n\nIf a loose object is empty, \"git fsck\" reports it as corrupt (which is\ngood), but with the following error message:\n\n$ git fsck\nChecking object directories: 100% (256/256), done.\nfatal: failed to read object c1738f6288c9e5d5e58da00ced34d284ae93976c: Invalid argument\n$ cat .git/objects/c1/738f6288c9e5d5e58da00ced34d284ae93976c \n$ ls -l .git/objects/c1/738f6288c9e5d5e58da00ced34d284ae93976c\n-r--r--r-- 1 moy synchron 0 Jan 20 16:20 .git/objects/c1/738f6288c9e5d5e58da00ced34d284ae93976c\n\n\"Invalid argument\" is really not the error message one would expect.\nBefore that, git used to say:\n\n   fatal: loose object c1738f6288c9e5d5e58da00ced34d284ae93976c (stored in\n   .git/objects/c1/738f6288c9e5d5e58da00ced34d28\n   4ae93976c) is corrupt\n\nWhich was far better.\n\nThis bisects back to 3ba7a065527a (A loose object is not corrupt if it\ncannot be read due to EMFILE), which essentially boils down to:\n\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -2090,16 +2090,21 @@ void *read_sha1_file_repl(const unsigned char *sha1,\n                          const unsigned char **replacement)\n[...]\n+       errno = 0;\n+       data = read_object(repl, type, size);\n        if (data) {\n                if (replacement)\n                        *replacement = repl;\n                return data;\n        }\n \n+       if (errno != ENOENT)\n+               die_errno(\"failed to read object %s\", sha1_to_hex(sha1));\n+\n\nThanks,\n\n-- \nMatthieu Moy\nhttp://www-verimag.imag.fr/~moy/\n"},{"id":"184032","messageId":"1328545492-13429-1-git-send-email-Matthieu.Moy@imag.fr","threadId":"29428","inReplyTo":"vpqfwf6en6e.fsf@bauges.imag.fr","subject":"[PATCH] fsck: give accurate error message on empty loose object files","fromName":"Matthieu Moy","fromEmail":"matthieu.moy@imag.fr","sentAt":"2012-02-06T16:24:52Z","receivedAt":"2012-02-06T16:24:52Z","isPatch":true,"sender":{"key":"git@matthieu-moy.fr","avatar":"https://avatars.githubusercontent.com/u/14709?v=4"},"body":"Since 3ba7a065527a (A loose object is not corrupt if it\ncannot be read due to EMFILE), \"git fsck\" on a repository with an empty\nloose object file complains with the error message\n\n  fatal: failed to read object <sha1>: Invalid argument\n\nThis comes from a failure of mmap on this empty file, which sets errno to\nEINVAL. Instead of calling xmmap on empty file, we display a clean error\nmessage ourselves, and return a NULL pointer. The new message is\n\n  error: object file .git/objects/09/<rest-of-sha1> is empty\n  fatal: loose object <sha1> (stored in .git/objects/09/<rest-of-sha1>) is corrupt\n\nThe second line was already there before the regression in 3ba7a065527a,\nand the first is an additional message, that should help diagnosing the\nproblem for the user.\n\nSigned-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>\n---\n sha1_file.c |    5 +++++\n 1 files changed, 5 insertions(+), 0 deletions(-)\n\ndiff --git a/sha1_file.c b/sha1_file.c\nindex 88f2151..fafc187 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -1202,6 +1202,11 @@ void *map_sha1_file(const unsigned char *sha1, unsigned long *size)\n \n \t\tif (!fstat(fd, &st)) {\n \t\t\t*size = xsize_t(st.st_size);\n+\t\t\tif (*size == 0) {\n+\t\t\t\t/* mmap() is forbidden on empty files */\n+\t\t\t\terror(\"object file %s is empty\", sha1_file_name(sha1));\n+\t\t\t\treturn NULL;\n+\t\t\t}\n \t\t\tmap = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);\n \t\t}\n \t\tclose(fd);\n-- \n1.7.9.111.gf3fb0.dirty\n"}]}