summaryrefslogtreecommitdiff
path: root/config.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2015-05-28 03:56:15 -0400
committerJunio C Hamano <gitster@pobox.com>2015-05-28 11:33:18 -0700
commit1570856b510e3722a3620063e7ba209106b75857 (patch)
tree80086c5f7772e866f92bffc6da99482c8b099a46 /config.c
parent3a1b3126ed5a0b51d5b1fdba827c92bf2acf5fc6 (diff)
downloadgit-1570856b510e3722a3620063e7ba209106b75857.tar.gz
config.c: avoid xmmap error messages
The config-writing code uses xmmap to map the existing config file, which will die if the map fails. This has two downsides: 1. The error message is not very helpful, as it lacks any context about the file we are mapping: $ mkdir foo $ git config --file=foo some.key value fatal: Out of memory? mmap failed: No such device 2. We normally do not die in this code path; instead, we'd rather report the error and return an appropriate exit status (which is part of the public interface documented in git-config.1). This patch introduces a "gentle" form of xmmap which lets us produce our own error message. We do not want to use mmap directly, because we would like to use the other compatibility elements of xmmap (e.g., handling 0-length maps portably). The end result is: $ git.compile config --file=foo some.key value error: unable to mmap 'foo': No such device $ echo $? 3 Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'config.c')
-rw-r--r--config.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/config.c b/config.c
index dc7f8b2abc..d348d792aa 100644
--- a/config.c
+++ b/config.c
@@ -2048,8 +2048,15 @@ int git_config_set_multivar_in_file(const char *config_filename,
fstat(in_fd, &st);
contents_sz = xsize_t(st.st_size);
- contents = xmmap(NULL, contents_sz, PROT_READ,
- MAP_PRIVATE, in_fd, 0);
+ contents = xmmap_gently(NULL, contents_sz, PROT_READ,
+ MAP_PRIVATE, in_fd, 0);
+ if (contents == MAP_FAILED) {
+ error("unable to mmap '%s': %s",
+ config_filename, strerror(errno));
+ ret = CONFIG_INVALID_FILE;
+ contents = NULL;
+ goto out_free;
+ }
close(in_fd);
if (chmod(lock->filename.buf, st.st_mode & 07777) < 0) {