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
31 changes: 27 additions & 4 deletions cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -662,12 +662,20 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
return true;
}

/* parse 4 digit hexadecimal number */
static unsigned parse_hex4(const unsigned char * const input)
/* parse 4 digit hexadecimal number
* On success, *ok (if not NULL) is set to 1 and the codepoint is returned.
* On invalid input, *ok is set to 0 and 0 is returned (so that a valid
* U+0000 can be distinguished from an invalid sequence). */
static unsigned parse_hex4(const unsigned char * const input, int *ok)
{
unsigned int h = 0;
size_t i = 0;

if (ok != NULL)
{
*ok = 1;
}

for (i = 0; i < 4; i++)
{
/* parse digit */
Expand All @@ -685,6 +693,10 @@ static unsigned parse_hex4(const unsigned char * const input)
}
else /* invalid */
{
if (ok != NULL)
{
*ok = 0;
}
return 0;
}

Expand All @@ -709,6 +721,7 @@ static unsigned char utf16_literal_to_utf8(const unsigned char * const input_poi
unsigned char utf8_position = 0;
unsigned char sequence_length = 0;
unsigned char first_byte_mark = 0;
int ok = 1;

if ((input_end - first_sequence) < 6)
{
Expand All @@ -717,7 +730,12 @@ static unsigned char utf16_literal_to_utf8(const unsigned char * const input_poi
}

/* get the first utf16 sequence */
first_code = parse_hex4(first_sequence + 2);
first_code = parse_hex4(first_sequence + 2, &ok);
if (!ok)
{
/* invalid hex digits in the first \uXXXX sequence */
goto fail;
}

/* check that the code is valid */
if (((first_code >= 0xDC00) && (first_code <= 0xDFFF)))
Expand Down Expand Up @@ -745,7 +763,12 @@ static unsigned char utf16_literal_to_utf8(const unsigned char * const input_poi
}

/* get the second utf16 sequence */
second_code = parse_hex4(second_sequence + 2);
second_code = parse_hex4(second_sequence + 2, &ok);
if (!ok)
{
/* invalid hex digits in the second \uXXXX sequence */
goto fail;
}
/* check that the code is valid */
if ((second_code < 0xDC00) || (second_code > 0xDFFF))
{
Expand Down
56 changes: 38 additions & 18 deletions tests/parse_hex4.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,41 +33,61 @@ static void parse_hex4_should_parse_all_combinations(void)
unsigned int number = 0;
unsigned char digits_lower[6];
unsigned char digits_upper[6];
int ok = 0;
/* test all combinations */
for (number = 0; number <= 0xFFFF; number++)
{
TEST_ASSERT_EQUAL_INT_MESSAGE(4, sprintf((char*)digits_lower, "%.4x", number), "sprintf failed.");
TEST_ASSERT_EQUAL_INT_MESSAGE(4, sprintf((char*)digits_upper, "%.4X", number), "sprintf failed.");

TEST_ASSERT_EQUAL_INT_MESSAGE(number, parse_hex4(digits_lower), "Failed to parse lowercase digits.");
TEST_ASSERT_EQUAL_INT_MESSAGE(number, parse_hex4(digits_upper), "Failed to parse uppercase digits.");
ok = 0;
TEST_ASSERT_EQUAL_INT_MESSAGE(number, parse_hex4(digits_lower, &ok), "Failed to parse lowercase digits.");
TEST_ASSERT_EQUAL_INT_MESSAGE(1, ok, "parse_hex4 reported failure on valid lowercase digits.");
ok = 0;
TEST_ASSERT_EQUAL_INT_MESSAGE(number, parse_hex4(digits_upper, &ok), "Failed to parse uppercase digits.");
TEST_ASSERT_EQUAL_INT_MESSAGE(1, ok, "parse_hex4 reported failure on valid uppercase digits.");
}
}

static void parse_hex4_should_parse_mixed_case(void)
{
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"beef"));
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"beeF"));
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"beEf"));
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"beEF"));
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"bEef"));
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"bEeF"));
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"bEEf"));
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"bEEF"));
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"Beef"));
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"BeeF"));
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"BeEf"));
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"BeEF"));
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"BEef"));
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"BEeF"));
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"BEEf"));
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"BEEF"));
int ok = 0;
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"beef", &ok)); TEST_ASSERT_EQUAL_INT(1, ok);
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"beeF", &ok)); TEST_ASSERT_EQUAL_INT(1, ok);
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"beEf", &ok)); TEST_ASSERT_EQUAL_INT(1, ok);
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"beEF", &ok)); TEST_ASSERT_EQUAL_INT(1, ok);
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"bEef", &ok)); TEST_ASSERT_EQUAL_INT(1, ok);
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"bEeF", &ok)); TEST_ASSERT_EQUAL_INT(1, ok);
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"bEEf", &ok)); TEST_ASSERT_EQUAL_INT(1, ok);
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"bEEF", &ok)); TEST_ASSERT_EQUAL_INT(1, ok);
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"Beef", &ok)); TEST_ASSERT_EQUAL_INT(1, ok);
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"BeeF", &ok)); TEST_ASSERT_EQUAL_INT(1, ok);
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"BeEf", &ok)); TEST_ASSERT_EQUAL_INT(1, ok);
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"BeEF", &ok)); TEST_ASSERT_EQUAL_INT(1, ok);
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"BEef", &ok)); TEST_ASSERT_EQUAL_INT(1, ok);
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"BEeF", &ok)); TEST_ASSERT_EQUAL_INT(1, ok);
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"BEEf", &ok)); TEST_ASSERT_EQUAL_INT(1, ok);
TEST_ASSERT_EQUAL_INT(0xBEEF, parse_hex4((const unsigned char*)"BEEF", &ok)); TEST_ASSERT_EQUAL_INT(1, ok);
}

static void parse_hex4_should_reject_invalid_digits(void)
{
int ok = 0;
TEST_ASSERT_EQUAL_INT(0, parse_hex4((const unsigned char*)"GGGG", &ok));
TEST_ASSERT_EQUAL_INT(0, ok);
ok = 0;
TEST_ASSERT_EQUAL_INT(0, parse_hex4((const unsigned char*)"0G12", &ok));
TEST_ASSERT_EQUAL_INT(0, ok);
ok = 0;
TEST_ASSERT_EQUAL_INT(0, parse_hex4((const unsigned char*)"----", &ok));
TEST_ASSERT_EQUAL_INT(0, ok);
}

int CJSON_CDECL main(void)
{
UNITY_BEGIN();
RUN_TEST(parse_hex4_should_parse_all_combinations);
RUN_TEST(parse_hex4_should_parse_mixed_case);
RUN_TEST(parse_hex4_should_reject_invalid_digits);
return UNITY_END();
}
16 changes: 16 additions & 0 deletions tests/parse_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@ static void parse_string_should_parse_bug_94(void)
reset(item);
}

static void parse_string_should_reject_invalid_unicode_escape(void)
{
cJSON *item = NULL;

/* invalid hex digits in the first \uXXXX must be rejected */
item = cJSON_Parse("\"\\uGGGG\"");
TEST_ASSERT_NULL_MESSAGE(item, "Invalid \\uXXXX (first half) must be rejected.");
cJSON_Delete(item);

/* invalid hex digits in the second half of a surrogate pair must be rejected */
item = cJSON_Parse("\"\\uD834\\uXXXX\"");
TEST_ASSERT_NULL_MESSAGE(item, "Invalid \\uXXXX (second half of surrogate) must be rejected.");
cJSON_Delete(item);
}

int CJSON_CDECL main(void)
{
/* initialize cJSON item and error pointer */
Expand All @@ -131,5 +146,6 @@ int CJSON_CDECL main(void)
RUN_TEST(parse_string_should_not_parse_invalid_backslash);
RUN_TEST(parse_string_should_parse_bug_94);
RUN_TEST(parse_string_should_not_overflow_with_closing_backslash);
RUN_TEST(parse_string_should_reject_invalid_unicode_escape);
return UNITY_END();
}