Commit 38b6e700 by Patrick Steinhardt

fileops: fix leaking fd in `mmap_ro_file`

When the `git_futils_mmap_ro_file` function encounters an error after
the file has been opened, it will do a simple returns. Instead, we
should close the opened file descriptor to avoid a leak. This commit
fixes the issue.
parent e572b631
...@@ -304,15 +304,19 @@ int git_futils_mmap_ro_file(git_map *out, const char *path) ...@@ -304,15 +304,19 @@ int git_futils_mmap_ro_file(git_map *out, const char *path)
if (fd < 0) if (fd < 0)
return fd; return fd;
if ((len = git_futils_filesize(fd)) < 0) if ((len = git_futils_filesize(fd)) < 0) {
return -1; result = -1;
goto out;
}
if (!git__is_sizet(len)) { if (!git__is_sizet(len)) {
giterr_set(GITERR_OS, "file `%s` too large to mmap", path); giterr_set(GITERR_OS, "file `%s` too large to mmap", path);
return -1; result = -1;
goto out;
} }
result = git_futils_mmap_ro(out, fd, 0, (size_t)len); result = git_futils_mmap_ro(out, fd, 0, (size_t)len);
out:
p_close(fd); p_close(fd);
return result; return result;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment