From bb61b4e2022bea1f8728d18dac18239ed6931826 Mon Sep 17 00:00:00 2001 From: pixdamix Date: Thu, 29 Oct 2009 05:07:11 -0400 Subject: Fix problems in error list push_error_list() should allocate the sizeof(struct) not sizeof(pointer to struct). And add some memory deallocation in error paths found while looking at this. git-svn-id: http://opkg.googlecode.com/svn/trunk@227 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358 --- (limited to 'libopkg/opkg_utils.c') diff --git a/libopkg/opkg_utils.c b/libopkg/opkg_utils.c index fb27b40..f0ef051 100644 --- a/libopkg/opkg_utils.c +++ b/libopkg/opkg_utils.c @@ -146,12 +146,26 @@ int line_is_blank(const char *line) return 1; } +/* + * XXX: this function should not allocate memory as it may be called to + * print an error because we are out of memory. + */ void push_error_list(struct errlist ** errors, char * msg){ struct errlist *err_lst_tmp; + err_lst_tmp = calloc (1, sizeof (struct errlist) ); + if (err_lst_tmp == NULL) { + fprintf(stderr, "%s: calloc: %s\n", __FUNCTION__, strerror(errno)); + return; + } + + err_lst_tmp->errmsg = strdup(msg); + if (err_lst_tmp->errmsg == NULL) { + fprintf(stderr, "%s: strdup: %s\n", __FUNCTION__, strerror(errno)); + free(err_lst_tmp); + return; + } - err_lst_tmp = calloc (1, sizeof (err_lst_tmp) ); - err_lst_tmp->errmsg=strdup(msg) ; err_lst_tmp->next = *errors; *errors = err_lst_tmp; } @@ -173,16 +187,16 @@ void reverse_error_list(struct errlist **errors){ } -void free_error_list(){ +void free_error_list(struct errlist **errors){ struct errlist *err_tmp_lst; - err_tmp_lst = error_list; + err_tmp_lst = *errors; while (err_tmp_lst != NULL) { free(err_tmp_lst->errmsg); err_tmp_lst = error_list->next; - free(error_list); - error_list = err_tmp_lst; + free(*errors); + *errors = err_tmp_lst; } -- cgit v0.9.1