Error in jsonToVar() when input not entirely consumed.

Something like 3="string" would return an Int64 variant and ignore the invalid portion after the integer. Other JSON interface functions have this check but it was forgotten here.

There are no current issues because of this but we want to be able to validate arbitrary JSON strings and this function was not working correctly for that usage.
This commit is contained in:
David Steele
2020-07-24 10:47:51 -04:00
parent 78ef442a18
commit 9d8f6d9fc2
2 changed files with 7 additions and 1 deletions
+6 -1
View File
@@ -584,7 +584,12 @@ jsonToVar(const String *json)
const char *jsonPtr = strPtr(json);
unsigned int jsonPos = 0;
FUNCTION_LOG_RETURN(VARIANT, jsonToVarInternal(jsonPtr, &jsonPos));
Variant *result = jsonToVarInternal(jsonPtr, &jsonPos);
if (jsonPos != strSize(json))
THROW_FMT(JsonFormatError, "unexpected characters after JSON at '%s'", strPtr(json) + jsonPos);
FUNCTION_LOG_RETURN(VARIANT, result);
}
/**********************************************************************************************************************************/
+1
View File
@@ -72,6 +72,7 @@ testRun(void)
TEST_ERROR(jsonToVar(strNew("")), JsonFormatError, "expected data");
TEST_ERROR(jsonToVar(strNew(" \t\r\n ")), JsonFormatError, "expected data");
TEST_ERROR(jsonToVar(strNew("z")), JsonFormatError, "invalid type at 'z'");
TEST_ERROR(jsonToVar(strNew("3 =")), JsonFormatError, "unexpected characters after JSON at '='");
// -------------------------------------------------------------------------------------------------------------------------
TEST_RESULT_STR_Z(varStr(jsonToVar(strNew(" \"test\""))), "test", "simple string");