You've already forked pgbackrest
mirror of
https://github.com/pgbackrest/pgbackrest.git
synced 2026-10-23 21:28:56 +03:00
Reject unexpected commas when reading JSON.
jsonReadTypeNextIgnoreComma() looks past a comma to find the type of the next value, but jsonReadPush() only consumed a comma between values in a container. A comma before a value outside a container, before the first value in a container, or before an object value was left in place. The string readers asserted on it in debug builds and read it as the opening quote of a string in production builds. Error on a comma in these positions.
This commit is contained in:
@@ -270,6 +270,10 @@ jsonReadPush(JsonRead *const this, const JsonType type, const bool key)
|
||||
{
|
||||
ASSERT(!key);
|
||||
|
||||
// No comma before a value outside a container
|
||||
if (*this->json == ',')
|
||||
THROW_FMT(JsonFormatError, "unexpected comma at: %s", this->json);
|
||||
|
||||
// If a scalar then no need to create the stack since the read is complete
|
||||
if (jsonTypeScalar(type))
|
||||
{
|
||||
@@ -323,7 +327,13 @@ jsonReadPush(JsonRead *const this, const JsonType type, const bool key)
|
||||
jsonReadConsumeWhiteSpace(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
// No comma before the first value in a container or before an object value
|
||||
if (*this->json == ',')
|
||||
THROW_FMT(JsonFormatError, "unexpected comma at: %s", this->json);
|
||||
|
||||
item->first = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Add container to the stack
|
||||
|
||||
@@ -35,6 +35,18 @@ testRun(void)
|
||||
jsonReadConsumeWhiteSpace(read);
|
||||
TEST_ERROR(jsonReadBool(read), JsonFormatError, "missing comma at: true]");
|
||||
|
||||
// -------------------------------------------------------------------------------------------------------------------------
|
||||
TEST_TITLE("error on unexpected comma");
|
||||
|
||||
TEST_ERROR(jsonReadStr(jsonReadNew(STRDEF(" ,\"a\""))), JsonFormatError, "unexpected comma at: ,\"a\"");
|
||||
|
||||
TEST_ASSIGN(read, jsonReadNew(STRDEF("[,true]")), "new read");
|
||||
|
||||
TEST_RESULT_VOID(jsonReadArrayBegin(read), "array begin");
|
||||
TEST_ERROR(jsonReadBool(read), JsonFormatError, "unexpected comma at: ,true]");
|
||||
|
||||
TEST_ERROR(jsonValidate(STRDEF("{\"a\":,\"b\"}")), JsonFormatError, "unexpected comma at: ,\"b\"}");
|
||||
|
||||
// -------------------------------------------------------------------------------------------------------------------------
|
||||
TEST_TITLE("error on mismatched container end");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user