pgbackrest.git / summary / log / commit / refs
commit dcb79ab8fb18c63c2d56ecacd9d680b999ea044b
Author: David Steele <david@pgmasters.net>
Date: Fri Feb 19 12:32:40 2021 +0000
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.
src/common/type/json.c | 18 +++++++++++++++++-
test/src/module/common/typeJsonTest.c | 3 ++-
2 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/src/common/type/json.c b/src/common/type/json.c
index 077d7bf2c..623c9ba56 100644
--- a/src/common/type/json.c
+++ b/src/common/type/json.c
@@ -254,7 +254,7 @@ jsonToStrInternal(const char *json, unsigned int *jsonPos)
noEscapeSize = 0;
}
- (*jsonPos)++;;
+ (*jsonPos)++;
switch (json[*jsonPos])
{
@@ -290,6 +290,22 @@ jsonToStrInternal(const char *json, unsigned int *jsonPos)
strCatChr(result, '\f');
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:
THROW_FMT(JsonFormatError, "invalid escape character '%c'", json[*jsonPos]);
}
diff --git a/test/src/module/common/typeJsonTest.c b/test/src/module/common/typeJsonTest.c
index 3945d1712..4e964d187 100644
--- a/test/src/module/common/typeJsonTest.c
+++ b/test/src/module/common/typeJsonTest.c
@@ -36,6 +36,7 @@ testRun(void)
if (testBegin("jsonToStr() and jsonToStrInternal()"))
{
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("\"normal\"L")), JsonFormatError, "unexpected characters after string at 'L'");
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_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("\"\\\"\\\\\\/\\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");
}
// *****************************************************************************************************************************
[parent: c4243331de25]