From 5431fec6726c18234c9c28b014cc6f18a0d79884 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Fri, 6 Jun 2025 14:09:32 -0500 Subject: Style touchup --- src/arena.c | 24 +++++++++------------ src/encryption.c | 25 ++++++++++++---------- src/ll.c | 43 +++++++++++++------------------------- src/scanner.c | 6 ++++-- src/shared.c | 31 ++++++++++++++-------------- src/shared.h | 2 +- src/threadpool.c | 63 ++++++++++++++++++++++++++++++++++++++++---------------- src/threadpool.h | 30 +++------------------------ 8 files changed, 106 insertions(+), 118 deletions(-) diff --git a/src/arena.c b/src/arena.c index 40c72d5..8f32064 100644 --- a/src/arena.c +++ b/src/arena.c @@ -1,6 +1,7 @@ #include "arena.h" #include "shared.h" +#include #include #include #include @@ -26,6 +27,8 @@ typedef struct arena { } arena; arenanode * arenanode_init(size_t bytes) { + if(bytes < 1) ERRRET(EINVAL, NULL); + arenanode *an = VALLOC(1, sizeof(*an)); if(!an) return NULL; @@ -47,10 +50,8 @@ arenanode * arenanode_init(size_t bytes) { } arena * arena_init(size_t bytes) { - if(!ISPOWOF2(MEM_ALIGN_BYTES)) - XALLOC_EXIT(" \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to create a new arena", ); - if(bytes < 1) - RETURNWERR(EINVAL, NULL); + if(!ISPOWOF2(MEM_ALIGN_BYTES)) XALLOC_EXIT(" \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to create a new arena", ); + if(bytes < 1) ERRRET(EINVAL, NULL); arena *a = VALLOC(1, sizeof(arena)); if(!a) @@ -73,12 +74,9 @@ arena * arena_init(size_t bytes) { } void * arena_alloc(arena * const arena, size_t bytes) { - if(!ISPOWOF2(MEM_ALIGN_BYTES)) - XALLOC_EXIT(" \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to allocate any memory", ); - if(!arena) - RETURNWERR(EINVAL, NULL); - if(bytes > arena->node_memspace) - RETURNWERR(ENOMEM, NULL); + if(!ISPOWOF2(MEM_ALIGN_BYTES)) XALLOC_EXIT(" \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to allocate any memory", ); + if(!arena) ERRRET(EINVAL, NULL); + if(bytes > arena->node_memspace) ERRRET(ENOMEM, NULL); if(bytes > ((arena->current)->allocated - (arena->current)->used)) { arenanode *new = arenanode_init(arena->node_memspace); @@ -133,10 +131,8 @@ void * simplearena_alloc(simplearena * const a, size_t bytes) { // The criteria to allocate new memory in arena_alloc is 'bytes > ((a->current)->allocated - (a->current)->used)', so if this // is true, just return NULL & set errno - if(!a) - RETURNWERR(EINVAL, NULL); - if(bytes > ((a->current)->allocated - (a->current)->used)) - RETURNWERR(ENOMEM, NULL); + if(!a) ERRRET(EINVAL, NULL); + if(bytes > ((a->current)->allocated - (a->current)->used)) ERRRET(ENOMEM, NULL); return arena_alloc(a, bytes); } diff --git a/src/encryption.c b/src/encryption.c index 3530eed..3bf9dd4 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -1,3 +1,5 @@ +// TODO: Go back and make sure every function has proper error handling + #define _GNU_SOURCE #include "encryption.h" @@ -70,12 +72,13 @@ void checksodium(void) { #endif int maketmp(const char * const dest) { - if(!dest) - RETURNWERR(EINVAL, -1); + if(!dest) ERRRET(EINVAL, -1); return open(dest, (O_TMPFILE | O_WRONLY | O_CLOEXEC | O_SYNC), (S_IRUSR | S_IWUSR)); } int linkto(const char * const target, int tgfd) { + if(!target) ERRRET(EINVAL, -1); + char *path = NULL; asprintf(&path, "/proc/self/fd/%d", tgfd); if(!path) @@ -129,11 +132,11 @@ int decryptto(const char * const encrypted, const char * const target, const uns #endif if(!encrypted) - RETURNWERR(EINVAL, -1); + ERRRET(EINVAL, -1); if(!target) - RETURNWERR(EINVAL, -1); + ERRRET(EINVAL, -1); if(!key) - RETURNWERR(EINVAL, -1); + ERRRET(EINVAL, -1); FILE *src, *dst; if(!(src = fopen(encrypted, "rb"))) @@ -174,11 +177,11 @@ int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr #endif if(!src) - RETURNWERR(EINVAL, -1); + ERRRET(EINVAL, -1); if(!dst) - RETURNWERR(EINVAL, -1); + ERRRET(EINVAL, -1); if(!key) - RETURNWERR(EINVAL, -1); + ERRRET(EINVAL, -1); // Write the header crypto_secretstream_xchacha20poly1305_init_push(&state, header, key); @@ -217,11 +220,11 @@ int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr #endif if(!src) - RETURNWERR(EINVAL, -1); + ERRRET(EINVAL, -1); if(!dst) - RETURNWERR(EINVAL, -1); + ERRRET(EINVAL, -1); if(!key) - RETURNWERR(EINVAL, -1); + ERRRET(EINVAL, -1); // Read the header if(fread(header, 1, sizeof(header), src) < sizeof(header)) diff --git a/src/ll.c b/src/ll.c index b8d0d4c..908f25f 100644 --- a/src/ll.c +++ b/src/ll.c @@ -66,15 +66,12 @@ void dlinkedlist_free(void *dll) { } int dlinkedlist_size(const dlinkedlist * const ll) { - if(!ll) - RETURNWERR(EINVAL, -1); - + if(!ll) ERRRET(EINVAL, -1); return ll->size; } int dlinkedlist_handlefirstnode(dlinkedlist * const ll, void *data, dll_freecb fcb) { - if(!ll) - RETURNWERR(EINVAL, 1); + if(!ll) ERRRET(EINVAL, 1); if(ll->size > 0) return 1; @@ -92,10 +89,7 @@ int dlinkedlist_handlefirstnode(dlinkedlist * const ll, void *data, dll_freecb f } int dlinkedlist_xxxend(dlinkedlist * const ll, void *data, dll_freecb fcb, char op) { - if(!ll) - RETURNWERR(EINVAL, 1); - if(op != 'a' && op != 'p') - RETURNWERR(EINVAL, 1); + if(!ll || (op != 'a' && op != 'p')) ERRRET(EINVAL, 1); int handleret; if((handleret = dlinkedlist_handlefirstnode(ll, data, fcb)) == 0) @@ -126,6 +120,8 @@ int dlinkedlist_xxxend(dlinkedlist * const ll, void *data, dll_freecb fcb, char ll->size++; return 0; } +// TODO: Figure out where the memory leak gcc keeps complaining about is & fix it + int dlinkedlist_append(dlinkedlist * const ll, void *data, dll_freecb fcb) { return dlinkedlist_xxxend(ll, data, fcb, 'a'); @@ -136,10 +132,8 @@ int dlinkedlist_prepend(dlinkedlist * const ll, void *data, dll_freecb fcb) { } dllnode * dlinkedlist_getnode(const dlinkedlist * const ll, int index) { - if(!ll) - RETURNWERR(EINVAL, NULL); - if(index < 0 || index >= ll->size) - RETURNWERR(EINVAL, NULL); + if(!ll) ERRRET(EINVAL, NULL); + if(index < 0 || index >= ll->size) ERRRET(EINVAL, NULL); if(index == 0) return ll->start; @@ -157,10 +151,8 @@ dllnode * dlinkedlist_getnode(const dlinkedlist * const ll, int index) { } int dlinkedlist_insert(dlinkedlist * const ll, void *data, dll_freecb fcb, int index) { - if(!ll) - RETURNWERR(EINVAL, 1); - if(index < 0 || index >= ll->size) - RETURNWERR(EINVAL, 1); + if(!ll) ERRRET(EINVAL, 1); + if(index < 0 || index >= ll->size) ERRRET(EINVAL, 1); // Handle the special cases of appending or prepending if(index == 0) @@ -188,10 +180,8 @@ int dlinkedlist_insert(dlinkedlist * const ll, void *data, dll_freecb fcb, int i } int dlinkedlist_remove(dlinkedlist * const ll, int index) { - if(!ll) - RETURNWERR(EINVAL, 1); - if(index < 0 || index >= ll->size) - RETURNWERR(EINVAL, 2); + if(!ll) ERRRET(EINVAL, 1); + if(index < 0 || index >= ll->size) ERRRET(EINVAL, 2); dllnode *current = dlinkedlist_getnode(ll, index); if(!current) @@ -226,10 +216,7 @@ void* dlinkedlist_get(const dlinkedlist * const ll, int index) { } int dlinkedlist_foreach(dlinkedlist *ll, int (*callback)(void*)) { - if(!ll) - RETURNWERR(EINVAL, -1); - if(!callback) - RETURNWERR(EINVAL, -1); + if(!ll || callback == NULL) ERRRET(EINVAL, -1); for(dllnode *p = ll->start; p != NULL; p = p->next) callback(p->data); @@ -238,10 +225,8 @@ int dlinkedlist_foreach(dlinkedlist *ll, int (*callback)(void*)) { } void * dlinkedlist_poplast(dlinkedlist *ll) { - if(!ll) - RETURNWERR(EINVAL, NULL); - if(dlinkedlist_isempty(ll)) - RETURNWERR(ENODATA, NULL); + if(!ll) ERRRET(EINVAL, NULL); + if(dlinkedlist_isempty(ll)) ERRRET(ENODATA, NULL); void *data = dlinkedlist_get(ll, ll->size - 1); dlinkedlist_remove(ll, ll->size - 1); diff --git a/src/scanner.c b/src/scanner.c index d20c714..ea35e3c 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -11,12 +11,14 @@ #include dlinkedlist * scandirlist(const char * const dir, int (*selector)(const struct dirent *), int (*cmp)(const struct dirent **, const struct dirent **)) { + if(!dir || selector == NULL || cmp == NULL) ERRRET(EINVAL, NULL); + struct dirent **namelist = NULL; dlinkedlist *list = NULL; int numentries = -1; if((numentries = scandir(dir, &namelist, selector, cmp)) < 0) - RETURNWERR(errno, NULL); + ERRRET(errno, NULL); list = dlinkedlist_init(); for(int i = 0; i < numentries; i++) @@ -26,7 +28,7 @@ dlinkedlist * scandirlist(const char * const dir, int (*selector)(const struct d free(namelist[j]); free(namelist); - RETURNWERR(errno, NULL); + ERRRET(errno, NULL); } free(namelist); diff --git a/src/shared.c b/src/shared.c index 1d5aa7f..e83261c 100644 --- a/src/shared.c +++ b/src/shared.c @@ -17,24 +17,23 @@ enum XALLOC_TYPE { }; void * xalloc(size_t nmemb, size_t size, enum XALLOC_TYPE actype, void *ptr) { - if(actype <= XALLOC_INVAL || actype >= XALLOC_2BIG) - RETURNWERR(EINVAL, NULL); + if(actype <= XALLOC_INVAL || actype >= XALLOC_2BIG) ERRRET(EINVAL, NULL); void *mem = NULL; switch(actype) { - case XALLOC_MALLOC: + case XALLOC_MALLOC: mem = malloc(nmemb * size); break; - case XALLOC_CALLOC: + case XALLOC_CALLOC: mem = calloc(nmemb, size); break; - case XALLOC_REALLOC: + case XALLOC_REALLOC: mem = realloc(ptr, nmemb * size); break; - - default: + + default: XALLOC_EXIT(" An unknown alloc type was passed, which shouldn't be possible", ); } @@ -61,7 +60,7 @@ int rwbuf(char **str, unsigned long int initsize, int fd) { // Bytes read == 0, return 0 // Bytes read < 0, free string, return -1; // When string hits capacity, double the capacity, and reallocate the string - + if(!str || initsize < 1) ERRRET(EINVAL, -1); const int ECODE = -100; char *lstr = NULL, *tmp = NULL; @@ -118,6 +117,8 @@ int rwbuf(char **str, unsigned long int initsize, int fd) { int wwbuf(int fd, const unsigned char *buf, int len) { + if(!buf || len <= 0) ERRRET(EINVAL, -1); + int total = 0; int left = len; int n = -1; @@ -225,7 +226,7 @@ char * xdirname(const char * const path) { int cleanup_init(cleanup *loc, fcallback callbacks[], void *arguments[], int size) { - if(!loc || !callbacks || !arguments || size <= 0) {errno = EINVAL; return -1;} + if(!loc || !callbacks || !arguments || size <= 0) ERRRET(EINVAL, -1); loc->callbacks = callbacks; loc->arguments = arguments; @@ -237,8 +238,8 @@ int cleanup_init(cleanup *loc, fcallback callbacks[], void *arguments[], int siz // registers if flag is NOT set int cleanup_register(cleanup *loc, fcallback cb, void *arg) { - if(!loc || !cb) {errno = EINVAL; return -1;} - if(loc->used >= loc->size || loc->used < 0) {errno = ENOMEM; return -1;} + if(!loc || !cb) ERRRET(EINVAL, -1); + if(loc->used >= loc->size || loc->used < 0) ERRRET(ENOMEM, -1); loc->callbacks[loc->used] = cb; loc->arguments[loc->used] = arg; @@ -248,20 +249,18 @@ int cleanup_register(cleanup *loc, fcallback cb, void *arg) { } int cleanup_cndregister(cleanup *loc, fcallback cb, void *arg, unsigned char flag) { - if(flag) - return 0; + if(flag) return 0; return cleanup_register(loc, cb, arg); } int cleanup_clear(cleanup *loc) { - if(!loc) {errno = EINVAL; return -1;} - + if(!loc) ERRRET(EINVAL, -1); loc->used = 0; return 0; } int cleanup_fire(cleanup *loc) { - if(!loc) {errno = EINVAL; return -1;} + if(!loc) ERRRET(EINVAL, -1); for(int i = (loc->used - 1); i >= 0; i--) { if(loc->callbacks[i] == NULL) { diff --git a/src/shared.h b/src/shared.h index adaad4a..0b401fe 100644 --- a/src/shared.h +++ b/src/shared.h @@ -8,7 +8,7 @@ typedef int (*gcallback)(void*); // Generic callback signature typedef void (*fcallback)(void*); // free()-like callback signature -#define RETURNWERR(errval, retval) do {\ +#define ERRRET(errval, retval) do {\ errno = (errval);\ return (retval);\ } while (0) diff --git a/src/threadpool.c b/src/threadpool.c index c4d8a5c..bd52c75 100644 --- a/src/threadpool.c +++ b/src/threadpool.c @@ -5,9 +5,37 @@ #include #include +typedef struct task { + gcallback callback; + fcallback freecb; + void *data; +} task; + +typedef struct tqnode { + struct tqnode *next; + struct tqnode *prev; + task *task; +} tqnode; + +typedef struct taskqueue { + tqnode *start; + tqnode *end; + unsigned int size; +} taskqueue; + +typedef struct ctqueue { + mtx_t mutex; + cnd_t cond; + unsigned char canceled; + + taskqueue *tq; + thrd_t *thrdarr; + int talen; +} ctqueue; + task * task_init(gcallback callback, fcallback freecb, void *data) { - if(callback == NULL) {errno = EINVAL; return NULL;} + if(callback == NULL) ERRRET(EINVAL, NULL); task *tsk = calloc(1, sizeof(*tsk)); if(!tsk) @@ -33,7 +61,7 @@ void task_free(void *tsk) { } int task_fire(task *tsk) { - if(!tsk) {errno = EINVAL; return -1;} + if(!tsk) ERRRET(EINVAL, -1); return tsk->callback(tsk->data); } @@ -46,7 +74,7 @@ int task_fired(task *tsk) { tqnode * tqnode_init(tqnode *next, tqnode *prev, task *tsk) { - if(!tsk) {errno = EINVAL; return NULL;} + if(!tsk) ERRRET(EINVAL, NULL); tqnode *node = calloc(1, sizeof(*node)); if(!node) return NULL; @@ -98,7 +126,7 @@ void taskqueue_free(void *tq) { } int taskqueue_handlefirst(taskqueue *tq, task *tsk) { - if(!tq || !tsk) {errno = EINVAL; return -1;} + if(!tq || !tsk) ERRRET(EINVAL, -1); if(tq->size) {return 0;} tqnode *first = tqnode_init(NULL, NULL, tsk); @@ -113,7 +141,7 @@ int taskqueue_handlefirst(taskqueue *tq, task *tsk) { } int taskqueue_push(taskqueue *tq, task *tsk) { - if(!tq || !tsk) {errno = EINVAL; return -1;} + if(!tq || !tsk) ERRRET(EINVAL, -1); int hf; if((hf = taskqueue_handlefirst(tq, tsk))) @@ -130,8 +158,8 @@ int taskqueue_push(taskqueue *tq, task *tsk) { } task * taskqueue_pop(taskqueue *tq) { - if(!tq) {errno = EINVAL; return NULL;} - if(tq->size <= 0) {errno = ENODATA; return NULL;} + if(!tq) ERRRET(EINVAL, NULL); + if(tq->size <= 0) ERRRET(ENODATA, NULL); tqnode *end = tq->end; task *ret = end->task; @@ -150,7 +178,7 @@ task * taskqueue_pop(taskqueue *tq) { } int taskqueue_pushfront(taskqueue *tq, task *tsk) { - if(!tq || !tsk) {errno = EINVAL; return -1;} + if(!tq || !tsk) ERRRET(EINVAL, -1); int hf; if((hf = taskqueue_handlefirst(tq, tsk))) @@ -167,8 +195,8 @@ int taskqueue_pushfront(taskqueue *tq, task *tsk) { } task * taskqueue_popback(taskqueue *tq) { - if(!tq) {errno = EINVAL; return NULL;} - if(tq->size <= 0) {errno = ENODATA; return NULL;} + if(!tq) ERRRET(EINVAL, NULL); + if(tq->size <= 0) ERRRET(ENODATA, NULL); tqnode *start = tq->start; task *ret = start->task; @@ -187,7 +215,7 @@ task * taskqueue_popback(taskqueue *tq) { } int taskqueue_size(taskqueue *tq) { - if(!tq) {errno = EINVAL; return -1;} + if(!tq) ERRRET(EINVAL, -1); return tq->size; } @@ -219,7 +247,7 @@ static void ___ucl_cnddestroy(void *cond) { } ctqueue * ctqueue_init(int nthreads) { - if(nthreads <= 0) {errno = EINVAL; return NULL;} + if(nthreads <= 0) ERRRET(EINVAL, NULL); cleanup_CREATE(6); ctqueue *ctq = calloc(1, sizeof(*ctq)); @@ -264,7 +292,7 @@ ctqueue * ctqueue_init(int nthreads) { } int ctqueue_cancel(ctqueue *ctq) { - if(!ctq) {errno = EINVAL; return -1;} + if(!ctq) ERRRET(EINVAL, -1); __CTQ_INLOCK(ctq, 1, ctq->canceled = 1; @@ -297,7 +325,7 @@ void ctqueue_free(void *ctq) { } int ctqueue_waitpush(ctqueue *ctq, task *tsk) { - if(!ctq || !tsk) {errno = EINVAL; return -1;} + if(!ctq || !tsk) ERRRET(EINVAL, -1); int retval = 0; __CTQ_INLOCK(ctq, -1, @@ -310,7 +338,7 @@ int ctqueue_waitpush(ctqueue *ctq, task *tsk) { } task * ctqueue_waitpop(ctqueue *ctq) { - if(!ctq) {errno = EINVAL; return NULL;} + if(!ctq) ERRRET(EINVAL, NULL); task *retval = NULL; __CTQ_INLOCK(ctq, NULL, @@ -318,9 +346,8 @@ task * ctqueue_waitpop(ctqueue *ctq) { cnd_wait(&ctq->cond, &ctq->mutex); if(ctq->canceled) { - errno = ECANCELED; mtx_unlock(&ctq->mutex); - return NULL; + ERRRET(ECANCELED, NULL); } retval = taskqueue_pop(ctq->tq); @@ -346,7 +373,7 @@ static int __CTQ_CONSUMER(void *ctq) { } int ctqueue_start(ctqueue *ctq) { - if(!ctq) {errno = EINVAL; return -1;} + if(!ctq) ERRRET(EINVAL, -1); ctq->canceled = 0; diff --git a/src/threadpool.h b/src/threadpool.h index 3048eea..7fda6f5 100644 --- a/src/threadpool.h +++ b/src/threadpool.h @@ -4,33 +4,9 @@ #include "shared.h" #include -typedef struct task { - gcallback callback; - fcallback freecb; - void *data; -} task; - -typedef struct tqnode { - struct tqnode *next; - struct tqnode *prev; - task *task; -} tqnode; - -typedef struct taskqueue { - tqnode *start; - tqnode *end; - unsigned int size; -} taskqueue; - -typedef struct ctqueue { - mtx_t mutex; - cnd_t cond; - unsigned char canceled; - - taskqueue *tq; - thrd_t *thrdarr; - int talen; -} ctqueue; +typedef struct task task; +typedef struct taskqueue taskqueue; +typedef struct ctqueue ctqueue; // Create a new task. Sets `errno` and returns `NULL` on error task * task_init(gcallback callback, fcallback freecb, void *data); -- cgit v1.2.3