Check the return value of mmap() correctly.
Empty files cannot be mmap'd so we implement some
work around code for that.
https://bugs.freedesktop.org/show_bug.cgi?id=74773
Signed-off-by: Stef Walter <stef@thewalter.net>
return NULL;
}
+ if (sb->st_size == 0) {
+ *data = "";
+ *size = 0;
+ return map;
+ }
+
map->size = sb->st_size;
map->data = mmap (NULL, map->size, PROT_READ, MAP_PRIVATE, map->fd, 0);
- if (map->data == NULL) {
+ if (map->data == MAP_FAILED) {
close (map->fd);
free (map);
return NULL;
void
p11_mmap_close (p11_mmap *map)
{
- munmap (map->data, map->size);
+ if (map->size)
+ munmap (map->data, map->size);
close (map->fd);
free (map);
}
free (path);
}
+static void
+test_mmap (void)
+{
+ p11_mmap *map;
+ void *data;
+ size_t size;
+ char file[] = "emptyfileXXXXXX";
+ int fd = mkstemp (file);
+ close (fd);
+ /* mmap on empty file should work */
+ map = p11_mmap_open (file, NULL, &data, &size);
+ unlink (file);
+ assert_ptr_not_null (map);
+ p11_mmap_close (map);
+}
+
#endif /* OS_UNIX */
int
if (!getenv ("FAKED_MODE")) {
p11_test (test_getauxval, "/compat/getauxval");
}
+ p11_test (test_mmap, "/compat/mmap");
#endif
return p11_test_run (argc, argv);
}