aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/jsonfuncs.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2019-06-11 13:33:08 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2019-06-11 13:33:22 -0400
commit6f34fcbbd5ad5a6546710e7b90e6331cacfd36dc (patch)
tree70831673c203664f1f95e57804336aa26ba1e685 /src/backend/utils/adt/jsonfuncs.c
parent9f05c44ba4a4a6a857b957734bb369a2bb4dd62b (diff)
downloadpostgresql-6f34fcbbd5ad5a6546710e7b90e6331cacfd36dc.tar.gz
postgresql-6f34fcbbd5ad5a6546710e7b90e6331cacfd36dc.zip
Fix conversion of JSON strings to JSON output columns in json_to_record().
json_to_record(), when an output column is declared as type json or jsonb, should emit the corresponding field of the input JSON object. But it got this slightly wrong when the field is just a string literal: it failed to escape the contents of the string. That typically resulted in syntax errors if the string contained any double quotes or backslashes. jsonb_to_record() handles such cases correctly, but I added corresponding test cases for it too, to prevent future backsliding. Improve the documentation, as it provided only a very hand-wavy description of the conversion rules used by these functions. Per bug report from Robert Vollmert. Back-patch to v10 where the error was introduced (by commit cf35346e8). Note that PG 9.4 - 9.6 also get this case wrong, but differently so: they feed the de-escaped contents of the string literal to json[b]_in. That behavior is less obviously wrong, so possibly it's being depended on in the field, so I won't risk trying to make the older branches behave like the newer ones. Discussion: https://postgr.es/m/D6921B37-BD8E-4664-8D5F-DB3525765DCD@vllmrt.net
Diffstat (limited to 'src/backend/utils/adt/jsonfuncs.c')
-rw-r--r--src/backend/utils/adt/jsonfuncs.c37
1 files changed, 16 insertions, 21 deletions
diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c
index 28bdc7243fd..9e7035c71a1 100644
--- a/src/backend/utils/adt/jsonfuncs.c
+++ b/src/backend/utils/adt/jsonfuncs.c
@@ -2803,26 +2803,7 @@ populate_scalar(ScalarIOData *io, Oid typid, int32 typmod, JsValue *jsv)
json = jsv->val.json.str;
Assert(json);
-
- /* already done the hard work in the json case */
- if ((typid == JSONOID || typid == JSONBOID) &&
- jsv->val.json.type == JSON_TOKEN_STRING)
- {
- /*
- * Add quotes around string value (should be already escaped) if
- * converting to json/jsonb.
- */
-
- if (len < 0)
- len = strlen(json);
-
- str = palloc(len + sizeof(char) * 3);
- str[0] = '"';
- memcpy(&str[1], json, len);
- str[len + 1] = '"';
- str[len + 2] = '\0';
- }
- else if (len >= 0)
+ if (len >= 0)
{
/* Need to copy non-null-terminated string */
str = palloc(len + 1 * sizeof(char));
@@ -2830,7 +2811,21 @@ populate_scalar(ScalarIOData *io, Oid typid, int32 typmod, JsValue *jsv)
str[len] = '\0';
}
else
- str = json; /* null-terminated string */
+ str = json; /* string is already null-terminated */
+
+ /* If converting to json/jsonb, make string into valid JSON literal */
+ if ((typid == JSONOID || typid == JSONBOID) &&
+ jsv->val.json.type == JSON_TOKEN_STRING)
+ {
+ StringInfoData buf;
+
+ initStringInfo(&buf);
+ escape_json(&buf, str);
+ /* free temporary buffer */
+ if (str != json)
+ pfree(str);
+ str = buf.data;
+ }
}
else
{