Permalink
Browse files

mmap() doesn't return NULL on failure. Fix that check.

  • Loading branch information...
shawnw committed Nov 6, 2018
1 parent 767cd56 commit 36a38774076a20525d2b4af78210fd9e30f20e24
Showing with 11 additions and 1 deletion.
  1. +11 −1 src/map_file.c
@@ -136,7 +136,7 @@ map_file(const char *filename, bool writable)
f->len = s.st_size;
f->data = mmap(NULL, s.st_size, prot, MAP_SHARED, fd, 0);
if (!f->data) {
if (f->data == (void *) -1) {
do_rawlog(LT_ERR, "map_file: unable to mmap file '%s': %s", filename,
strerror(errno));
close(fd);
@@ -220,6 +220,16 @@ TEST_GROUP(map_file)
TEST("map_file.open_file.2", bytes == 7);
TEST("map_file.writable.5", memcmp(data, "aBcdefg", 7) == 0);
// Non-existent file
m = map_file("no_such_file.txt", 0);
TEST("map_file.missing_file.1", m == NULL);
// Un-mappable file
#ifdef HAVE_DEV_URANDOM
m = map_file("/dev/urandom", 0);
TEST("map_file.unmappable.1", m == NULL);
#endif
cleanup:
remove(fname);
}

0 comments on commit 36a3877

Please sign in to comment.