From a2d4b970e7490ad364a7fe595beb6556457fda29 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 18 Nov 2023 22:28:50 +0100 Subject: [PATCH] chore: please cppcheck --- lsm/src/store/lsm_store_disk_read.c | 12 +++++++++++- lsm/src/store/lsm_store_disk_write.c | 6 +++--- lsm/src/store/lsm_store_entry.c | 4 +++- src/main.c | 7 ++----- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lsm/src/store/lsm_store_disk_read.c b/lsm/src/store/lsm_store_disk_read.c index b3e057a..721b4f3 100644 --- a/lsm/src/store/lsm_store_disk_read.c +++ b/lsm/src/store/lsm_store_disk_read.c @@ -47,6 +47,8 @@ lsm_error lsm_store_load(lsm_store **ptr, lsm_str *data_path) { idx_file = fopen(idx_file_path, "wb"); if (idx_file == NULL) { + fclose(db_file); + return lsm_error_failed_io; } @@ -55,6 +57,9 @@ lsm_error lsm_store_load(lsm_store **ptr, lsm_str *data_path) { uint64_t num = 0; if (fwrite(&num, sizeof(uint64_t), 1, idx_file) == 0) { + fclose(db_file); + fclose(idx_file); + return lsm_error_failed_io; } @@ -65,6 +70,8 @@ lsm_error lsm_store_load(lsm_store **ptr, lsm_str *data_path) { idx_file = fopen(idx_file_path, "r+b"); if (idx_file == NULL) { + fclose(db_file); + return lsm_error_failed_io; } } @@ -73,7 +80,10 @@ lsm_error lsm_store_load(lsm_store **ptr, lsm_str *data_path) { store->db.f = db_file; store->idx.f = idx_file; - LSM_RES(lsm_store_load_db(store)); + LSM_RES2(lsm_store_load_db(store), { + fclose(db_file); + fclose(idx_file); + }); *ptr = store; diff --git a/lsm/src/store/lsm_store_disk_write.c b/lsm/src/store/lsm_store_disk_write.c index b6906e6..31f907b 100644 --- a/lsm/src/store/lsm_store_disk_write.c +++ b/lsm/src/store/lsm_store_disk_write.c @@ -1,7 +1,7 @@ #include "lsm/store_internal.h" static lsm_error lsm_fwrite(uint64_t *sum, FILE *f, uint64_t size, - uint64_t count, void *val) { + uint64_t count, const void *val) { size_t res = fwrite(val, size, count, f); if (res < count) { @@ -15,7 +15,7 @@ static lsm_error lsm_fwrite(uint64_t *sum, FILE *f, uint64_t size, return lsm_error_ok; } -static lsm_error lsm_write_str(uint64_t *sum, FILE *f, lsm_str *s) { +static lsm_error lsm_write_str(uint64_t *sum, FILE *f, const lsm_str *s) { uint64_t len = lsm_str_len(s); LSM_RES(lsm_fwrite(sum, f, sizeof(uint64_t), 1, &len)); @@ -129,7 +129,7 @@ lsm_error lsm_entry_disk_insert(lsm_entry_handle *handle) { // its entry to zero lsm_error lsm_entry_disk_remove(lsm_entry_handle *handle) { lsm_store *store = handle->store; - lsm_entry *entry = handle->wrapper->entry; + const lsm_entry *entry = handle->wrapper->entry; pthread_mutex_lock(&store->idx.lock); diff --git a/lsm/src/store/lsm_store_entry.c b/lsm/src/store/lsm_store_entry.c index 89e3ea7..63d90a7 100644 --- a/lsm/src/store/lsm_store_entry.c +++ b/lsm/src/store/lsm_store_entry.c @@ -247,7 +247,7 @@ void lsm_entry_data_path(char *path, const lsm_entry_handle *handle) { const lsm_str *key = handle->wrapper->entry->key; uint8_t levels = - key->len <= LSM_STORE_DATA_LEVELS ? key->len : LSM_STORE_DATA_LEVELS; + key->len > LSM_STORE_DATA_LEVELS ? LSM_STORE_DATA_LEVELS : key->len; memcpy(path, lsm_str_ptr(data_path), data_path->len); path[data_path->len] = '/'; @@ -255,6 +255,7 @@ void lsm_entry_data_path(char *path, const lsm_entry_handle *handle) { uint64_t index = data_path->len + 1; // Create each directory in the file hierarchy + // cppcheck-suppress knownConditionTrueFalse for (uint8_t i = 0; i < levels; i++) { path[index] = lsm_str_char(key, i); path[index + 1] = '/'; @@ -279,6 +280,7 @@ lsm_error lsm_entry_data_open_write(lsm_entry_handle *handle) { key->len <= LSM_STORE_DATA_LEVELS ? key->len : LSM_STORE_DATA_LEVELS; // Create all required directories in the path + // cppcheck-suppress knownConditionTrueFalse for (uint8_t i = 0; i < levels; i++) { path[data_path->len + 2 * (i + 1)] = '\0'; diff --git a/src/main.c b/src/main.c index fa9c64f..2f52fc1 100644 --- a/src/main.c +++ b/src/main.c @@ -9,15 +9,12 @@ const char *var = getenv(env_var); \ if (var == NULL) { \ critical(1, "Missing environment variable %s", env_var); \ - } \ - var = strdup(var); + } #define ENV_OPT(var, env_var, default) \ const char *var = getenv(env_var); \ if (var == NULL) { \ - var = strdup(default); \ - } else { \ - var = strdup(var); \ + var = default; \ } int main() {