summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPascal Terjan <pterjan@google.com>2014-02-09 23:39:20 +0000
committerStef Walter <stef@thewalter.net>2014-02-13 08:23:16 +0100
commit44beedb8c2b4e30b421b604fb1b044402a1d1ff6 (patch)
tree6cd39093cdf428f8d01e650cd862d86dd64c0219
parentc59a6b577b7ba1990a7dc04a894c3bc4f4671471 (diff)
downloadp11-kit-44beedb8c2b4e30b421b604fb1b044402a1d1ff6.tar.gz
Fix handling of mmap failure and mapping empty files
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>
-rw-r--r--common/compat.c11
-rw-r--r--common/tests/test-compat.c17
2 files changed, 26 insertions, 2 deletions
diff --git a/common/compat.c b/common/compat.c
index 9aa556a..f5b88ec 100644
--- a/common/compat.c
+++ b/common/compat.c
@@ -216,9 +216,15 @@ p11_mmap_open (const char *path,
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;
@@ -232,7 +238,8 @@ p11_mmap_open (const char *path,
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);
}
diff --git a/common/tests/test-compat.c b/common/tests/test-compat.c
index 872170d..42471ae 100644
--- a/common/tests/test-compat.c
+++ b/common/tests/test-compat.c
@@ -83,6 +83,22 @@ test_getauxval (void)
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
@@ -95,6 +111,7 @@ main (int argc,
if (!getenv ("FAKED_MODE")) {
p11_test (test_getauxval, "/compat/getauxval");
}
+ p11_test (test_mmap, "/compat/mmap");
#endif
return p11_test_run (argc, argv);
}