From 68fd5460f40ecad9019607b8eb4499553290046a Mon Sep 17 00:00:00 2001 From: krangelov Date: Wed, 10 Nov 2021 17:10:31 +0100 Subject: [PATCH] fix cleanup after exceptions in PgfDB::PgfDB --- src/runtime/c/pgf/db.cxx | 50 +++++++++++++++++++++++++++++++++------ src/runtime/c/pgf/pgf.cxx | 1 - 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/runtime/c/pgf/db.cxx b/src/runtime/c/pgf/db.cxx index dd743600a..b929eb9b0 100644 --- a/src/runtime/c/pgf/db.cxx +++ b/src/runtime/c/pgf/db.cxx @@ -368,21 +368,25 @@ PgfDB::PgfDB(const char* filepath, int flags, int mode) { #else if (fd >= 0) { hMap = CreateFileMapping((HANDLE) _get_osfhandle(fd), - NULL, - PAGE_READWRITE, - HIWORD(file_size), LOWORD(file_size), - NULL); + NULL, + PAGE_READWRITE, + HIWORD(file_size), LOWORD(file_size), + NULL); if (hMap != NULL) { ms = (malloc_state*) MapViewOfFile(hMap, - FILE_MAP_WRITE, - 0,0,file_size); + FILE_MAP_WRITE, + 0,0,file_size); + if (ms == NULL) { + CloseHandle(hMap); + hMap = INVALID_HANDLE_VALUE; + } } else { hMap = INVALID_HANDLE_VALUE; ms = NULL; } } else { hMap = INVALID_HANDLE_VALUE; - ms = (malloc_state*) ::malloc(file_size); + ms = (malloc_state*) ::malloc(file_size); } if (ms == NULL) { @@ -396,6 +400,22 @@ PgfDB::PgfDB(const char* filepath, int flags, int mode) { try { rwlock = ipc_new_file_rwlock(this->filepath, &is_first); } catch (pgf_systemerror e) { +#ifndef _WIN32 +#ifndef MREMAP_MAYMOVE + if (fd < 0) { + ::free(ms); + } else +#endif + munmap(ms,file_size); +#else + if (fd < 0) { + ::free(ms); + } else { + UnmapViewOfFile(ms); + CloseHandle(hMap); + } +#endif + ::free((void *) this->filepath); close(fd); throw e; @@ -405,6 +425,22 @@ PgfDB::PgfDB(const char* filepath, int flags, int mode) { init_state(file_size); } else { if (strncmp(ms->sign, slovo, sizeof(ms->sign)) != 0) { +#ifndef _WIN32 +#ifndef MREMAP_MAYMOVE + if (fd < 0) { + ::free(ms); + } else +#endif + munmap(ms,file_size); +#else + if (fd < 0) { + ::free(ms); + } else { + UnmapViewOfFile(ms); + CloseHandle(hMap); + } +#endif + ::free((void *) this->filepath); close(fd); throw pgf_error("Invalid file content"); diff --git a/src/runtime/c/pgf/pgf.cxx b/src/runtime/c/pgf/pgf.cxx index e5dda5c3d..e91f165d4 100644 --- a/src/runtime/c/pgf/pgf.cxx +++ b/src/runtime/c/pgf/pgf.cxx @@ -1,7 +1,6 @@ #include #include #include -#include #include "data.h" #include "reader.h"