From 0a661a7d70a049c67b606941f191db6ecdf16f3c Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Thu, 19 Oct 2023 20:55:31 -0700 Subject: [PATCH] Close file handle when done. This also brings the arena to a single-return standard. --- src/sl/Arena.cc | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/sl/Arena.cc b/src/sl/Arena.cc index 85923bc..b111021 100644 --- a/src/sl/Arena.cc +++ b/src/sl/Arena.cc @@ -127,26 +127,25 @@ Arena::Open(const char *path) int Arena::Create(const char *path, size_t fileSize) { + int ret = -1; + if (this->size > 0) { this->Destroy(); } auto *fHandle = fopen(path, "w"); - if (fHandle == nullptr) { - return -1; - } + if (fHandle != nullptr) { + auto newFileDes = fileno(fHandle); + if (ftruncate(newFileDes, static_cast(fileSize)) == 0) { + ret = this->Open(path); + } - auto newFileDes = fileno(fHandle); - - if (ftruncate(newFileDes, static_cast(fileSize)) == -1) { close(newFileDes); fclose(fHandle); - return -1; } - close(newFileDes); - return this->Open(path); + return ret; }