chore: integrate cppcheck into workflow

lsm
Jef Roosens 2023-11-14 10:49:12 +01:00
parent b053aa6c93
commit 6af3e6ad6d
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
17 changed files with 64 additions and 69 deletions

View File

@ -43,7 +43,6 @@ libtrie:
liblsm: liblsm:
$(MAKE) -C lsm $(MAKE) -C lsm
.PHONY: $(BIN)
$(BIN): libtrie liblsm $(OBJS) $(BIN): libtrie liblsm $(OBJS)
$(CC) -o $@ $(OBJS) $(_LDFLAGS) $(CC) -o $@ $(OBJS) $(_LDFLAGS)
@ -55,6 +54,12 @@ $(BUILD_DIR)/$(THIRDPARTY_DIR)/%.c.o: $(THIRDPARTY_DIR)/%.c
mkdir -p $(dir $@) mkdir -p $(dir $@)
$(CC) $(_CFLAGS) -c $< -o $@ $(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===== # =====TESTING=====
.PHONY: run .PHONY: run
@ -110,6 +115,19 @@ lint:
fmt: fmt:
clang-format -i $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL) 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 .PHONY: clean
clean: clean:
rm -rf $(BUILD_DIR) rm -rf $(BUILD_DIR)

View File

@ -62,7 +62,7 @@ bool lander_post_file(event_loop_conn *conn);
/** /**
* Store the requested header as an attribute, if it's present. * 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); lander_attr_type attr_type);
/** /**

View File

@ -28,7 +28,7 @@ void lsm_bt_clear(lsm_bt *bt);
/** /**
* Return the size of the binary tree * 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. * Search for the data stored behind the given key.

View File

@ -117,7 +117,7 @@ lsm_error lsm_store_init(lsm_store **ptr);
* @param store store to use * @param store store to use
* @return how many elements are in the store * @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. * Open the given database file and load it into a new store object.

View File

@ -34,7 +34,7 @@ lsm_error lsm_str_init_zero(lsm_str **ptr);
* @param ptr pointer to store newly allocated pointer * @param ptr pointer to store newly allocated pointer
* @param s string to copy into lsm string * @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 * 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 s string to copy into lsm string
* @param len length of string to copy * @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. * 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 str lsm_str object to modify
* @param s string to copy into lsm string * @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, * 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 s string to copy into lsm string
* @param len length of the string to copy * @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 * 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. * @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 * Return a pointer to the string's underlying char array. Note that this array

View File

@ -55,6 +55,6 @@ lsm_error lsm_trie_remove(void **data, lsm_trie *trie, lsm_str *key);
* *
* @param trie trie to return size for * @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 #endif

View File

@ -57,7 +57,7 @@ void lsm_bt_free(lsm_bt *bt) {
free(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_error lsm_bt_insert(lsm_bt *bt, char key, void *data) {
lsm_bt_node **dest = &bt->root; 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) { 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; lsm_bt_node **dest = &bt->root;
while ((*dest != NULL) && ((*dest)->key != key)) { while ((*dest != NULL) && ((*dest)->key != key)) {

View File

@ -30,7 +30,7 @@ lsm_error lsm_store_init(lsm_store **ptr) {
return lsm_error_ok; 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_error lsm_store_open_read(lsm_entry_handle **out, lsm_store *store,
lsm_str *key) { 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; return lsm_error_lock_busy;
} }
lsm_entry *entry = wrapper->entry;
// While the trie's data field will never be NULL, the actual entry pointer // While the trie's data field will never be NULL, the actual entry pointer
// might be // might be
if (entry == NULL) { if (wrapper->entry == NULL) {
pthread_rwlock_unlock(&wrapper->lock); pthread_rwlock_unlock(&wrapper->lock);
return lsm_error_not_found; 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; return lsm_error_lock_busy;
} }
lsm_entry *entry = wrapper->entry;
// While the trie's data field will never be NULL, the actual entry pointer // While the trie's data field will never be NULL, the actual entry pointer
// might be // might be
if (entry == NULL) { if (wrapper->entry == NULL) {
pthread_rwlock_unlock(&wrapper->lock); pthread_rwlock_unlock(&wrapper->lock);
return lsm_error_not_found; 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_error lsm_entry_data_read(uint64_t *out, char *buf,
lsm_entry_handle *handle, uint64_t len) { 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) { if (entry->data_len == 0) {
*out = 0; *out = 0;

View File

@ -27,7 +27,7 @@ lsm_error lsm_store_load(lsm_store **ptr, lsm_str *data_path) {
fclose(db_file); fclose(db_file);
FILE *db_file = fopen(db_file_path, "r+b"); db_file = fopen(db_file_path, "r+b");
if (db_file == NULL) { if (db_file == NULL) {
return lsm_error_failed_io; return lsm_error_failed_io;
@ -62,7 +62,7 @@ lsm_error lsm_store_load(lsm_store **ptr, lsm_str *data_path) {
fclose(idx_file); fclose(idx_file);
// If opening it in extended read mode still fails now, there's a problem // 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) { if (idx_file == NULL) {
return lsm_error_failed_io; return lsm_error_failed_io;

View File

@ -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)); 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++) { for (uint8_t i = 0; i < sizeof(uint64_t) / sizeof(char); i++) {
((char *)&num)[i] = lsm_str_char(s, i); ((char *)&num)[i] = lsm_str_char(s, i);

View File

@ -33,7 +33,7 @@ lsm_error lsm_str_init_zero(lsm_str **ptr) {
return lsm_error_ok; 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)); lsm_str *str = calloc(1, sizeof(lsm_str));
if (str == NULL) { if (str == NULL) {
@ -47,7 +47,7 @@ lsm_error lsm_str_init_copy(lsm_str **ptr, char *s) {
return lsm_error_ok; 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)); lsm_str *str = calloc(1, sizeof(lsm_str));
if (str == NULL) { 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)); 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) { if (len <= 8) {
memcpy(str->data.val, s, len); memcpy(str->data.val, s, len);
} else { } else {
@ -108,7 +108,7 @@ void lsm_str_free(lsm_str *str) {
free(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) { const char *lsm_str_ptr(lsm_str *str) {
if (str->len <= 8) { if (str->len <= 8) {

View File

@ -32,6 +32,8 @@ lsm_error lsm_trie_init(lsm_trie **ptr) {
lsm_error res = lsm_trie_node_init(&root); lsm_error res = lsm_trie_node_init(&root);
if (res != lsm_error_ok) { if (res != lsm_error_ok) {
free(trie);
return res; return res;
} }
@ -41,7 +43,7 @@ lsm_error lsm_trie_init(lsm_trie **ptr) {
return lsm_error_ok; 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) { 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 // 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; uint64_t index = 0;
lsm_trie_node *node = trie->root; lsm_trie_node *node = trie->root;
lsm_trie_node *next_node; lsm_trie_node *next_node;
lsm_error res;
while (index < key_len) { while (index < key_len) {
char c = lsm_str_char(key, index); 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 // No child is present yet for this character, so we can insert the string
// here // here
@ -165,11 +166,10 @@ lsm_error lsm_trie_search(void **out, lsm_trie *trie, lsm_str *key) {
uint64_t index = 0; uint64_t index = 0;
lsm_trie_node *node = trie->root; lsm_trie_node *node = trie->root;
lsm_trie_node *next_node; lsm_trie_node *next_node;
lsm_error res;
while (index < key_len) { while (index < key_len) {
char c = lsm_str_char(key, index); 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) { if (res != lsm_error_ok) {
return res; return res;
@ -220,12 +220,10 @@ lsm_error lsm_trie_remove(void **data, lsm_trie *trie, lsm_str *key) {
uint64_t index = 0; uint64_t index = 0;
lsm_trie_node *parent = trie->root; lsm_trie_node *parent = trie->root;
lsm_trie_node *child; lsm_trie_node *child;
lsm_error res;
char c;
while (index < key_len) { while (index < key_len) {
c = lsm_str_char(key, index); char c = lsm_str_char(key, index);
res = lsm_bt_search((void **)&child, &parent->bt, c); lsm_error res = lsm_bt_search((void **)&child, &parent->bt, c);
if (res != lsm_error_ok) { if (res != lsm_error_ok) {
return res; return res;

View File

@ -13,14 +13,12 @@
#include "event_loop.h" #include "event_loop.h"
#include "log.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); int flags = fcntl(fd, F_GETFL);
flags |= O_NONBLOCK; flags |= O_NONBLOCK;
fcntl(fd, F_SETFL, flags); fcntl(fd, F_SETFL, flags);
return 0;
} }
event_loop *event_loop_init() { 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 // set the new connection fd to nonblocking mode
int res = event_loop_fd_set_nb(connfd); event_loop_fd_set_nb(connfd);
if (res < 0) {
close(connfd);
return -2;
}
// creating the struct Conn // creating the struct Conn
event_loop_conn *conn = event_loop_conn_init(el); 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->fd = connfd;
conn->state = event_loop_conn_state_req; conn->state = event_loop_conn_state_req;
res = event_loop_put(el, conn); int res = event_loop_put(el, conn);
if (res != 0) { if (res != 0) {
close(connfd); 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 // The listening socket is always poll'ed in non-blocking mode as well
res = event_loop_fd_set_nb(fd); event_loop_fd_set_nb(fd);
if (res != 0) {
critical(1, "Failed to set listening socket to non-blocking, errno: %i",
errno);
}
// TODO don't hardcode the number 32 // TODO don't hardcode the number 32
struct pollfd *poll_args = calloc(sizeof(struct pollfd), 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 // for convenience, the listening fd is put in the first position
struct pollfd pfd = {fd, POLLIN, 0}; 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); info("Starting event loop on port %i", port);
while (1) { while (1) {
poll_args_count = 1; size_t poll_args_count = 1;
// connection fds // connection fds
for (size_t i = 0; i < el->connection_count; i++) { 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 = (conn->state == event_loop_conn_state_req) ? POLLIN : POLLOUT;
events |= POLLERR; events |= POLLERR;
struct pollfd pfd = {conn->fd, events, 0}; pfd.fd = conn->fd;
pfd.events = events;
poll_args[poll_args_count] = pfd; poll_args[poll_args_count] = pfd;
poll_args_count++; poll_args_count++;

View File

@ -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) { 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) { void http_loop_run(event_loop *el, int port) {
debug("Compiling RegEx routes"); debug("Compiling RegEx routes");
http_loop_gctx *gctx = el->gctx; http_loop_gctx *gctx = el->gctx;
http_route *route;
for (size_t i = 0; i < gctx->route_count; i++) { 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) { if (route->type == http_route_regex) {
regex_t *r = calloc(sizeof(regex_t), 1); regex_t *r = calloc(sizeof(regex_t), 1);

View File

@ -28,7 +28,7 @@ bool http_loop_step_parse_content_length(event_loop_conn *conn) {
http_loop_ctx *ctx = conn->ctx; http_loop_ctx *ctx = conn->ctx;
for (size_t i = 0; i < ctx->req.num_headers; i++) { 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 (strncmp(header->name, "Content-Length", header->name_len) == 0) {
// If the content length header is present but contains an invalid // 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; http_loop_ctx *ctx = conn->ctx;
for (size_t i = 0; i < ctx->req.num_headers; i++) { 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 (strncmp(header->name, "Content-Length", header->name_len) == 0) {
// If the content length header is present but contains an invalid // 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; http_loop_ctx *ctx = conn->ctx;
for (size_t i = 0; i < ctx->req.num_headers; i++) { 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) && if ((strncmp("X-Api-Key", header->name, header->name_len) == 0) &&
(strncmp(header->value, ctx->g->api_key, header->value_len) == 0) && (strncmp(header->value, ctx->g->api_key, header->value_len) == 0) &&

View File

@ -72,12 +72,12 @@ void lander_ctx_reset(lander_ctx *ctx) {
void lander_ctx_free(lander_ctx *ctx) { free(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_attr_type attr_type) {
lander_ctx *c_ctx = ctx->c; lander_ctx *c_ctx = ctx->c;
for (size_t i = 0; i < ctx->req.num_headers; i++) { 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 (strncmp(header->name, header_name, header->name_len) == 0) {
if (header->value_len > 0) { if (header->value_len > 0) {

View File

@ -37,7 +37,7 @@ bool lander_insert_entry(http_loop_ctx *ctx) {
randomize_key(key_s, key_len); randomize_key(key_s, key_len);
lsm_str_init(&key, key_s); lsm_str_init(&key, key_s);
} else { } 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; 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); lsm_str_init_copy_n(&key, key_s, key_len);