summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLukas Fleischer <calcurse@cryptocrack.de>2012-02-16 08:44:55 +0100
committerLukas Fleischer <calcurse@cryptocrack.de>2012-02-16 08:44:55 +0100
commitf73f50055987a5ba644ab9721741ef1b9861b079 (patch)
treed7d38e86fd6609c1bde5f7e7dc3d70f76ea5c1b4
parentdfc98b5fa1f7744b17ead661f2919f5b7c2105a7 (diff)
downloadcalcurse-f73f50055987a5ba644ab9721741ef1b9861b079.tar.gz
calcurse-f73f50055987a5ba644ab9721741ef1b9861b079.zip
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>
-rw-r--r--src/llist.c8
1 files changed, 7 insertions, 1 deletions
diff --git a/src/llist.c b/src/llist.c
index a85cfa9..e560408 100644
--- a/src/llist.c
+++ b/src/llist.c
@@ -99,7 +99,10 @@ llist_nth (llist_t *l, int n)
{
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;
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;
+ if (n < 0)
+ return NULL;
+
for (i = l->head; i; i = i->next)
{
if (fn_match (i->data, data) && (n-- == 0))