From e3a9e5e57cdaeb4b71112cff014f6e6d90e5a94f Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Sun, 3 Jun 2018 21:52:11 +0200 Subject: Fixed bug when searching tail of list When searching tail of list, you cannot use directly input parameter (*l) , you should use a local variable instead (here new_elt). Change-Id: I13fc042f85580342ecf33cca2067a37ee55f4799 Signed-off-by: Sebastien Douheret --- .vscode/tasks.json | 47 +++++++++++++---- conf.d/cmake/config.cmake | 2 +- src/utils/list.c | 129 ++++++++++++++++++++++------------------------ 3 files changed, 100 insertions(+), 78 deletions(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 9b4e080..07b0deb 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -1,12 +1,39 @@ { - // See https://go.microsoft.com/fwlink/?LinkId=733558 - // for the documentation about the tasks.json format - "version": "2.0.0", - "tasks": [ - { - "label": "first_run", - "type": "shell", - "command": "rm -f ${workspaceFolder}/build/package/last_db_read" - } - ] + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "first_run", + "type": "shell", + "command": "rm -f ${workspaceFolder}/build/package/last_db_read" + }, + { + "label": "Initial Build", + "type": "shell", + "command": "rm -rf build && mkdir -p build && cd build && pwd && cmake ..", + "problemMatcher": [ + "$gcc" + ] + }, + { + "label": "Build", + "type": "shell", + "command": "clear && cd build && make", + "presentation": { + "echo": true, + //"reveal": "silent", + "reveal": "always", + "focus": false, + "panel": "shared" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + } + } + ] } diff --git a/conf.d/cmake/config.cmake b/conf.d/cmake/config.cmake index d131aca..8fb6e10 100644 --- a/conf.d/cmake/config.cmake +++ b/conf.d/cmake/config.cmake @@ -41,7 +41,7 @@ set(PROJECT_APP_TEMPLATES_DIR "conf.d/app-templates") # Compilation Mode (DEBUG, RELEASE) # ---------------------------------- -#set(CMAKE_BUILD_TYPE "DEBUG") +set(CMAKE_BUILD_TYPE "DEBUG") #set(USE_EFENCE 1) # Helpers Submodule parameters diff --git a/src/utils/list.c b/src/utils/list.c index d47f761..c5152a3 100644 --- a/src/utils/list.c +++ b/src/utils/list.c @@ -17,96 +17,91 @@ #include "list.h" #include "string.h" +#include +#include -void destroy_list(struct list *l) +void destroy_list(struct list* l) { - struct list *save; - while(l != NULL) { - save = l->next; - free(l); - l = save; - } + struct list* save; + while (l != NULL) { + save = l->next; + free(l); + l = save; + } } -void add_elt(struct list **l, const char *key, json_object *value) +void add_elt(struct list** l, const char* key, json_object* value) { - struct list *new_elt = malloc(sizeof(struct list)); - new_elt->key = key; - new_elt->value = value; - new_elt->next = NULL; + struct list* new_elt; - if(*l) { - while((*l)->next != NULL) { - *l = (*l)->next; - } - (*l)->next = new_elt; - } - else { - *l = new_elt; - } + // search tail + new_elt = *l; + while (new_elt != NULL) { + l = &new_elt->next; + new_elt = new_elt->next; + } + + // alloc new elem + new_elt = malloc(sizeof(struct list)); + if (!new_elt) { + fprintf(stderr, "OUT of memory"); + exit(1); + } + + *l = new_elt; + new_elt->key = key; + new_elt->value = value; + new_elt->next = NULL; } -void add_key(struct list **l, const char *key) +void add_key(struct list** l, const char* key) { - struct list *new_elt = malloc(sizeof(struct list)); - new_elt->key = key; - new_elt->value = NULL; - new_elt->next = NULL; - - if(*l) { - while((*l)->next != NULL) { - *l = (*l)->next; - } - (*l)->next = new_elt; - } - else { - *l = new_elt; - } + add_elt(l, key, NULL); } -int set_value(struct list *l, json_object *val, int index) +int set_value(struct list* l, json_object* val, int index) { - int i; + int i; - for (i = 0; i < index; i++) { - l = l->next; - if ( l == NULL ) - return -1; - } + for (i = 0; i < index; i++) { + l = l->next; + if (l == NULL) + return -1; + } - l->value = val; - return 0; + l->value = val; + return 0; } -struct list *get_elt(struct list *l, int index) +struct list* get_elt(struct list* l, int index) { - int i; + int i; - for (i = 0; i < index; i++) { - l = l->next; - if ( l == NULL ) - return NULL; - } + for (i = 0; i < index; i++) { + l = l->next; + if (l == NULL) + return NULL; + } - return l; + return l; } -struct list *find_elt_from_key(struct list *l, const char *key) +struct list* find_elt_from_key(struct list* l, const char* key) { - while(l != NULL) { - if(strcasecmp(l->key, key) == 0) - return l; - l = l->next; - } - return NULL; + while (l != NULL) { + if (strcasecmp(l->key, key) == 0) + return l; + l = l->next; + } + return NULL; } -json_object *find_key_value(struct list *l, const char *key) +json_object* find_key_value(struct list* l, const char* key) { - while(l != NULL) { - if(strcasecmp(l->key, key) == 0) - return l->value; - l = l->next; - } - return NULL; + while (l != NULL) { + if (strcasecmp(l->key, key) == 0) + return l->value; + l = l->next; + } + return NULL; } -- cgit 1.2.3-korg