Skip to content


packed_ref_cache: don't use mmap() for small files
Browse files Browse the repository at this point in the history
Take a hint from commit ea68b0c (hash-object: don't use mmap() for
small files, 2010-02-21) and use read() instead of mmap() for small
packed-refs files.

This also fixes #1410
(where xmmap() returns NULL for zero length[1], for which munmap() later

Alternatively, we could simply check for NULL before munmap(), or
introduce xmunmap() that could be used together with xmmap(). However,
always setting snapshot->buf to a valid pointer, by relying on
xmalloc(0)'s fallback to 1-byte allocation, makes using snapshots

[1] Logic introduced in commit 9130ac1 (Better error messages for
    corrupt databases, 2007-01-11)

This was cherry-picked from upstream's `pu` branch so that the fix is
included in Git for Windows v2.16.0.

Signed-off-by: Kim Gybels <>
Signed-off-by: Junio C Hamano <>
Signed-off-by: Johannes Schindelin <>
  • Loading branch information
kgybels authored and dscho committed Jan 17, 2018
1 parent 90e90ed commit a56c4f9
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions refs/packed-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,8 @@ static void verify_buffer_safe(struct snapshot *snapshot)
last_line, eof - last_line);

#define SMALL_FILE_SIZE (32*1024)

* Depending on `mmap_strategy`, either mmap or read the contents of
* the `packed-refs` file into the snapshot. Return 1 if the file
Expand Down Expand Up @@ -489,21 +491,17 @@ static int load_contents(struct snapshot *snapshot)
die_errno("couldn't stat %s", snapshot->refs->path);
size = xsize_t(st.st_size);

switch (mmap_strategy) {
if (size <= SMALL_FILE_SIZE || mmap_strategy == MMAP_NONE) {
snapshot->buf = xmalloc(size);
bytes_read = read_in_full(fd, snapshot->buf, size);
if (bytes_read < 0 || bytes_read != size)
die_errno("couldn't read %s", snapshot->refs->path);
snapshot->eof = snapshot->buf + size;
snapshot->mmapped = 0;
case MMAP_OK:
} else {
snapshot->buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
snapshot->eof = snapshot->buf + size;
snapshot->mmapped = 1;

Expand Down

0 comments on commit a56c4f9

Please sign in to comment.