"create_snapshot" would try to munmap the file when the "mmap_strategy" is "MMAP_TEMPORARY". We also need to do this operation when checking the consistency of the "packed-refs" file. Create a new function "munmap_snapshot_if_temporary" to do above and change "create_snapshot" to align with the behavior. Suggested-by: Jeff King <peff@xxxxxxxx> Suggested-by: Patrick Steinhardt <ps@xxxxxx> Signed-off-by: shejialuo <shejialuo@xxxxxxxxx> --- refs/packed-backend.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/refs/packed-backend.c b/refs/packed-backend.c index e582227772..dd903db301 100644 --- a/refs/packed-backend.c +++ b/refs/packed-backend.c @@ -543,6 +543,23 @@ static int allocate_snapshot_buffer(struct snapshot *snapshot, int fd, struct st return 1; } +static void munmap_snapshot_if_temporary(struct snapshot *snapshot) +{ + if (mmap_strategy != MMAP_OK && snapshot->mmapped) { + /* + * We don't want to leave the file mmapped, so we are + * forced to make a copy now: + */ + size_t size = snapshot->eof - snapshot->start; + char *buf_copy = xmalloc(size); + + memcpy(buf_copy, snapshot->start, size); + clear_snapshot_buffer(snapshot); + snapshot->buf = snapshot->start = buf_copy; + snapshot->eof = buf_copy + size; + } +} + /* * Depending on `mmap_strategy`, either mmap or read the contents of * the `packed-refs` file into the snapshot. Return 1 if the file @@ -761,19 +778,7 @@ static struct snapshot *create_snapshot(struct packed_ref_store *refs) verify_buffer_safe(snapshot); } - if (mmap_strategy != MMAP_OK && snapshot->mmapped) { - /* - * We don't want to leave the file mmapped, so we are - * forced to make a copy now: - */ - size_t size = snapshot->eof - snapshot->start; - char *buf_copy = xmalloc(size); - - memcpy(buf_copy, snapshot->start, size); - clear_snapshot_buffer(snapshot); - snapshot->buf = snapshot->start = buf_copy; - snapshot->eof = buf_copy + size; - } + munmap_snapshot_if_temporary(snapshot); return snapshot; } -- 2.49.0