alpm_list: abort on memory allocation failure
This makes it possible to detect a failure in several alpm_list functions. Previously these functions would continue after a failure, returning partial results and potentially leaking memory. Unfortunately, NULL is a valid return value for the affected functions if the input list is empty, so they still do not have a dedicated error value. Callers can at least detect an error by checking if the input list was empty. Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com> Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
parent
c44c649a52
commit
bcc9c417ae
1 changed files with 24 additions and 5 deletions
|
@ -461,7 +461,10 @@ alpm_list_t SYMEXPORT *alpm_list_remove_dupes(const alpm_list_t *list)
|
||||||
alpm_list_t *newlist = NULL;
|
alpm_list_t *newlist = NULL;
|
||||||
while(lp) {
|
while(lp) {
|
||||||
if(!alpm_list_find_ptr(newlist, lp->data)) {
|
if(!alpm_list_find_ptr(newlist, lp->data)) {
|
||||||
newlist = alpm_list_add(newlist, lp->data);
|
if(alpm_list_append(&newlist, lp->data) == NULL) {
|
||||||
|
alpm_list_free(newlist);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
lp = lp->next;
|
lp = lp->next;
|
||||||
}
|
}
|
||||||
|
@ -480,7 +483,10 @@ alpm_list_t SYMEXPORT *alpm_list_strdup(const alpm_list_t *list)
|
||||||
const alpm_list_t *lp = list;
|
const alpm_list_t *lp = list;
|
||||||
alpm_list_t *newlist = NULL;
|
alpm_list_t *newlist = NULL;
|
||||||
while(lp) {
|
while(lp) {
|
||||||
newlist = alpm_list_add(newlist, strdup(lp->data));
|
if(alpm_list_append_strdup(&newlist, lp->data) == NULL) {
|
||||||
|
FREELIST(newlist);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
lp = lp->next;
|
lp = lp->next;
|
||||||
}
|
}
|
||||||
return newlist;
|
return newlist;
|
||||||
|
@ -498,7 +504,10 @@ alpm_list_t SYMEXPORT *alpm_list_copy(const alpm_list_t *list)
|
||||||
const alpm_list_t *lp = list;
|
const alpm_list_t *lp = list;
|
||||||
alpm_list_t *newlist = NULL;
|
alpm_list_t *newlist = NULL;
|
||||||
while(lp) {
|
while(lp) {
|
||||||
newlist = alpm_list_add(newlist, lp->data);
|
if(alpm_list_append(&newlist, lp->data) == NULL) {
|
||||||
|
alpm_list_free(newlist);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
lp = lp->next;
|
lp = lp->next;
|
||||||
}
|
}
|
||||||
return newlist;
|
return newlist;
|
||||||
|
@ -523,8 +532,15 @@ alpm_list_t SYMEXPORT *alpm_list_copy_data(const alpm_list_t *list,
|
||||||
void *newdata = malloc(size);
|
void *newdata = malloc(size);
|
||||||
if(newdata) {
|
if(newdata) {
|
||||||
memcpy(newdata, lp->data, size);
|
memcpy(newdata, lp->data, size);
|
||||||
newlist = alpm_list_add(newlist, newdata);
|
if(alpm_list_append(&newlist, newdata) == NULL) {
|
||||||
|
free(newdata);
|
||||||
|
FREELIST(newlist);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
lp = lp->next;
|
lp = lp->next;
|
||||||
|
} else {
|
||||||
|
FREELIST(newlist);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return newlist;
|
return newlist;
|
||||||
|
@ -552,7 +568,10 @@ alpm_list_t SYMEXPORT *alpm_list_reverse(alpm_list_t *list)
|
||||||
list->prev = NULL;
|
list->prev = NULL;
|
||||||
|
|
||||||
while(lp) {
|
while(lp) {
|
||||||
newlist = alpm_list_add(newlist, lp->data);
|
if(alpm_list_append(&newlist, lp->data) == NULL) {
|
||||||
|
alpm_list_free(newlist);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
lp = lp->prev;
|
lp = lp->prev;
|
||||||
}
|
}
|
||||||
list->prev = backup; /* restore tail pointer */
|
list->prev = backup; /* restore tail pointer */
|
||||||
|
|
Loading…
Add table
Reference in a new issue