summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2019-07-28 18:12:45 -0700
committerH. Peter Anvin <hpa@zytor.com>2019-07-28 18:12:45 -0700
commit4b691b81ce6a5607788ced4ce89f9bbbc4ec5d8a (patch)
treedab22f4c7a90b7bf53359a7b0e281fdf52858004
parentf3ec9311a824de504d2ff8960d565d0e6cb28b0a (diff)
downloadsamples-4b691b81ce6a5607788ced4ce89f9bbbc4ec5d8a.tar.gz
samples-4b691b81ce6a5607788ced4ce89f9bbbc4ec5d8a.tar.xz
samples-4b691b81ce6a5607788ced4ce89f9bbbc4ec5d8a.zip
file.c: don't lose READ_FILE on mmap failure
Losing READ_FILE causes map_file_stdio() to think we are writing. Signed-off-by: H. Peter Anvin <hpa@zytor.com>
-rw-r--r--elf2exe/file.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/elf2exe/file.c b/elf2exe/file.c
index 12f2b46..e15c4d9 100644
--- a/elf2exe/file.c
+++ b/elf2exe/file.c
@@ -50,6 +50,9 @@ static int map_file_mmap(struct fileinfo *fileinfo)
void *map;
int fd = fileno(fileinfo->fp);
const int prot = PROT_READ | (for_write ? PROT_WRITE : 0);
+ struct stat st;
+ size_t size = fileinfo->size;
+ size_t mapsize;
if (!pagemask)
pagemask = ~((size_t)sysconf(_SC_PAGESIZE) - 1);
@@ -58,20 +61,22 @@ static int map_file_mmap(struct fileinfo *fileinfo)
if (ftruncate(fd, fileinfo->size))
goto err;
} else {
- struct stat st;
if (fstat(fd, &st))
goto err;
- fileinfo->size = st.st_size;
+ size = st.st_size;
}
- fileinfo->mapsize = (fileinfo->size + ~pagemask) & pagemask;
- map = mmap(NULL, fileinfo->mapsize, prot, MAP_SHARED, fd, 0);
+ mapsize = (size + ~pagemask) & pagemask;
+ map = mmap(NULL, mapsize, prot, MAP_SHARED, fd, 0);
if (map == MAP_FAILED)
goto err;
+ fileinfo->size = size; /* Don't set this on failure!!!! */
+ fileinfo->mapsize = mapsize;
fileinfo->rd = map;
if (for_write)
fileinfo->wr = map;
+
return 0;
err: