Skip to content

Commit 1570856

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3a1b312 commit 1570856

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

config.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,8 +2048,15 @@ int git_config_set_multivar_in_file(const char *config_filename,
20482048

20492049
fstat(in_fd, &st);
20502050
contents_sz = xsize_t(st.st_size);
2051-
contents = xmmap(NULL, contents_sz, PROT_READ,
2052-
MAP_PRIVATE, in_fd, 0);
2051+
contents = xmmap_gently(NULL, contents_sz, PROT_READ,
2052+
MAP_PRIVATE, in_fd, 0);
2053+
if (contents == MAP_FAILED) {
2054+
error("unable to mmap '%s': %s",
2055+
config_filename, strerror(errno));
2056+
ret = CONFIG_INVALID_FILE;
2057+
contents = NULL;
2058+
goto out_free;
2059+
}
20532060
close(in_fd);
20542061

20552062
if (chmod(lock->filename.buf, st.st_mode & 07777) < 0) {

git-compat-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ extern char *xstrndup(const char *str, size_t len);
623623
extern void *xrealloc(void *ptr, size_t size);
624624
extern void *xcalloc(size_t nmemb, size_t size);
625625
extern void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
626+
extern void *xmmap_gently(void *start, size_t length, int prot, int flags, int fd, off_t offset);
626627
extern ssize_t xread(int fd, void *buf, size_t len);
627628
extern ssize_t xwrite(int fd, const void *buf, size_t len);
628629
extern ssize_t xpread(int fd, void *buf, size_t len, off_t offset);

sha1_file.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -707,8 +707,8 @@ static void mmap_limit_check(size_t length)
707707
(uintmax_t)length, (uintmax_t)limit);
708708
}
709709

710-
void *xmmap(void *start, size_t length,
711-
int prot, int flags, int fd, off_t offset)
710+
void *xmmap_gently(void *start, size_t length,
711+
int prot, int flags, int fd, off_t offset)
712712
{
713713
void *ret;
714714

@@ -719,12 +719,19 @@ void *xmmap(void *start, size_t length,
719719
return NULL;
720720
release_pack_memory(length);
721721
ret = mmap(start, length, prot, flags, fd, offset);
722-
if (ret == MAP_FAILED)
723-
die_errno("Out of memory? mmap failed");
724722
}
725723
return ret;
726724
}
727725

726+
void *xmmap(void *start, size_t length,
727+
int prot, int flags, int fd, off_t offset)
728+
{
729+
void *ret = xmmap_gently(start, length, prot, flags, fd, offset);
730+
if (ret == MAP_FAILED)
731+
die_errno("Out of memory? mmap failed");
732+
return ret;
733+
}
734+
728735
void close_pack_windows(struct packed_git *p)
729736
{
730737
while (p->windows) {

0 commit comments

Comments
 (0)