matcher.c: properly handle negative array indizes
authorJo-Philipp Wich <jow@openwrt.org>
Thu, 19 Jun 2014 10:08:08 +0000 (12:08 +0200)
committerJo-Philipp Wich <jow@openwrt.org>
Thu, 19 Jun 2014 12:00:21 +0000 (14:00 +0200)
Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
matcher.c

index 7fb6d0224f748fffbc092a575f5bdad20f86de8e..ceb756e66573679840f4900dbc2ac7a064afa157 100644 (file)
--- a/matcher.c
+++ b/matcher.c
@@ -237,7 +237,8 @@ jp_match_next(struct jp_opcode *ptr,
               struct json_object *root, struct json_object *cur,
               jp_match_cb_t cb, void *priv)
 {
-       struct json_object *next;
+       int idx;
+       struct json_object *next = NULL;
 
        if (!ptr)
        {
@@ -257,7 +258,13 @@ jp_match_next(struct jp_opcode *ptr,
                break;
 
        case T_NUMBER:
-               next = json_object_array_get_idx(cur, ptr->num);
+               idx = ptr->num;
+
+               if (idx < 0)
+                       idx += json_object_array_length(cur);
+
+               if (idx >= 0)
+                       next = json_object_array_get_idx(cur, idx);
 
                if (next)
                        return jp_match_next(ptr->sibling, root, next, cb, priv);