check file size before mapping on linux

This commit is contained in:
Evgeniy A. Dushistov
2022-06-24 21:25:55 +03:00
committed by Evgeniy Dushistov
parent 823ec3d840
commit b294b76fb5

View File

@@ -7,6 +7,7 @@
#ifdef HAVE_MMAP
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#endif
#ifdef _WIN32
@@ -43,6 +44,13 @@ inline bool MapFile::open(const char *file_name, unsigned long file_size)
// g_print("Open file %s failed!\n",fullfilename);
return false;
}
struct stat st;
if (fstat(mmap_fd, &st) == -1 || st.st_size < 0 || (st.st_size == 0 && S_ISREG(st.st_mode))
|| sizeof(st.st_size) > sizeof(file_size) || static_cast<unsigned long>(st.st_size) != file_size) {
close(mmap_fd);
return false;
}
data = (gchar *)mmap(nullptr, file_size, PROT_READ, MAP_SHARED, mmap_fd, 0);
if ((void *)data == (void *)(-1)) {
// g_print("mmap file %s failed!\n",idxfilename);