1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-11-06 08:49:29 +02:00

Decode JSON \u escaped characters.

ASCII may occasionally be encoded (e.g. &) to prevent ambiguity depending on where the JSON is located.

Only ASCII can be decoded. In general Unicode should not be encoded in JSON.
This commit is contained in:
David Steele
2021-02-19 07:32:40 -05:00
parent c4243331de
commit dcb79ab8fb
2 changed files with 19 additions and 2 deletions

View File

@@ -254,7 +254,7 @@ jsonToStrInternal(const char *json, unsigned int *jsonPos)
noEscapeSize = 0; noEscapeSize = 0;
} }
(*jsonPos)++;; (*jsonPos)++;
switch (json[*jsonPos]) switch (json[*jsonPos])
{ {
@@ -290,6 +290,22 @@ jsonToStrInternal(const char *json, unsigned int *jsonPos)
strCatChr(result, '\f'); strCatChr(result, '\f');
break; break;
case 'u':
{
(*jsonPos)++;
// We don't know how to decode anything except ASCII so fail if it looks like Unicode
if (strncmp(json + *jsonPos, "00", 2) != 0)
THROW_FMT(JsonFormatError, "unable to decode '%.4s'", json + *jsonPos);
// Decode char
(*jsonPos) += 2;
strCatChr(result, (char)cvtZToUIntBase(strZ(strNewN(json + *jsonPos, 2)), 16));
(*jsonPos) += 1;
break;
}
default: default:
THROW_FMT(JsonFormatError, "invalid escape character '%c'", json[*jsonPos]); THROW_FMT(JsonFormatError, "invalid escape character '%c'", json[*jsonPos]);
} }

View File

@@ -36,6 +36,7 @@ testRun(void)
if (testBegin("jsonToStr() and jsonToStrInternal()")) if (testBegin("jsonToStr() and jsonToStrInternal()"))
{ {
TEST_ERROR(jsonToStr(strNew("\"\\j\"")), JsonFormatError, "invalid escape character 'j'"); TEST_ERROR(jsonToStr(strNew("\"\\j\"")), JsonFormatError, "invalid escape character 'j'");
TEST_ERROR(jsonToStr(strNew("\"\\uffff\"")), JsonFormatError, "unable to decode 'ffff'");
TEST_ERROR(jsonToStr(strNew("\"runonstring")), JsonFormatError, "expected '\"' but found null delimiter"); TEST_ERROR(jsonToStr(strNew("\"runonstring")), JsonFormatError, "expected '\"' but found null delimiter");
TEST_ERROR(jsonToStr(strNew("\"normal\"L")), JsonFormatError, "unexpected characters after string at 'L'"); TEST_ERROR(jsonToStr(strNew("\"normal\"L")), JsonFormatError, "unexpected characters after string at 'L'");
TEST_ERROR(jsonToStr(strNew("nullz")), JsonFormatError, "unexpected characters after string at 'z'"); TEST_ERROR(jsonToStr(strNew("nullz")), JsonFormatError, "unexpected characters after string at 'z'");
@@ -43,7 +44,7 @@ testRun(void)
TEST_RESULT_STR(jsonToStr(strNew("null")), NULL, "null string"); TEST_RESULT_STR(jsonToStr(strNew("null")), NULL, "null string");
TEST_RESULT_STR_Z(jsonToStr(strNew(" \"test\"")), "test", "simple string"); TEST_RESULT_STR_Z(jsonToStr(strNew(" \"test\"")), "test", "simple string");
TEST_RESULT_STR_Z(jsonToStr(strNew("\"te\\\"st\" ")), "te\"st", "string with quote"); TEST_RESULT_STR_Z(jsonToStr(strNew("\"te\\\"st\" ")), "te\"st", "string with quote");
TEST_RESULT_STR_Z(jsonToStr(strNew("\"\\\"\\\\\\/\\b\\n\\r\\t\\f\"")), "\"\\/\b\n\r\t\f", "string with escapes"); TEST_RESULT_STR_Z(jsonToStr(strNew("\"\\\"\\\\\\/\\b\\n\\r\\t\\f\\u0026\"")), "\"\\/\b\n\r\t\f&", "string with escapes");
} }
// ***************************************************************************************************************************** // *****************************************************************************************************************************