From e0a7f81b396c6a37a886a1679ddcd0969995d43e Mon Sep 17 00:00:00 2001 From: Petri Lehtinen Date: Tue, 4 Dec 2012 21:20:57 +0200 Subject: [PATCH] Really fix the off-by-one error in json_array_remove() It didn't affect only the last element but all of them. One item too much was always moved. --- src/value.c | 2 +- test/suites/api/test_array.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/value.c b/src/value.c index 2aae326..003b5f3 100644 --- a/src/value.c +++ b/src/value.c @@ -511,7 +511,7 @@ int json_array_remove(json_t *json, size_t index) /* If we're removing the last element, nothing has to be moved */ if(index < array->entries - 1) - array_move(array, index, index + 1, array->entries - index); + array_move(array, index, index + 1, array->entries - index - 1); array->entries--; diff --git a/test/suites/api/test_array.c b/test/suites/api/test_array.c index 3ff8f1a..b20637f 100644 --- a/test/suites/api/test_array.c +++ b/test/suites/api/test_array.c @@ -264,8 +264,8 @@ static void test_remove(void) if(json_array_size(array) != 8) fail("unable to append 8 items to array"); - /* Remove the last element from a "full" array. */ - json_array_remove(array, 7); + /* Remove an element from a "full" array. */ + json_array_remove(array, 5); json_decref(five); json_decref(seven);