Commit 763e2b7e535b501b2fdd4a0c207ca424d0390d48

Ran Benita 2016-03-13T20:24:44

src/utils: check if fileno() failed in map_file fileno() can fail, if called on e.g. fmemopen() FILEs which are not backed by a file descriptor. This functions uses mmap to map the entire file to memory, so using such FILEs will not work. (There is actually no change of behavior here, since the following fstat would have already failed with EBADF. But lets make it clear.) Another possibility is to fall back to the !HAVE_MMAP case; but it sounds like a better idea to leave it to the programmer to use the new_from_string/new_from_buffer functions instead, instead of doing double allocation behind their back. Signed-off-by: Ran Benita <ran234@gmail.com>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
diff --git a/src/utils.c b/src/utils.c
index c417106..d725bbd 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -35,10 +35,13 @@ bool
 map_file(FILE *file, char **string_out, size_t *size_out)
 {
     struct stat stat_buf;
-    const int fd = fileno(file);
+    int fd;
     char *string;
 
     /* Make sure to keep the errno on failure! */
+    fd = fileno(file);
+    if (fd < 0)
+        return false;
 
     if (fstat(fd, &stat_buf) != 0)
         return false;