diff --git a/Makefile b/Makefile index 63c35f2..f2c448c 100644 --- a/Makefile +++ b/Makefile @@ -106,10 +106,14 @@ $(BUILD_DIR)/$(TEST_DIR)/%.c.o: $(TEST_DIR)/%.c .PHONY: lint lint: clang-format -n --Werror $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL) + make -C lsm lint + make -C landerctl lint .PHONY: fmt fmt: clang-format -i $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL) + make -C lsm fmt + make -C landerctl fmt .PHONY: check check: @@ -117,21 +121,21 @@ check: cppcheck \ $(addprefix -I,$(INC_DIRS)) \ --cppcheck-build-dir=$(BUILD_DIR)/cppcheck \ - --project=compile_commands.json \ --error-exitcode=1 \ --enable=warning,style \ - -ithirdparty/* \ - -itrie/* \ --inline-suppr \ --check-level=exhaustive \ --quiet \ - -j$(shell nproc) + -j$(shell nproc) \ + $(SRCS) + make -C lsm check + make -C landerctl check .PHONY: clean clean: rm -rf $(BUILD_DIR) $(MAKE) -C lsm clean - + $(MAKE) -C landerctl clean .PHONY: bear bear: clean diff --git a/landerctl/Makefile b/landerctl/Makefile index 72b8239..487e0f2 100644 --- a/landerctl/Makefile +++ b/landerctl/Makefile @@ -98,13 +98,13 @@ check: cppcheck \ $(addprefix -I,$(INC_DIRS)) \ --cppcheck-build-dir=$(BUILD_DIR)/cppcheck \ - --project=compile_commands.json \ --error-exitcode=1 \ --enable=warning,style \ --inline-suppr \ --check-level=exhaustive \ --quiet \ - -j$(shell nproc) + -j$(shell nproc) \ + $(SRCS) .PHONY: clean clean: diff --git a/lsm/Makefile b/lsm/Makefile index c853ed9..716d82d 100644 --- a/lsm/Makefile +++ b/lsm/Makefile @@ -97,6 +97,20 @@ lint: fmt: clang-format -i $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL) $(SRCS_EXAMPLE) +.PHONY: check +check: + mkdir -p $(BUILD_DIR)/cppcheck + cppcheck \ + $(addprefix -I,$(INC_DIRS)) \ + --cppcheck-build-dir=$(BUILD_DIR)/cppcheck \ + --error-exitcode=1 \ + --enable=warning,style \ + --inline-suppr \ + --check-level=exhaustive \ + --quiet \ + -j$(shell nproc) \ + $(SRCS) + .PHONY: clean clean: rm -rf $(BUILD_DIR) 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() {