1.1 --- a/libfsclient/lib/src/client.cc Sat Mar 18 00:52:53 2023 +0100
1.2 +++ b/libfsclient/lib/src/client.cc Sat Mar 18 23:57:46 2023 +0100
1.3 @@ -26,6 +26,7 @@
1.4 #include <stdlib.h>
1.5 #include <string.h>
1.6
1.7 +#include <mem/memory_utils.h>
1.8 #include <systypes/env.h>
1.9 #include <systypes/fcntl.h>
1.10
1.11 @@ -79,11 +80,24 @@
1.12
1.13 if (file->object_flags & OBJECT_SUPPORTS_MMAP)
1.14 {
1.15 + offset_t adjusted_position = position;
1.16 +
1.17 /* Where the position is outside the current region, re-map. */
1.18
1.19 if ((position < file->start_pos) || (position >= file->end_pos))
1.20 {
1.21 - if (file_mmap(file, position, file_span(file), 0, 0,
1.22 + offset_t length = file_span(file);
1.23 +
1.24 + if (!length)
1.25 + length = PAGE_SIZE;
1.26 +
1.27 + /* Avoid growth of the mapped region when the end of the region is
1.28 + calculated to be position + span, which is then rounded up, whereas the
1.29 + start of the region is rounded down. */
1.30 +
1.31 + adjusted_position = trunc(position, PAGE_SIZE);
1.32 +
1.33 + if (file_mmap(file, adjusted_position, length, 0, 0,
1.34 file_region_flags(file->flags)))
1.35 return -L4_EIO;
1.36 }