From 6af3e6ad6d85bb4bd9ba656481d7e940b8c0a656 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 14 Nov 2023 10:49:12 +0100 Subject: [PATCH] chore: integrate cppcheck into workflow --- Makefile | 20 +++++++++++++++++++- include/lander.h | 2 +- lsm/include/lsm/bt.h | 2 +- lsm/include/lsm/store.h | 2 +- lsm/include/lsm/str.h | 10 +++++----- lsm/include/lsm/trie.h | 2 +- lsm/src/bt/lsm_bt.c | 6 +----- lsm/src/store/lsm_store.c | 12 ++++-------- lsm/src/store/lsm_store_disk_read.c | 4 ++-- lsm/src/store/lsm_store_entry.c | 2 +- lsm/src/str/lsm_str.c | 10 +++++----- lsm/src/trie/lsm_trie.c | 16 +++++++--------- src/event_loop/event_loop.c | 27 +++++++-------------------- src/http_loop/http_loop.c | 6 +++--- src/http_loop/http_loop_steps.c | 6 +++--- src/lander/lander.c | 4 ++-- src/lander/lander_post.c | 2 +- 17 files changed, 64 insertions(+), 69 deletions(-) diff --git a/Makefile b/Makefile index 1dc0dce..c40c27a 100644 --- a/Makefile +++ b/Makefile @@ -43,7 +43,6 @@ libtrie: liblsm: $(MAKE) -C lsm -.PHONY: $(BIN) $(BIN): libtrie liblsm $(OBJS) $(CC) -o $@ $(OBJS) $(_LDFLAGS) @@ -55,6 +54,12 @@ $(BUILD_DIR)/$(THIRDPARTY_DIR)/%.c.o: $(THIRDPARTY_DIR)/%.c mkdir -p $(dir $@) $(CC) $(_CFLAGS) -c $< -o $@ +.PHONY: bin-docker +bin-docker: + docker build -t lander . + docker container create --name lander-temp lander + docker cp -q lander-temp:/bin/lander $(BUILD_DIR)/lander-docker + docker container rm lander-temp # =====TESTING===== .PHONY: run @@ -110,6 +115,19 @@ lint: fmt: clang-format -i $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL) +.PHONY: check +check: + mkdir -p $(BUILD_DIR)/cppcheck + cppcheck \ + $(addprefix -I,$(INC_DIRS)) \ + --cppcheck-build-dir=$(BUILD_DIR)/cppcheck \ + --project=compile_commands.json \ + --error-exitcode=1 \ + --enable=warning,style \ + -ithirdparty/* \ + -itrie/* \ + --quiet + .PHONY: clean clean: rm -rf $(BUILD_DIR) diff --git a/include/lander.h b/include/lander.h index bff41fd..a30c32d 100644 --- a/include/lander.h +++ b/include/lander.h @@ -62,7 +62,7 @@ bool lander_post_file(event_loop_conn *conn); /** * Store the requested header as an attribute, if it's present. */ -void lander_header_to_attr(http_loop_ctx *ctx, char *header, +void lander_header_to_attr(http_loop_ctx *ctx, const char *header, lander_attr_type attr_type); /** diff --git a/lsm/include/lsm/bt.h b/lsm/include/lsm/bt.h index 2e30ae5..d7fe497 100644 --- a/lsm/include/lsm/bt.h +++ b/lsm/include/lsm/bt.h @@ -28,7 +28,7 @@ void lsm_bt_clear(lsm_bt *bt); /** * Return the size of the binary tree */ -uint64_t lsm_bt_size(lsm_bt *bt); +uint64_t lsm_bt_size(const lsm_bt *bt); /** * Search for the data stored behind the given key. diff --git a/lsm/include/lsm/store.h b/lsm/include/lsm/store.h index 45eda7e..82785a5 100644 --- a/lsm/include/lsm/store.h +++ b/lsm/include/lsm/store.h @@ -117,7 +117,7 @@ lsm_error lsm_store_init(lsm_store **ptr); * @param store store to use * @return how many elements are in the store */ -uint64_t lsm_store_size(lsm_store *store); +uint64_t lsm_store_size(const lsm_store *store); /** * Open the given database file and load it into a new store object. diff --git a/lsm/include/lsm/str.h b/lsm/include/lsm/str.h index 01f2651..2866f4f 100644 --- a/lsm/include/lsm/str.h +++ b/lsm/include/lsm/str.h @@ -34,7 +34,7 @@ lsm_error lsm_str_init_zero(lsm_str **ptr); * @param ptr pointer to store newly allocated pointer * @param s string to copy into lsm string */ -lsm_error lsm_str_init_copy(lsm_str **ptr, char *s); +lsm_error lsm_str_init_copy(lsm_str **ptr, const char *s); /** * Same as `lsm_str_init_copy`, except that it takes an additional argument @@ -45,7 +45,7 @@ lsm_error lsm_str_init_copy(lsm_str **ptr, char *s); * @param s string to copy into lsm string * @param len length of string to copy */ -lsm_error lsm_str_init_copy_n(lsm_str **ptr, char *s, uint64_t len); +lsm_error lsm_str_init_copy_n(lsm_str **ptr, const char *s, uint64_t len); /** * Overwrite an existing lsm_str so it now represents the new provided string. @@ -65,7 +65,7 @@ void lsm_str_overwrite(lsm_str *str, char *s); * @param str lsm_str object to modify * @param s string to copy into lsm string */ -lsm_error lsm_str_overwrite_copy(lsm_str *str, char *s); +lsm_error lsm_str_overwrite_copy(lsm_str *str, const char *s); /** * Same as `lsm_str_overwrite_copy`, except the length is explicitely specified, @@ -75,7 +75,7 @@ lsm_error lsm_str_overwrite_copy(lsm_str *str, char *s); * @param s string to copy into lsm string * @param len length of the string to copy */ -lsm_error lsm_str_overwrite_copy_n(lsm_str *str, char *s, uint64_t len); +lsm_error lsm_str_overwrite_copy_n(lsm_str *str, const char *s, uint64_t len); /** * Deallocate the existing internal string if needed and replace the lsm_str @@ -99,7 +99,7 @@ void lsm_str_free(lsm_str *str); * * @param str string to return length for. */ -uint64_t lsm_str_len(lsm_str *str); +uint64_t lsm_str_len(const lsm_str *str); /** * Return a pointer to the string's underlying char array. Note that this array diff --git a/lsm/include/lsm/trie.h b/lsm/include/lsm/trie.h index 7fd6b5b..6801890 100644 --- a/lsm/include/lsm/trie.h +++ b/lsm/include/lsm/trie.h @@ -55,6 +55,6 @@ lsm_error lsm_trie_remove(void **data, lsm_trie *trie, lsm_str *key); * * @param trie trie to return size for */ -uint64_t lsm_trie_size(lsm_trie *trie); +uint64_t lsm_trie_size(const lsm_trie *trie); #endif diff --git a/lsm/src/bt/lsm_bt.c b/lsm/src/bt/lsm_bt.c index 69fa895..6bfd435 100644 --- a/lsm/src/bt/lsm_bt.c +++ b/lsm/src/bt/lsm_bt.c @@ -57,7 +57,7 @@ void lsm_bt_free(lsm_bt *bt) { free(bt); } -uint64_t lsm_bt_size(lsm_bt *bt) { return bt->size; } +uint64_t lsm_bt_size(const lsm_bt *bt) { return bt->size; } lsm_error lsm_bt_insert(lsm_bt *bt, char key, void *data) { lsm_bt_node **dest = &bt->root; @@ -102,10 +102,6 @@ lsm_error lsm_bt_search(void **out, lsm_bt *bt, char key) { } lsm_error lsm_bt_remove(void **out, lsm_bt *bt, char key) { - if (bt->root == NULL) { - return lsm_error_not_found; - } - lsm_bt_node **dest = &bt->root; while ((*dest != NULL) && ((*dest)->key != key)) { diff --git a/lsm/src/store/lsm_store.c b/lsm/src/store/lsm_store.c index f93e289..460350c 100644 --- a/lsm/src/store/lsm_store.c +++ b/lsm/src/store/lsm_store.c @@ -30,7 +30,7 @@ lsm_error lsm_store_init(lsm_store **ptr) { return lsm_error_ok; } -uint64_t lsm_store_size(lsm_store *store) { return lsm_trie_size(store->trie); } +uint64_t lsm_store_size(const lsm_store *store) { return lsm_trie_size(store->trie); } lsm_error lsm_store_open_read(lsm_entry_handle **out, lsm_store *store, lsm_str *key) { @@ -43,11 +43,9 @@ lsm_error lsm_store_open_read(lsm_entry_handle **out, lsm_store *store, return lsm_error_lock_busy; } - lsm_entry *entry = wrapper->entry; - // While the trie's data field will never be NULL, the actual entry pointer // might be - if (entry == NULL) { + if (wrapper->entry == NULL) { pthread_rwlock_unlock(&wrapper->lock); return lsm_error_not_found; @@ -81,11 +79,9 @@ lsm_error lsm_store_open_write(lsm_entry_handle **out, lsm_store *store, return lsm_error_lock_busy; } - lsm_entry *entry = wrapper->entry; - // While the trie's data field will never be NULL, the actual entry pointer // might be - if (entry == NULL) { + if (wrapper->entry == NULL) { pthread_rwlock_unlock(&wrapper->lock); return lsm_error_not_found; @@ -202,7 +198,7 @@ lsm_error lsm_entry_data_append(lsm_entry_handle *handle, lsm_str *data) { lsm_error lsm_entry_data_read(uint64_t *out, char *buf, lsm_entry_handle *handle, uint64_t len) { - lsm_entry *entry = handle->wrapper->entry; + const lsm_entry *entry = handle->wrapper->entry; if (entry->data_len == 0) { *out = 0; diff --git a/lsm/src/store/lsm_store_disk_read.c b/lsm/src/store/lsm_store_disk_read.c index 72e34bd..b3e057a 100644 --- a/lsm/src/store/lsm_store_disk_read.c +++ b/lsm/src/store/lsm_store_disk_read.c @@ -27,7 +27,7 @@ lsm_error lsm_store_load(lsm_store **ptr, lsm_str *data_path) { fclose(db_file); - FILE *db_file = fopen(db_file_path, "r+b"); + db_file = fopen(db_file_path, "r+b"); if (db_file == NULL) { return lsm_error_failed_io; @@ -62,7 +62,7 @@ lsm_error lsm_store_load(lsm_store **ptr, lsm_str *data_path) { fclose(idx_file); // If opening it in extended read mode still fails now, there's a problem - FILE *idx_file = fopen(idx_file_path, "r+b"); + idx_file = fopen(idx_file_path, "r+b"); if (idx_file == NULL) { return lsm_error_failed_io; diff --git a/lsm/src/store/lsm_store_entry.c b/lsm/src/store/lsm_store_entry.c index a9c0ade..04a4710 100644 --- a/lsm/src/store/lsm_store_entry.c +++ b/lsm/src/store/lsm_store_entry.c @@ -107,7 +107,7 @@ lsm_error lsm_entry_attr_get_uint64_t(uint64_t *out, lsm_entry_handle *handle, LSM_RES(lsm_entry_attr_get(&s, handle, type)); - uint64_t num; + uint64_t num = 0; for (uint8_t i = 0; i < sizeof(uint64_t) / sizeof(char); i++) { ((char *)&num)[i] = lsm_str_char(s, i); diff --git a/lsm/src/str/lsm_str.c b/lsm/src/str/lsm_str.c index 0cfd571..393bbd2 100644 --- a/lsm/src/str/lsm_str.c +++ b/lsm/src/str/lsm_str.c @@ -33,7 +33,7 @@ lsm_error lsm_str_init_zero(lsm_str **ptr) { return lsm_error_ok; } -lsm_error lsm_str_init_copy(lsm_str **ptr, char *s) { +lsm_error lsm_str_init_copy(lsm_str **ptr, const char *s) { lsm_str *str = calloc(1, sizeof(lsm_str)); if (str == NULL) { @@ -47,7 +47,7 @@ lsm_error lsm_str_init_copy(lsm_str **ptr, char *s) { return lsm_error_ok; } -lsm_error lsm_str_init_copy_n(lsm_str **ptr, char *s, uint64_t len) { +lsm_error lsm_str_init_copy_n(lsm_str **ptr, const char *s, uint64_t len) { lsm_str *str = calloc(1, sizeof(lsm_str)); if (str == NULL) { @@ -72,11 +72,11 @@ void lsm_str_overwrite(lsm_str *str, char *s) { } } -lsm_error lsm_str_overwrite_copy(lsm_str *str, char *s) { +lsm_error lsm_str_overwrite_copy(lsm_str *str, const char *s) { return lsm_str_overwrite_copy_n(str, s, strlen(s)); } -lsm_error lsm_str_overwrite_copy_n(lsm_str *str, char *s, uint64_t len) { +lsm_error lsm_str_overwrite_copy_n(lsm_str *str, const char *s, uint64_t len) { if (len <= 8) { memcpy(str->data.val, s, len); } else { @@ -108,7 +108,7 @@ void lsm_str_free(lsm_str *str) { free(str); } -uint64_t lsm_str_len(lsm_str *str) { return str->len; } +uint64_t lsm_str_len(const lsm_str *str) { return str->len; } const char *lsm_str_ptr(lsm_str *str) { if (str->len <= 8) { diff --git a/lsm/src/trie/lsm_trie.c b/lsm/src/trie/lsm_trie.c index 0e5b548..eb13ec4 100644 --- a/lsm/src/trie/lsm_trie.c +++ b/lsm/src/trie/lsm_trie.c @@ -32,6 +32,8 @@ lsm_error lsm_trie_init(lsm_trie **ptr) { lsm_error res = lsm_trie_node_init(&root); if (res != lsm_error_ok) { + free(trie); + return res; } @@ -41,7 +43,7 @@ lsm_error lsm_trie_init(lsm_trie **ptr) { return lsm_error_ok; } -uint64_t lsm_trie_size(lsm_trie *trie) { return trie->size; } +uint64_t lsm_trie_size(const lsm_trie *trie) { return trie->size; } lsm_error lsm_trie_insert(lsm_trie *trie, lsm_str *key, void *data) { // NULL is not allowed as a data value, as it's used to indicate a lack of @@ -67,11 +69,10 @@ lsm_error lsm_trie_insert(lsm_trie *trie, lsm_str *key, void *data) { uint64_t index = 0; lsm_trie_node *node = trie->root; lsm_trie_node *next_node; - lsm_error res; while (index < key_len) { char c = lsm_str_char(key, index); - res = lsm_bt_search((void **)&next_node, &node->bt, c); + lsm_error res = lsm_bt_search((void **)&next_node, &node->bt, c); // No child is present yet for this character, so we can insert the string // here @@ -165,11 +166,10 @@ lsm_error lsm_trie_search(void **out, lsm_trie *trie, lsm_str *key) { uint64_t index = 0; lsm_trie_node *node = trie->root; lsm_trie_node *next_node; - lsm_error res; while (index < key_len) { char c = lsm_str_char(key, index); - res = lsm_bt_search((void **)&next_node, &node->bt, c); + lsm_error res = lsm_bt_search((void **)&next_node, &node->bt, c); if (res != lsm_error_ok) { return res; @@ -220,12 +220,10 @@ lsm_error lsm_trie_remove(void **data, lsm_trie *trie, lsm_str *key) { uint64_t index = 0; lsm_trie_node *parent = trie->root; lsm_trie_node *child; - lsm_error res; - char c; while (index < key_len) { - c = lsm_str_char(key, index); - res = lsm_bt_search((void **)&child, &parent->bt, c); + char c = lsm_str_char(key, index); + lsm_error res = lsm_bt_search((void **)&child, &parent->bt, c); if (res != lsm_error_ok) { return res; diff --git a/src/event_loop/event_loop.c b/src/event_loop/event_loop.c index 53317bc..8129223 100644 --- a/src/event_loop/event_loop.c +++ b/src/event_loop/event_loop.c @@ -13,14 +13,12 @@ #include "event_loop.h" #include "log.h" -static int event_loop_fd_set_nb(int fd) { +static void event_loop_fd_set_nb(int fd) { int flags = fcntl(fd, F_GETFL); flags |= O_NONBLOCK; fcntl(fd, F_SETFL, flags); - - return 0; } event_loop *event_loop_init() { @@ -61,13 +59,7 @@ int event_loop_accept(event_loop *el, int fd) { } // set the new connection fd to nonblocking mode - int res = event_loop_fd_set_nb(connfd); - - if (res < 0) { - close(connfd); - - return -2; - } + event_loop_fd_set_nb(connfd); // creating the struct Conn event_loop_conn *conn = event_loop_conn_init(el); @@ -82,7 +74,7 @@ int event_loop_accept(event_loop *el, int fd) { conn->fd = connfd; conn->state = event_loop_conn_state_req; - res = event_loop_put(el, conn); + int res = event_loop_put(el, conn); if (res != 0) { close(connfd); @@ -126,16 +118,10 @@ void event_loop_run(event_loop *el, int port) { } // The listening socket is always poll'ed in non-blocking mode as well - res = event_loop_fd_set_nb(fd); - - if (res != 0) { - critical(1, "Failed to set listening socket to non-blocking, errno: %i", - errno); - } + event_loop_fd_set_nb(fd); // TODO don't hardcode the number 32 struct pollfd *poll_args = calloc(sizeof(struct pollfd), 32); - size_t poll_args_count; // for convenience, the listening fd is put in the first position struct pollfd pfd = {fd, POLLIN, 0}; @@ -147,7 +133,7 @@ void event_loop_run(event_loop *el, int port) { info("Starting event loop on port %i", port); while (1) { - poll_args_count = 1; + size_t poll_args_count = 1; // connection fds for (size_t i = 0; i < el->connection_count; i++) { @@ -160,7 +146,8 @@ void event_loop_run(event_loop *el, int port) { events = (conn->state == event_loop_conn_state_req) ? POLLIN : POLLOUT; events |= POLLERR; - struct pollfd pfd = {conn->fd, events, 0}; + pfd.fd = conn->fd; + pfd.events = events; poll_args[poll_args_count] = pfd; poll_args_count++; diff --git a/src/http_loop/http_loop.c b/src/http_loop/http_loop.c index 964992d..cb4289e 100644 --- a/src/http_loop/http_loop.c +++ b/src/http_loop/http_loop.c @@ -72,17 +72,17 @@ event_loop *http_loop_init(http_route *routes, size_t route_count, } void http_loop_set_api_key(http_loop *hl, const char *api_key) { - ((http_loop_gctx *)hl->gctx)->api_key = api_key; + http_loop_gctx *gctx = hl->gctx; + gctx->api_key = api_key; } void http_loop_run(event_loop *el, int port) { debug("Compiling RegEx routes"); http_loop_gctx *gctx = el->gctx; - http_route *route; for (size_t i = 0; i < gctx->route_count; i++) { - route = &gctx->routes[i]; + http_route *route = &gctx->routes[i]; if (route->type == http_route_regex) { regex_t *r = calloc(sizeof(regex_t), 1); diff --git a/src/http_loop/http_loop_steps.c b/src/http_loop/http_loop_steps.c index bcfeae7..c3c9c0a 100644 --- a/src/http_loop/http_loop_steps.c +++ b/src/http_loop/http_loop_steps.c @@ -28,7 +28,7 @@ bool http_loop_step_parse_content_length(event_loop_conn *conn) { http_loop_ctx *ctx = conn->ctx; for (size_t i = 0; i < ctx->req.num_headers; i++) { - struct phr_header *header = &ctx->req.headers[i]; + const struct phr_header *header = &ctx->req.headers[i]; if (strncmp(header->name, "Content-Length", header->name_len) == 0) { // If the content length header is present but contains an invalid @@ -65,7 +65,7 @@ bool try_parse_content_length(event_loop_conn *conn) { http_loop_ctx *ctx = conn->ctx; for (size_t i = 0; i < ctx->req.num_headers; i++) { - struct phr_header *header = &ctx->req.headers[i]; + const struct phr_header *header = &ctx->req.headers[i]; if (strncmp(header->name, "Content-Length", header->name_len) == 0) { // If the content length header is present but contains an invalid @@ -145,7 +145,7 @@ bool http_loop_step_auth(event_loop_conn *conn) { http_loop_ctx *ctx = conn->ctx; for (size_t i = 0; i < ctx->req.num_headers; i++) { - struct phr_header *header = &ctx->req.headers[i]; + const struct phr_header *header = &ctx->req.headers[i]; if ((strncmp("X-Api-Key", header->name, header->name_len) == 0) && (strncmp(header->value, ctx->g->api_key, header->value_len) == 0) && diff --git a/src/lander/lander.c b/src/lander/lander.c index 442847a..5d1c1fe 100644 --- a/src/lander/lander.c +++ b/src/lander/lander.c @@ -72,12 +72,12 @@ void lander_ctx_reset(lander_ctx *ctx) { void lander_ctx_free(lander_ctx *ctx) { free(ctx); } -void lander_header_to_attr(http_loop_ctx *ctx, char *header_name, +void lander_header_to_attr(http_loop_ctx *ctx, const char *header_name, lander_attr_type attr_type) { lander_ctx *c_ctx = ctx->c; for (size_t i = 0; i < ctx->req.num_headers; i++) { - struct phr_header *header = &ctx->req.headers[i]; + const struct phr_header *header = &ctx->req.headers[i]; if (strncmp(header->name, header_name, header->name_len) == 0) { if (header->value_len > 0) { diff --git a/src/lander/lander_post.c b/src/lander/lander_post.c index b630373..9711d03 100644 --- a/src/lander/lander_post.c +++ b/src/lander/lander_post.c @@ -37,7 +37,7 @@ bool lander_insert_entry(http_loop_ctx *ctx) { randomize_key(key_s, key_len); lsm_str_init(&key, key_s); } else { - char *key_s = (char *)&ctx->req.path[ctx->req.regex_groups[2].rm_so]; + const char *key_s = &ctx->req.path[ctx->req.regex_groups[2].rm_so]; key_len = ctx->req.regex_groups[2].rm_eo - ctx->req.regex_groups[2].rm_so; lsm_str_init_copy_n(&key, key_s, key_len);