fix a memory leak

This commit is contained in:
krangelov
2021-09-28 09:53:40 +02:00
parent 5334174923
commit 9863f32d05

View File

@@ -300,37 +300,53 @@ PgfDB::PgfDB(const char* filepath, int flags, int mode) {
if (fd < 0) if (fd < 0)
throw pgf_systemerror(errno, filepath); throw pgf_systemerror(errno, filepath);
this->filepath = strdup(filepath);
file_size = lseek(fd, 0, SEEK_END); file_size = lseek(fd, 0, SEEK_END);
if (file_size == ((off_t) -1)) if (file_size == ((off_t) -1)) {
throw pgf_systemerror(errno, filepath); int code = errno;
close(fd);
throw pgf_systemerror(code, filepath);
}
is_new = false; is_new = false;
if (file_size == 0) { if (file_size == 0) {
file_size = getpagesize(); file_size = getpagesize();
if (ftruncate(fd, file_size) < 0) if (ftruncate(fd, file_size) < 0) {
throw pgf_systemerror(errno, filepath); int code = errno;
close(fd);
throw pgf_systemerror(code, filepath);
}
is_new = true; is_new = true;
} }
this->filepath = strdup(filepath);
} }
int mflags = (fd < 0) ? (MAP_PRIVATE | MAP_ANONYMOUS) : MAP_SHARED; int mflags = (fd < 0) ? (MAP_PRIVATE | MAP_ANONYMOUS) : MAP_SHARED;
ms = (malloc_state*) ms = (malloc_state*)
mmap(NULL, file_size, PROT_READ | PROT_WRITE, mflags, fd, 0); mmap(NULL, file_size, PROT_READ | PROT_WRITE, mflags, fd, 0);
if (ms == MAP_FAILED) if (ms == MAP_FAILED) {
throw pgf_systemerror(errno, filepath); ::free((void *) this->filepath);
int code = errno;
close(fd);
throw pgf_systemerror(code, filepath);
}
int res = pthread_rwlock_init(&rwlock, NULL); int res = pthread_rwlock_init(&rwlock, NULL);
if (res != 0) { if (res != 0) {
throw pgf_systemerror(errno); ::free((void *) this->filepath);
int code = errno;
close(fd);
throw pgf_systemerror(code);
} }
if (is_new) { if (is_new) {
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) {
::free((void *) this->filepath);
close(fd);
throw pgf_error("Invalid file content"); throw pgf_error("Invalid file content");
}
// We must make sure that left-over transient revisions are // We must make sure that left-over transient revisions are
// released. This may happen if a client process was killed // released. This may happen if a client process was killed