From: "René Scharfe" <l.s.r@web.de>
To: Git List <git@vger.kernel.org>
Cc: Matthieu Moy <Matthieu.Moy@grenoble-inp.fr>,
Junio C Hamano <gitster@pobox.com>
Subject: [PATCH] sha1-file: close fd of empty file in map_sha1_file_1()
Date: Mon, 7 Jan 2019 17:48:02 +0100 [thread overview]
Message-ID: <1136813c-2f14-042a-858e-2bca1aba990f@web.de> (raw)
map_sha1_file_1() checks if the file it is about to mmap() is empty and
errors out in that case and explains the situation in an error message.
It leaks the private handle to that empty file, though.
Have the function clean up after itself and close the file descriptor
before exiting early.
Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
Patch generated with --function-context for easier review.
sha1-file.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/sha1-file.c b/sha1-file.c
index 5a272f70de..bfa7a2e121 100644
--- a/sha1-file.c
+++ b/sha1-file.c
@@ -942,31 +942,32 @@ static int quick_has_loose(struct repository *r,
/*
* Map the loose object at "path" if it is not NULL, or the path found by
* searching for a loose object named "sha1".
*/
static void *map_sha1_file_1(struct repository *r, const char *path,
const unsigned char *sha1, unsigned long *size)
{
void *map;
int fd;
if (path)
fd = git_open(path);
else
fd = open_sha1_file(r, sha1, &path);
map = NULL;
if (fd >= 0) {
struct stat st;
if (!fstat(fd, &st)) {
*size = xsize_t(st.st_size);
if (!*size) {
/* mmap() is forbidden on empty files */
error(_("object file %s is empty"), path);
+ close(fd);
return NULL;
}
map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
}
close(fd);
}
return map;
}
--
2.20.1
reply other threads:[~2019-01-07 16:48 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1136813c-2f14-042a-858e-2bca1aba990f@web.de \
--to=l.s.r@web.de \
--cc=Matthieu.Moy@grenoble-inp.fr \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).