Skip to content
Extraits de code Groupes Projets
Vérifiée Valider a0b0b2db rédigé par Kubat's avatar Kubat
Parcourir les fichiers

MISC: Abort on border case errors with the stack.

In case of invalid parameter and out of memory, it is preferable to exit
before continuing on a strange and invalid state.
parent 4dcac1ab
Branches
Étiquettes
1 requête de fusion!157Caching for karas & refactor & others
...@@ -7,23 +7,33 @@ extern "C" { ...@@ -7,23 +7,33 @@ extern "C" {
#include <stddef.h> #include <stddef.h>
// clang-format off
struct stack { struct stack {
volatile void **contents; volatile void **contents;
volatile size_t size; volatile size_t size;
volatile size_t len; volatile size_t len;
}; };
int stack_new (struct stack *ret); /* Create a new stack. Can't fail because it will avort on out of memory or
* invalid parameter. */
void stack_new(struct stack *ret);
/* Free a stack */
void stack_free(struct stack *lst); void stack_free(struct stack *lst);
/* Check if a stack is empty, return 1 if it is empty or invalid, 0 otherwise. */
int stack_empty(struct stack *lst); int stack_empty(struct stack *lst);
int stack_push(struct stack *lst, void *item); /* Push an item to the stack. If it failed (incorrect lst parameter or out of
* memory), will abort. */
void stack_push(struct stack *lst, void *item);
/* Pop an item from the stack, if the pointer was retreived with malloc, you
* must free it yourself. If no item is available will return 1, and return 0
* uppon successfull pop. */
int stack_pop(struct stack *lst, void **item); int stack_pop(struct stack *lst, void **item);
int stack_head(struct stack *lst, void **item);
// clang-format on /* Peek the head of the stack, don't free the pointer if it was allocated! */
int stack_head(struct stack *lst, void **item);
#if defined(__cplusplus) #if defined(__cplusplus)
} }
......
...@@ -4,11 +4,10 @@ ...@@ -4,11 +4,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
int void
stack_new(struct stack *ret) stack_new(struct stack *ret)
{ {
if (!ret) assert(ret);
return 1;
struct stack _ret = { struct stack _ret = {
.contents = safe_malloc(LKT_DEFAULT_LIST_SIZE * sizeof(void *)), .contents = safe_malloc(LKT_DEFAULT_LIST_SIZE * sizeof(void *)),
...@@ -16,11 +15,7 @@ stack_new(struct stack *ret) ...@@ -16,11 +15,7 @@ stack_new(struct stack *ret)
.len = 0, .len = 0,
}; };
if (_ret.contents == NULL)
return 1;
*ret = _ret; *ret = _ret;
return 0;
} }
void void
...@@ -44,11 +39,10 @@ stack_empty(struct stack *lst) ...@@ -44,11 +39,10 @@ stack_empty(struct stack *lst)
return 0; return 0;
} }
int void
stack_push(struct stack *lst, void *item) stack_push(struct stack *lst, void *item)
{ {
if (!lst) assert(lst);
return 1;
if (lst->size == lst->len) { if (lst->size == lst->len) {
volatile void **new = realloc(lst->contents, lst->size * 2 * sizeof(void *)); volatile void **new = realloc(lst->contents, lst->size * 2 * sizeof(void *));
...@@ -63,7 +57,6 @@ stack_push(struct stack *lst, void *item) ...@@ -63,7 +57,6 @@ stack_push(struct stack *lst, void *item)
} }
lst->contents[(lst->len)++] = item; lst->contents[(lst->len)++] = item;
return 0;
} }
int int
......
...@@ -65,16 +65,15 @@ __inc(volatile sqlite3 *db, const char *name, bool check) ...@@ -65,16 +65,15 @@ __inc(volatile sqlite3 *db, const char *name, bool check)
{ {
SQLITE_EXEC(db, "UPDATE misc SET opened = (SELECT opened + 1 FROM misc);", error); SQLITE_EXEC(db, "UPDATE misc SET opened = (SELECT opened + 1 FROM misc);", error);
if (db_stack_init == 0 && stack_new(&db_stack)) { if (db_stack_init == 0) {
exit(EXIT_FAILURE); stack_new(&db_stack);
}
db_stack_init = 1; db_stack_init = 1;
}
struct named_db *item = safe_malloc(sizeof(struct stack)); struct named_db *item = safe_malloc(sizeof(struct stack));
item->name = strdup(name); item->name = strdup(name);
item->db = db; item->db = db;
if (stack_push(&db_stack, item)) stack_push(&db_stack, item);
exit(EXIT_FAILURE);
return; return;
error: error:
LOG_ERROR("DB", "Database already in use"); LOG_ERROR("DB", "Database already in use");
......
...@@ -56,8 +56,8 @@ poller_new(struct poller_thread *th, void *(*func)(struct poller_thread_arg *), ...@@ -56,8 +56,8 @@ poller_new(struct poller_thread *th, void *(*func)(struct poller_thread_arg *),
.output_lock = mtx2, .output_lock = mtx2,
}; };
if (stack_new(&th_.input) || stack_new(&th_.output)) stack_new(&th_.input);
goto out_of_memory; stack_new(&th_.output);
struct ___poller_thread_args *__args; struct ___poller_thread_args *__args;
*th = th_; *th = th_;
...@@ -73,12 +73,6 @@ poller_new(struct poller_thread *th, void *(*func)(struct poller_thread_arg *), ...@@ -73,12 +73,6 @@ poller_new(struct poller_thread *th, void *(*func)(struct poller_thread_arg *),
LOG_INFO("THREAD", "Create a new poller thread"); LOG_INFO("THREAD", "Create a new poller thread");
return 0; return 0;
out_of_memory:
LOG_ERROR("MEMORY", "Out of memory");
stack_free(&th_.input);
stack_free(&th_.output);
return 1;
} }
UNUSED static int UNUSED static int
...@@ -101,9 +95,9 @@ UNUSED static int ...@@ -101,9 +95,9 @@ UNUSED static int
th_append(struct stack *lst, pthread_mutex_t *lock, void *ptr) th_append(struct stack *lst, pthread_mutex_t *lock, void *ptr)
{ {
RETURN_IF(pthread_mutex_lock(lock), "Failed to lock", 1); RETURN_IF(pthread_mutex_lock(lock), "Failed to lock", 1);
int ret = stack_push(lst, ptr); stack_push(lst, ptr);
RETURN_IF(pthread_mutex_unlock(lock), "Failed to lock", 1); RETURN_IF(pthread_mutex_unlock(lock), "Failed to lock", 1);
return ret; return 0;
} }
UNUSED static int UNUSED static int
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter