Skip to content
Open
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
2 changes: 1 addition & 1 deletion cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 13 additions & 0 deletions tests/parse_examples.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}