{"thread":{"id":"23774","subject":"[PATCH] hash_object: correction for zero length file","startedAt":"2010-05-10T21:38:17Z","lastAt":"2010-05-10T21:38:17Z","messageCount":1,"participants":["Dmitry Potapov"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"141441","messageId":"20100510213817.GK14069@dpotapov.dyndns.org","threadId":"23774","inReplyTo":null,"subject":"[PATCH] hash_object: correction for zero length file","fromName":"Dmitry Potapov","fromEmail":"dpotapov@gmail.com","sentAt":"2010-05-10T21:38:17Z","receivedAt":"2010-05-10T21:38:17Z","isPatch":true,"sender":{"key":"dpotapov@gmail.com","avatar":"https://avatars.githubusercontent.com/u/6568595?v=4"},"body":"The check whether size is zero was done after if size <= SMALL_FILE_SIZE,\nas result, zero size case was never triggered. Instead zero length file\nwas treated as any other small file. This did not caused any problem, but\nif we have a special case for size equal to zero, it is better to make it\nwork and avoid redundant malloc().\n\nSigned-off-by: Dmitry Potapov <dpotapov@gmail.com>\n---\n sha1_file.c |    9 +++++----\n 1 files changed, 5 insertions(+), 4 deletions(-)\n\ndiff --git a/sha1_file.c b/sha1_file.c\nindex 28c056e..2ece9b6 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -2448,7 +2448,9 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,\n \t\telse\n \t\t\tret = -1;\n \t\tstrbuf_release(&sbuf);\n-\t} else if (size <= SMALL_FILE_SIZE) {\n+\t} else if (!size)\n+\t\tret = index_mem(sha1, NULL, size, write_object, type, path);\n+\telse if (size <= SMALL_FILE_SIZE) {\n \t\tchar *buf = xmalloc(size);\n \t\tif (size == read_in_full(fd, buf, size))\n \t\t\tret = index_mem(sha1, buf, size, write_object, type,\n@@ -2456,12 +2458,11 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,\n \t\telse\n \t\t\tret = error(\"short read %s\", strerror(errno));\n \t\tfree(buf);\n-\t} else if (size) {\n+\t} else {\n \t\tvoid *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);\n \t\tret = index_mem(sha1, buf, size, write_object, type, path);\n \t\tmunmap(buf, size);\n-\t} else\n-\t\tret = index_mem(sha1, NULL, size, write_object, type, path);\n+\t}\n \tclose(fd);\n \treturn ret;\n }\n-- \n1.7.1.83.g11b7c\n"}]}