--- a/tests/test_json.c Wed Jan 22 20:36:10 2025 +0100 +++ b/tests/test_json.c Wed Jan 22 21:02:46 2025 +0100 @@ -143,6 +143,39 @@ cxJsonDestroy(&json); } +CX_TEST(test_json_escaped_end_of_string) { + CxJson json; + cxJsonInit(&json, NULL); + CX_TEST_DO { + // first test, normal scenario + cxJsonFill(&json, "\"a \\\"test\\\" string\""); + CxJsonValue *val; + CxJsonStatus result = cxJsonNext(&json, &val); + CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); + CX_TEST_ASSERT(cxJsonIsString(val)); + CX_TEST_ASSERT(0 == cx_strcmp( + cxJsonAsCxString(val), + cx_str("a \"test\" string")) + ); + cxJsonValueFree(val); + + // second test - uncompleted token with hanging escape char + cxJsonFill(&json, "\"a \\\"test\\"); + result = cxJsonNext(&json, &val); + CX_TEST_ASSERT(result == CX_JSON_INCOMPLETE_DATA); + cxJsonFill(&json, "\" string\""); + result = cxJsonNext(&json, &val); + CX_TEST_ASSERT(result == CX_JSON_NO_ERROR); + CX_TEST_ASSERT(cxJsonIsString(val)); + CX_TEST_ASSERT(0 == cx_strcmp( + cxJsonAsCxString(val), + cx_str("a \"test\" string")) + ); + cxJsonValueFree(val); + } + cxJsonDestroy(&json); +} + CX_TEST(test_json_object_incomplete_token) { cxstring text = cx_str( "{\"message\":\"success\" , \"__timestamp\":1729348561}"); @@ -1009,6 +1042,7 @@ cx_test_register(suite, test_json_init_default); cx_test_register(suite, test_json_simple_object); cx_test_register(suite, test_json_escaped_strings); + cx_test_register(suite, test_json_escaped_end_of_string); cx_test_register(suite, test_json_object_incomplete_token); cx_test_register(suite, test_json_token_wrongly_completed); cx_test_register(suite, test_json_object_error);