Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -3069,7 +3069,14 @@ CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item)
return (item->type & 0xFF) == cJSON_Raw;
}

static cJSON_bool compare_rec(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive, size_t depth);

CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive)
{
return compare_rec(a, b, case_sensitive, 0);
}

static cJSON_bool compare_rec(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive, size_t depth)
{
if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)))
{
Expand Down Expand Up @@ -3132,9 +3139,14 @@ CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * cons
cJSON *a_element = a->child;
cJSON *b_element = b->child;

if (depth > CJSON_NESTING_LIMIT)
{
return false;
}

for (; (a_element != NULL) && (b_element != NULL);)
{
if (!cJSON_Compare(a_element, b_element, case_sensitive))
if (!compare_rec(a_element, b_element, case_sensitive, depth + 1))
{
return false;
}
Expand All @@ -3155,6 +3167,12 @@ CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * cons
{
cJSON *a_element = NULL;
cJSON *b_element = NULL;

if (depth > CJSON_NESTING_LIMIT)
{
return false;
}

cJSON_ArrayForEach(a_element, a)
{
/* TODO This has O(n^2) runtime, which is horrible! */
Expand All @@ -3164,7 +3182,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * cons
return false;
}

if (!cJSON_Compare(a_element, b_element, case_sensitive))
if (!compare_rec(a_element, b_element, case_sensitive, depth + 1))
{
return false;
}
Expand All @@ -3180,7 +3198,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * cons
return false;
}

if (!cJSON_Compare(b_element, a_element, case_sensitive))
if (!compare_rec(b_element, a_element, case_sensitive, depth + 1))
{
return false;
}
Expand Down
55 changes: 55 additions & 0 deletions tests/compare_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,59 @@ static void cjson_compare_should_compare_objects(void)
false))
}

static cJSON *build_nested_array(size_t depth)
{
cJSON *root = cJSON_CreateArray();
cJSON *current = root;
size_t i = 0;

TEST_ASSERT_NOT_NULL_MESSAGE(root, "Failed to create root array.");

for (i = 0; i < depth; i++)
{
cJSON *child = cJSON_CreateArray();
TEST_ASSERT_NOT_NULL_MESSAGE(child, "Failed to create nested array.");
cJSON_AddItemToArray(current, child);
current = child;
}

return root;
}

static void cjson_compare_should_compare_deeply_nested_arrays(void)
{
cJSON *a = NULL;
cJSON *b = NULL;

/* CJSON_NESTING_LIMIT is the deepest nesting the parser accepts, so this is
* the deepest tree a caller can obtain from JSON text. It still has to
* compare equal, otherwise the guard would change behaviour for valid
* input. */
a = build_nested_array(CJSON_NESTING_LIMIT);
b = build_nested_array(CJSON_NESTING_LIMIT);

TEST_ASSERT_TRUE(cJSON_Compare(a, b, true));

cJSON_Delete(a);
cJSON_Delete(b);
}

static void cjson_compare_should_not_recurse_past_nesting_limit(void)
{
cJSON *a = NULL;
cJSON *b = NULL;

/* deeper than anything the parser can produce: reported as unequal instead
* of recursing until the stack is exhausted */
a = build_nested_array(CJSON_NESTING_LIMIT + 100);
b = build_nested_array(CJSON_NESTING_LIMIT + 100);

TEST_ASSERT_FALSE(cJSON_Compare(a, b, true));

cJSON_Delete(a);
cJSON_Delete(b);
}

int CJSON_CDECL main(void)
{
UNITY_BEGIN();
Expand All @@ -203,6 +256,8 @@ int CJSON_CDECL main(void)
RUN_TEST(cjson_compare_should_compare_raw);
RUN_TEST(cjson_compare_should_compare_arrays);
RUN_TEST(cjson_compare_should_compare_objects);
RUN_TEST(cjson_compare_should_compare_deeply_nested_arrays);
RUN_TEST(cjson_compare_should_not_recurse_past_nesting_limit);

return UNITY_END();
}