forked from GitHub/gf-core
fix cleanup after exceptions in PgfDB::PgfDB
This commit is contained in:
@@ -368,21 +368,25 @@ PgfDB::PgfDB(const char* filepath, int flags, int mode) {
|
|||||||
#else
|
#else
|
||||||
if (fd >= 0) {
|
if (fd >= 0) {
|
||||||
hMap = CreateFileMapping((HANDLE) _get_osfhandle(fd),
|
hMap = CreateFileMapping((HANDLE) _get_osfhandle(fd),
|
||||||
NULL,
|
NULL,
|
||||||
PAGE_READWRITE,
|
PAGE_READWRITE,
|
||||||
HIWORD(file_size), LOWORD(file_size),
|
HIWORD(file_size), LOWORD(file_size),
|
||||||
NULL);
|
NULL);
|
||||||
if (hMap != NULL) {
|
if (hMap != NULL) {
|
||||||
ms = (malloc_state*) MapViewOfFile(hMap,
|
ms = (malloc_state*) MapViewOfFile(hMap,
|
||||||
FILE_MAP_WRITE,
|
FILE_MAP_WRITE,
|
||||||
0,0,file_size);
|
0,0,file_size);
|
||||||
|
if (ms == NULL) {
|
||||||
|
CloseHandle(hMap);
|
||||||
|
hMap = INVALID_HANDLE_VALUE;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
hMap = INVALID_HANDLE_VALUE;
|
hMap = INVALID_HANDLE_VALUE;
|
||||||
ms = NULL;
|
ms = NULL;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
hMap = INVALID_HANDLE_VALUE;
|
hMap = INVALID_HANDLE_VALUE;
|
||||||
ms = (malloc_state*) ::malloc(file_size);
|
ms = (malloc_state*) ::malloc(file_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ms == NULL) {
|
if (ms == NULL) {
|
||||||
@@ -396,6 +400,22 @@ PgfDB::PgfDB(const char* filepath, int flags, int mode) {
|
|||||||
try {
|
try {
|
||||||
rwlock = ipc_new_file_rwlock(this->filepath, &is_first);
|
rwlock = ipc_new_file_rwlock(this->filepath, &is_first);
|
||||||
} catch (pgf_systemerror e) {
|
} 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);
|
::free((void *) this->filepath);
|
||||||
close(fd);
|
close(fd);
|
||||||
throw e;
|
throw e;
|
||||||
@@ -405,6 +425,22 @@ PgfDB::PgfDB(const char* filepath, int flags, int mode) {
|
|||||||
init_state(file_size);
|
init_state(file_size);
|
||||||
} else {
|
} else {
|
||||||
if (strncmp(ms->sign, slovo, sizeof(ms->sign)) != 0) {
|
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);
|
::free((void *) this->filepath);
|
||||||
close(fd);
|
close(fd);
|
||||||
throw pgf_error("Invalid file content");
|
throw pgf_error("Invalid file content");
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <io.h>
|
|
||||||
|
|
||||||
#include "data.h"
|
#include "data.h"
|
||||||
#include "reader.h"
|
#include "reader.h"
|
||||||
|
|||||||
Reference in New Issue
Block a user