Skip to content

Commit 01caf20

Browse files
mhaggergitster
authored andcommitted
load_contents(): don't try to mmap an empty file
We don't actually create zero-length `packed-refs` files, but they are valid and we should handle them correctly. The old code `xmmap()`ed such files, which led to an error when `munmap()` was called. So, if the `packed-refs` file is empty, leave the snapshot at its zero values and return 0 without trying to read or mmap the file. Returning 0 also makes `create_snapshot()` exit early, which avoids the technically undefined comparison `NULL < NULL`. Reported-by: Kim Gybels <[email protected]> Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f342429 commit 01caf20

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

refs/packed-backend.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,8 @@ static void verify_buffer_safe(struct snapshot *snapshot)
461461
/*
462462
* Depending on `mmap_strategy`, either mmap or read the contents of
463463
* the `packed-refs` file into the snapshot. Return 1 if the file
464-
* existed and was read, or 0 if the file was absent. Die on errors.
464+
* existed and was read, or 0 if the file was absent or empty. Die on
465+
* errors.
465466
*/
466467
static int load_contents(struct snapshot *snapshot)
467468
{
@@ -492,19 +493,17 @@ static int load_contents(struct snapshot *snapshot)
492493
die_errno("couldn't stat %s", snapshot->refs->path);
493494
size = xsize_t(st.st_size);
494495

495-
switch (mmap_strategy) {
496-
case MMAP_NONE:
496+
if (!size) {
497+
return 0;
498+
} else if (mmap_strategy == MMAP_NONE) {
497499
snapshot->buf = xmalloc(size);
498500
bytes_read = read_in_full(fd, snapshot->buf, size);
499501
if (bytes_read < 0 || bytes_read != size)
500502
die_errno("couldn't read %s", snapshot->refs->path);
501503
snapshot->mmapped = 0;
502-
break;
503-
case MMAP_TEMPORARY:
504-
case MMAP_OK:
504+
} else {
505505
snapshot->buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
506506
snapshot->mmapped = 1;
507-
break;
508507
}
509508
close(fd);
510509

0 commit comments

Comments
 (0)