cJSON

mirror of Dave's cJSON
git clone git://git.thc420.xyz/cJSON
Log | Files | Refs | README | LICENSE

commit 569aa060c6319fb24ab21bbf699aad2c40f454c1
parent b9cc911831b0b3e1bb72f142389428e59f882b38
Author: Max Bruckner <max@maxbruckner.de>
Date:   Tue, 13 Jun 2017 08:39:18 +0200

cJSON_Compare: Fix comparison of arrays

It did consider two arrays equal if one is a prefix of the other one,
which is incorrect.

See #180

Diffstat:
McJSON.c | 5+++++
Mtests/compare_tests.c | 4++++
2 files changed, 9 insertions(+), 0 deletions(-)

diff --git a/cJSON.c b/cJSON.c @@ -2594,6 +2594,11 @@ CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * cons b_element = b_element->next; } + /* one of the arrays is longer than the other */ + if (a_element != b_element) { + return false; + } + return true; } diff --git a/tests/compare_tests.c b/tests/compare_tests.c @@ -148,6 +148,10 @@ static void cjson_compare_should_compare_arrays(void) TEST_ASSERT_FALSE(compare_from_string("[true,null,42,\"string\",[],{}]", "[false, true, null, 42, \"string\", [], {}]", true)); TEST_ASSERT_FALSE(compare_from_string("[true,null,42,\"string\",[],{}]", "[false, true, null, 42, \"string\", [], {}]", false)); + + /* Arrays that are a prefix of another array */ + TEST_ASSERT_FALSE(compare_from_string("[1,2,3]", "[1,2]", true)); + TEST_ASSERT_FALSE(compare_from_string("[1,2,3]", "[1,2]", false)); } static void cjson_compare_should_compare_objects(void)