src/llist.c: Bail out early on negative indexes

Make sure we don't return bogus list elements if negative indexes are
used in llist_{,find_}nth(). Bail out early and return NULL instead.

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
This commit is contained in:
Lukas Fleischer 2012-02-16 08:44:55 +01:00
parent dfc98b5fa1
commit f73f500559

View File

@ -99,7 +99,10 @@ llist_nth (llist_t *l, int n)
{ {
llist_item_t *i; llist_item_t *i;
for (i = l->head; i && n > 0; n--) if (n < 0)
return NULL;
for (i = l->head; i && n != 0; n--)
i = i->next; i = i->next;
return i; return i;
@ -267,6 +270,9 @@ llist_find_nth (llist_t *l, int n, long data, llist_fn_match_t fn_match)
{ {
llist_item_t *i; llist_item_t *i;
if (n < 0)
return NULL;
for (i = l->head; i; i = i->next) for (i = l->head; i; i = i->next)
{ {
if (fn_match (i->data, data) && (n-- == 0)) if (fn_match (i->data, data) && (n-- == 0))