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>
master
Ran Benita 2016-03-13 20:24:44 +02:00
parent 39082082f6
commit 763e2b7e53
1 changed files with 4 additions and 1 deletions

View File

@ -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;