From f73f50055987a5ba644ab9721741ef1b9861b079 Mon Sep 17 00:00:00 2001
From: Lukas Fleischer <calcurse@cryptocrack.de>
Date: Thu, 16 Feb 2012 08:44:55 +0100
Subject: 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>
---
 src/llist.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

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))
-- 
cgit v1.2.3-70-g09d2