Skip to content

Commit 08bda20

Browse files
dmpotgitster
authored andcommitted
hash_object: correction for zero length file
The check whether size is zero was done after if size <= SMALL_FILE_SIZE, as result, zero size case was never triggered. Instead zero length file was treated as any other small file. This did not caused any problem, but if we have a special case for size equal to zero, it is better to make it work and avoid redundant malloc(). Signed-off-by: Dmitry Potapov <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3368edd commit 08bda20

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

sha1_file.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2448,6 +2448,8 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
24482448
else
24492449
ret = -1;
24502450
strbuf_release(&sbuf);
2451+
} else if (!size) {
2452+
ret = index_mem(sha1, NULL, size, write_object, type, path);
24512453
} else if (size <= SMALL_FILE_SIZE) {
24522454
char *buf = xmalloc(size);
24532455
if (size == read_in_full(fd, buf, size))
@@ -2456,12 +2458,11 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
24562458
else
24572459
ret = error("short read %s", strerror(errno));
24582460
free(buf);
2459-
} else if (size) {
2461+
} else {
24602462
void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
24612463
ret = index_mem(sha1, buf, size, write_object, type, path);
24622464
munmap(buf, size);
2463-
} else
2464-
ret = index_mem(sha1, NULL, size, write_object, type, path);
2465+
}
24652466
close(fd);
24662467
return ret;
24672468
}

0 commit comments

Comments
 (0)