From 89f58decb6e0ecadb37ab77c471ec780203fcd17 Mon Sep 17 00:00:00 2001 From: Anderson Toshiyuki Sasaki Date: Tue, 15 Jan 2019 10:05:59 +0100 Subject: misc: Add NULL checks before accessing lists Signed-off-by: Anderson Toshiyuki Sasaki Reviewed-by: Andreas Schneider --- src/misc.c | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/misc.c b/src/misc.c index abb82425..070a7766 100644 --- a/src/misc.c +++ b/src/misc.c @@ -485,9 +485,17 @@ static struct ssh_iterator *ssh_iterator_new(const void *data){ } int ssh_list_append(struct ssh_list *list,const void *data){ - struct ssh_iterator *iterator=ssh_iterator_new(data); - if(!iterator) - return SSH_ERROR; + struct ssh_iterator *iterator = NULL; + + if (list == NULL) { + return SSH_ERROR; + } + + iterator = ssh_iterator_new(data); + if (iterator == NULL) { + return SSH_ERROR; + } + if(!list->end){ /* list is empty */ list->root=list->end=iterator; @@ -500,8 +508,13 @@ int ssh_list_append(struct ssh_list *list,const void *data){ } int ssh_list_prepend(struct ssh_list *list, const void *data){ - struct ssh_iterator *it = ssh_iterator_new(data); + struct ssh_iterator *it = NULL; + if (list == NULL) { + return SSH_ERROR; + } + + it = ssh_iterator_new(data); if (it == NULL) { return SSH_ERROR; } @@ -520,6 +533,11 @@ int ssh_list_prepend(struct ssh_list *list, const void *data){ void ssh_list_remove(struct ssh_list *list, struct ssh_iterator *iterator){ struct ssh_iterator *ptr,*prev; + + if (list == NULL) { + return; + } + prev=NULL; ptr=list->root; while(ptr && ptr != iterator){ @@ -554,10 +572,17 @@ void ssh_list_remove(struct ssh_list *list, struct ssh_iterator *iterator){ * if the list is empty. */ const void *_ssh_list_pop_head(struct ssh_list *list){ - struct ssh_iterator *iterator=list->root; - const void *data; - if(!list->root) - return NULL; + struct ssh_iterator *iterator = NULL; + const void *data = NULL; + + if (list == NULL) { + return NULL; + } + + iterator = list->root; + if (iterator == NULL) { + return NULL; + } data=iterator->data; list->root=iterator->next; if(list->end==iterator) -- cgit v1.2.3