Skip to content

Commit

Permalink
Error in jsonToVar() when input not entirely consumed.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dwsteele committed Jul 24, 2020
1 parent 78ef442 commit 9d8f6d9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/common/type/json.c
Expand Up @@ -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);
}

/**********************************************************************************************************************************/
Expand Down
1 change: 1 addition & 0 deletions test/src/module/common/typeJsonTest.c
Expand Up @@ -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");
Expand Down

0 comments on commit 9d8f6d9

Please sign in to comment.