From 5eac12188a848bc23f2f237a887be970e63d2083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=95=88=EB=AF=BC=EA=B8=B0?= <205003064+Sn0wyDay@users.noreply.github.com> Date: Sun, 13 Sep 2026 02:05:53 +0900 Subject: [PATCH] Fix parsing of exact-length JSON with a UTF-8 BOM --- cJSON.c | 2 +- tests/parse_examples.c | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/cJSON.c b/cJSON.c index 88c2d95b..46f21d9b 100644 --- a/cJSON.c +++ b/cJSON.c @@ -1120,7 +1120,7 @@ static parse_buffer *skip_utf8_bom(parse_buffer * const buffer) return NULL; } - if (can_access_at_index(buffer, 4) && (strncmp((const char*)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0)) + if (can_access_at_index(buffer, 2) && (strncmp((const char*)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0)) { buffer->offset += 3; } diff --git a/tests/parse_examples.c b/tests/parse_examples.c index d35d6cfb..9df2ebd1 100644 --- a/tests/parse_examples.c +++ b/tests/parse_examples.c @@ -277,6 +277,18 @@ static void test15_should_not_heap_buffer_overflow(void) } } +static void test16_should_parse_short_json_with_bom_without_null_termination(void) +{ + const char json[] = { (char)0xef, (char)0xbb, (char)0xbf, '0' }; + cJSON *tree = cJSON_ParseWithLength(json, sizeof(json)); + + TEST_ASSERT_NOT_NULL_MESSAGE(tree, "Failed to parse valid short JSON with a UTF-8 BOM."); + TEST_ASSERT_TRUE(cJSON_IsNumber(tree)); + TEST_ASSERT_EQUAL_DOUBLE(0, tree->valuedouble); + + cJSON_Delete(tree); +} + int CJSON_CDECL main(void) { UNITY_BEGIN(); @@ -295,5 +307,6 @@ int CJSON_CDECL main(void) RUN_TEST(test13_should_be_parsed_without_null_termination); RUN_TEST(test14_should_not_be_parsed); RUN_TEST(test15_should_not_heap_buffer_overflow); + RUN_TEST(test16_should_parse_short_json_with_bom_without_null_termination); return UNITY_END(); }