libc: read whole file into buffer

Fix #3023
This commit is contained in:
Ben Larson 2018-10-28 19:35:05 -06:00 committed by Christian Helmuth
parent 789d908cee
commit cde542c37c

View File

@ -1189,11 +1189,23 @@ void *Libc::Vfs_plugin::mmap(void *addr_in, ::size_t length, int prot, int flags
return (void *)-1;
}
if (::pread(fd->libc_fd, addr, length, offset) < 0) {
Genode::error("mmap could not obtain file content");
::munmap(addr, length);
errno = EACCES;
return (void *)-1;
/* copy variables for complete read */
size_t read_remain = length;
size_t read_offset = offset;
char *read_addr = (char *)addr;
while (read_remain > 0) {
ssize_t length_read = ::pread(fd->libc_fd, read_addr, read_remain, read_offset);
if (length_read < 0) { /* error */
Genode::error("mmap could not obtain file content");
::munmap(addr, length);
errno = EACCES;
return (void *)-1;
} else if (length_read == 0) /* EOF */
break; /* done (length can legally be greater than the file length) */
read_remain -= length_read;
read_offset += length_read;
read_addr += length_read;
}
return addr;