Convert json_in and jsonb_in to report errors softly.

This requires a bit of further infrastructure-extension to allow
trapping errors reported by numeric_in and pg_unicode_to_server,
but otherwise it's pretty straightforward.

In the case of jsonb_in, we are only capturing errors reported
during the initial "parse" phase.  The value-construction phase
(JsonbValueToJsonb) can also throw errors if assorted implementation
limits are exceeded.  We should improve that, but it seems like a
separable project.

Andrew Dunstan and Tom Lane

Discussion: https://postgr.es/m/3bac9841-fe07-713d-fa42-606c225567d6@dunslane.net
This commit is contained in:
Tom Lane 2022-12-11 11:28:15 -05:00
parent 50428a301d
commit c60c9badba
17 changed files with 282 additions and 46 deletions

View File

@ -81,9 +81,10 @@ json_in(PG_FUNCTION_ARGS)
/* validate it */ /* validate it */
lex = makeJsonLexContext(result, false); lex = makeJsonLexContext(result, false);
pg_parse_json_or_ereport(lex, &nullSemAction); if (!pg_parse_json_or_errsave(lex, &nullSemAction, fcinfo->context))
PG_RETURN_NULL();
/* Internal representation is the same as text, for now */ /* Internal representation is the same as text */
PG_RETURN_TEXT_P(result); PG_RETURN_TEXT_P(result);
} }
@ -1337,7 +1338,7 @@ json_typeof(PG_FUNCTION_ARGS)
/* Lex exactly one token from the input and check its type. */ /* Lex exactly one token from the input and check its type. */
result = json_lex(lex); result = json_lex(lex);
if (result != JSON_SUCCESS) if (result != JSON_SUCCESS)
json_ereport_error(result, lex); json_errsave_error(result, lex, NULL);
tok = lex->token_type; tok = lex->token_type;
switch (tok) switch (tok)
{ {

View File

@ -33,6 +33,7 @@ typedef struct JsonbInState
{ {
JsonbParseState *parseState; JsonbParseState *parseState;
JsonbValue *res; JsonbValue *res;
Node *escontext;
} JsonbInState; } JsonbInState;
/* unlike with json categories, we need to treat json and jsonb differently */ /* unlike with json categories, we need to treat json and jsonb differently */
@ -61,8 +62,8 @@ typedef struct JsonbAggState
Oid val_output_func; Oid val_output_func;
} JsonbAggState; } JsonbAggState;
static inline Datum jsonb_from_cstring(char *json, int len); static inline Datum jsonb_from_cstring(char *json, int len, Node *escontext);
static size_t checkStringLen(size_t len); static bool checkStringLen(size_t len, Node *escontext);
static JsonParseErrorType jsonb_in_object_start(void *pstate); static JsonParseErrorType jsonb_in_object_start(void *pstate);
static JsonParseErrorType jsonb_in_object_end(void *pstate); static JsonParseErrorType jsonb_in_object_end(void *pstate);
static JsonParseErrorType jsonb_in_array_start(void *pstate); static JsonParseErrorType jsonb_in_array_start(void *pstate);
@ -98,7 +99,7 @@ jsonb_in(PG_FUNCTION_ARGS)
{ {
char *json = PG_GETARG_CSTRING(0); char *json = PG_GETARG_CSTRING(0);
return jsonb_from_cstring(json, strlen(json)); return jsonb_from_cstring(json, strlen(json), fcinfo->context);
} }
/* /*
@ -122,7 +123,7 @@ jsonb_recv(PG_FUNCTION_ARGS)
else else
elog(ERROR, "unsupported jsonb version number %d", version); elog(ERROR, "unsupported jsonb version number %d", version);
return jsonb_from_cstring(str, nbytes); return jsonb_from_cstring(str, nbytes, NULL);
} }
/* /*
@ -251,9 +252,12 @@ jsonb_typeof(PG_FUNCTION_ARGS)
* Turns json string into a jsonb Datum. * Turns json string into a jsonb Datum.
* *
* Uses the json parser (with hooks) to construct a jsonb. * Uses the json parser (with hooks) to construct a jsonb.
*
* If escontext points to an ErrorSaveContext, errors are reported there
* instead of being thrown.
*/ */
static inline Datum static inline Datum
jsonb_from_cstring(char *json, int len) jsonb_from_cstring(char *json, int len, Node *escontext)
{ {
JsonLexContext *lex; JsonLexContext *lex;
JsonbInState state; JsonbInState state;
@ -263,6 +267,7 @@ jsonb_from_cstring(char *json, int len)
memset(&sem, 0, sizeof(sem)); memset(&sem, 0, sizeof(sem));
lex = makeJsonLexContextCstringLen(json, len, GetDatabaseEncoding(), true); lex = makeJsonLexContextCstringLen(json, len, GetDatabaseEncoding(), true);
state.escontext = escontext;
sem.semstate = (void *) &state; sem.semstate = (void *) &state;
sem.object_start = jsonb_in_object_start; sem.object_start = jsonb_in_object_start;
@ -272,23 +277,24 @@ jsonb_from_cstring(char *json, int len)
sem.scalar = jsonb_in_scalar; sem.scalar = jsonb_in_scalar;
sem.object_field_start = jsonb_in_object_field_start; sem.object_field_start = jsonb_in_object_field_start;
pg_parse_json_or_ereport(lex, &sem); if (!pg_parse_json_or_errsave(lex, &sem, escontext))
return (Datum) 0;
/* after parsing, the item member has the composed jsonb structure */ /* after parsing, the item member has the composed jsonb structure */
PG_RETURN_POINTER(JsonbValueToJsonb(state.res)); PG_RETURN_POINTER(JsonbValueToJsonb(state.res));
} }
static size_t static bool
checkStringLen(size_t len) checkStringLen(size_t len, Node *escontext)
{ {
if (len > JENTRY_OFFLENMASK) if (len > JENTRY_OFFLENMASK)
ereport(ERROR, ereturn(escontext, false,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("string too long to represent as jsonb string"), errmsg("string too long to represent as jsonb string"),
errdetail("Due to an implementation restriction, jsonb strings cannot exceed %d bytes.", errdetail("Due to an implementation restriction, jsonb strings cannot exceed %d bytes.",
JENTRY_OFFLENMASK))); JENTRY_OFFLENMASK)));
return len; return true;
} }
static JsonParseErrorType static JsonParseErrorType
@ -339,7 +345,9 @@ jsonb_in_object_field_start(void *pstate, char *fname, bool isnull)
Assert(fname != NULL); Assert(fname != NULL);
v.type = jbvString; v.type = jbvString;
v.val.string.len = checkStringLen(strlen(fname)); v.val.string.len = strlen(fname);
if (!checkStringLen(v.val.string.len, _state->escontext))
return JSON_SEM_ACTION_FAILED;
v.val.string.val = fname; v.val.string.val = fname;
_state->res = pushJsonbValue(&_state->parseState, WJB_KEY, &v); _state->res = pushJsonbValue(&_state->parseState, WJB_KEY, &v);
@ -390,7 +398,9 @@ jsonb_in_scalar(void *pstate, char *token, JsonTokenType tokentype)
case JSON_TOKEN_STRING: case JSON_TOKEN_STRING:
Assert(token != NULL); Assert(token != NULL);
v.type = jbvString; v.type = jbvString;
v.val.string.len = checkStringLen(strlen(token)); v.val.string.len = strlen(token);
if (!checkStringLen(v.val.string.len, _state->escontext))
return JSON_SEM_ACTION_FAILED;
v.val.string.val = token; v.val.string.val = token;
break; break;
case JSON_TOKEN_NUMBER: case JSON_TOKEN_NUMBER:
@ -401,10 +411,11 @@ jsonb_in_scalar(void *pstate, char *token, JsonTokenType tokentype)
*/ */
Assert(token != NULL); Assert(token != NULL);
v.type = jbvNumeric; v.type = jbvNumeric;
numd = DirectFunctionCall3(numeric_in, if (!DirectInputFunctionCallSafe(numeric_in, token,
CStringGetDatum(token), InvalidOid, -1,
ObjectIdGetDatum(InvalidOid), _state->escontext,
Int32GetDatum(-1)); &numd))
return JSON_SEM_ACTION_FAILED;
v.val.numeric = DatumGetNumeric(numd); v.val.numeric = DatumGetNumeric(numd);
break; break;
case JSON_TOKEN_TRUE: case JSON_TOKEN_TRUE:
@ -738,6 +749,9 @@ jsonb_categorize_type(Oid typoid,
* *
* If key_scalar is true, the value is stored as a key, so insist * If key_scalar is true, the value is stored as a key, so insist
* it's of an acceptable type, and force it to be a jbvString. * it's of an acceptable type, and force it to be a jbvString.
*
* Note: currently, we assume that result->escontext is NULL and errors
* will be thrown.
*/ */
static void static void
datum_to_jsonb(Datum val, bool is_null, JsonbInState *result, datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
@ -910,7 +924,8 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
default: default:
outputstr = OidOutputFunctionCall(outfuncoid, val); outputstr = OidOutputFunctionCall(outfuncoid, val);
jb.type = jbvString; jb.type = jbvString;
jb.val.string.len = checkStringLen(strlen(outputstr)); jb.val.string.len = strlen(outputstr);
(void) checkStringLen(jb.val.string.len, NULL);
jb.val.string.val = outputstr; jb.val.string.val = outputstr;
break; break;
} }
@ -1648,6 +1663,7 @@ jsonb_agg_finalfn(PG_FUNCTION_ARGS)
* shallow clone is sufficient as we aren't going to change any of the * shallow clone is sufficient as we aren't going to change any of the
* values, just add the final array end marker. * values, just add the final array end marker.
*/ */
memset(&result, 0, sizeof(JsonbInState));
result.parseState = clone_parse_state(arg->res->parseState); result.parseState = clone_parse_state(arg->res->parseState);
@ -1880,6 +1896,7 @@ jsonb_object_agg_finalfn(PG_FUNCTION_ARGS)
* going to change any of the values, just add the final object end * going to change any of the values, just add the final object end
* marker. * marker.
*/ */
memset(&result, 0, sizeof(JsonbInState));
result.parseState = clone_parse_state(arg->res->parseState); result.parseState = clone_parse_state(arg->res->parseState);

View File

@ -25,6 +25,7 @@
#include "lib/stringinfo.h" #include "lib/stringinfo.h"
#include "mb/pg_wchar.h" #include "mb/pg_wchar.h"
#include "miscadmin.h" #include "miscadmin.h"
#include "nodes/miscnodes.h"
#include "utils/array.h" #include "utils/array.h"
#include "utils/builtins.h" #include "utils/builtins.h"
#include "utils/fmgroids.h" #include "utils/fmgroids.h"
@ -490,21 +491,31 @@ static JsonParseErrorType transform_string_values_object_field_start(void *state
static JsonParseErrorType transform_string_values_array_element_start(void *state, bool isnull); static JsonParseErrorType transform_string_values_array_element_start(void *state, bool isnull);
static JsonParseErrorType transform_string_values_scalar(void *state, char *token, JsonTokenType tokentype); static JsonParseErrorType transform_string_values_scalar(void *state, char *token, JsonTokenType tokentype);
/* /*
* pg_parse_json_or_ereport * pg_parse_json_or_errsave
* *
* This function is like pg_parse_json, except that it does not return a * This function is like pg_parse_json, except that it does not return a
* JsonParseErrorType. Instead, in case of any failure, this function will * JsonParseErrorType. Instead, in case of any failure, this function will
* save error data into *escontext if that's an ErrorSaveContext, otherwise
* ereport(ERROR). * ereport(ERROR).
*
* Returns a boolean indicating success or failure (failure will only be
* returned when escontext is an ErrorSaveContext).
*/ */
void bool
pg_parse_json_or_ereport(JsonLexContext *lex, JsonSemAction *sem) pg_parse_json_or_errsave(JsonLexContext *lex, JsonSemAction *sem,
Node *escontext)
{ {
JsonParseErrorType result; JsonParseErrorType result;
result = pg_parse_json(lex, sem); result = pg_parse_json(lex, sem);
if (result != JSON_SUCCESS) if (result != JSON_SUCCESS)
json_ereport_error(result, lex); {
json_errsave_error(result, lex, escontext);
return false;
}
return true;
} }
/* /*
@ -608,17 +619,25 @@ jsonb_object_keys(PG_FUNCTION_ARGS)
* Report a JSON error. * Report a JSON error.
*/ */
void void
json_ereport_error(JsonParseErrorType error, JsonLexContext *lex) json_errsave_error(JsonParseErrorType error, JsonLexContext *lex,
Node *escontext)
{ {
if (error == JSON_UNICODE_HIGH_ESCAPE || if (error == JSON_UNICODE_HIGH_ESCAPE ||
error == JSON_UNICODE_UNTRANSLATABLE ||
error == JSON_UNICODE_CODE_POINT_ZERO) error == JSON_UNICODE_CODE_POINT_ZERO)
ereport(ERROR, errsave(escontext,
(errcode(ERRCODE_UNTRANSLATABLE_CHARACTER), (errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
errmsg("unsupported Unicode escape sequence"), errmsg("unsupported Unicode escape sequence"),
errdetail_internal("%s", json_errdetail(error, lex)), errdetail_internal("%s", json_errdetail(error, lex)),
report_json_context(lex))); report_json_context(lex)));
else if (error == JSON_SEM_ACTION_FAILED)
{
/* semantic action function had better have reported something */
if (!SOFT_ERROR_OCCURRED(escontext))
elog(ERROR, "JSON semantic action function did not provide error information");
}
else else
ereport(ERROR, errsave(escontext,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s", "json"), errmsg("invalid input syntax for type %s", "json"),
errdetail_internal("%s", json_errdetail(error, lex)), errdetail_internal("%s", json_errdetail(error, lex)),
@ -1274,7 +1293,7 @@ get_array_start(void *state)
error = json_count_array_elements(_state->lex, &nelements); error = json_count_array_elements(_state->lex, &nelements);
if (error != JSON_SUCCESS) if (error != JSON_SUCCESS)
json_ereport_error(error, _state->lex); json_errsave_error(error, _state->lex, NULL);
if (-_state->path_indexes[lex_level] <= nelements) if (-_state->path_indexes[lex_level] <= nelements)
_state->path_indexes[lex_level] += nelements; _state->path_indexes[lex_level] += nelements;

View File

@ -1614,6 +1614,51 @@ InputFunctionCallSafe(FmgrInfo *flinfo, char *str,
return true; return true;
} }
/*
* Call a directly-named datatype input function, with non-exception
* handling of "soft" errors.
*
* This is like InputFunctionCallSafe, except that it is given a direct
* pointer to the C function to call. We assume that that function is
* strict. Also, the function cannot be one that needs to
* look at FmgrInfo, since there won't be any.
*/
bool
DirectInputFunctionCallSafe(PGFunction func, char *str,
Oid typioparam, int32 typmod,
fmNodePtr escontext,
Datum *result)
{
LOCAL_FCINFO(fcinfo, 3);
if (str == NULL)
{
*result = (Datum) 0; /* just return null result */
return true;
}
InitFunctionCallInfoData(*fcinfo, NULL, 3, InvalidOid, escontext, NULL);
fcinfo->args[0].value = CStringGetDatum(str);
fcinfo->args[0].isnull = false;
fcinfo->args[1].value = ObjectIdGetDatum(typioparam);
fcinfo->args[1].isnull = false;
fcinfo->args[2].value = Int32GetDatum(typmod);
fcinfo->args[2].isnull = false;
*result = (*func) (fcinfo);
/* Result value is garbage, and could be null, if an error was reported */
if (SOFT_ERROR_OCCURRED(escontext))
return false;
/* Otherwise, shouldn't get null result */
if (fcinfo->isnull)
elog(ERROR, "input function %p returned NULL", (void *) func);
return true;
}
/* /*
* Call a previously-looked-up datatype output function. * Call a previously-looked-up datatype output function.
* *

View File

@ -916,6 +916,63 @@ pg_unicode_to_server(pg_wchar c, unsigned char *s)
BoolGetDatum(false)); BoolGetDatum(false));
} }
/*
* Convert a single Unicode code point into a string in the server encoding.
*
* Same as pg_unicode_to_server(), except that we don't throw errors,
* but simply return false on conversion failure.
*/
bool
pg_unicode_to_server_noerror(pg_wchar c, unsigned char *s)
{
unsigned char c_as_utf8[MAX_MULTIBYTE_CHAR_LEN + 1];
int c_as_utf8_len;
int converted_len;
int server_encoding;
/* Fail if invalid Unicode code point */
if (!is_valid_unicode_codepoint(c))
return false;
/* Otherwise, if it's in ASCII range, conversion is trivial */
if (c <= 0x7F)
{
s[0] = (unsigned char) c;
s[1] = '\0';
return true;
}
/* If the server encoding is UTF-8, we just need to reformat the code */
server_encoding = GetDatabaseEncoding();
if (server_encoding == PG_UTF8)
{
unicode_to_utf8(c, s);
s[pg_utf_mblen(s)] = '\0';
return true;
}
/* For all other cases, we must have a conversion function available */
if (Utf8ToServerConvProc == NULL)
return false;
/* Construct UTF-8 source string */
unicode_to_utf8(c, c_as_utf8);
c_as_utf8_len = pg_utf_mblen(c_as_utf8);
c_as_utf8[c_as_utf8_len] = '\0';
/* Convert, but without throwing error if we can't */
converted_len = DatumGetInt32(FunctionCall6(Utf8ToServerConvProc,
Int32GetDatum(PG_UTF8),
Int32GetDatum(server_encoding),
CStringGetDatum((char *) c_as_utf8),
CStringGetDatum((char *) s),
Int32GetDatum(c_as_utf8_len),
BoolGetDatum(true)));
/* Conversion was successful iff it consumed the whole input */
return (converted_len == c_as_utf8_len);
}
/* convert a multibyte string to a wchar */ /* convert a multibyte string to a wchar */
int int

View File

@ -791,19 +791,16 @@ json_lex_string(JsonLexContext *lex)
/* /*
* Add the represented character to lex->strval. In the * Add the represented character to lex->strval. In the
* backend, we can let pg_unicode_to_server() handle any * backend, we can let pg_unicode_to_server_noerror()
* required character set conversion; in frontend, we can * handle any required character set conversion; in
* only deal with trivial conversions. * frontend, we can only deal with trivial conversions.
*
* Note: pg_unicode_to_server() will throw an error for a
* conversion failure, rather than returning a failure
* indication. That seems OK.
*/ */
#ifndef FRONTEND #ifndef FRONTEND
{ {
char cbuf[MAX_UNICODE_EQUIVALENT_STRING + 1]; char cbuf[MAX_UNICODE_EQUIVALENT_STRING + 1];
pg_unicode_to_server(ch, (unsigned char *) cbuf); if (!pg_unicode_to_server_noerror(ch, (unsigned char *) cbuf))
return JSON_UNICODE_UNTRANSLATABLE;
appendStringInfoString(lex->strval, cbuf); appendStringInfoString(lex->strval, cbuf);
} }
#else #else
@ -1167,6 +1164,10 @@ json_errdetail(JsonParseErrorType error, JsonLexContext *lex)
case JSON_UNICODE_HIGH_ESCAPE: case JSON_UNICODE_HIGH_ESCAPE:
/* note: this case is only reachable in frontend not backend */ /* note: this case is only reachable in frontend not backend */
return _("Unicode escape values cannot be used for code point values above 007F when the encoding is not UTF8."); return _("Unicode escape values cannot be used for code point values above 007F when the encoding is not UTF8.");
case JSON_UNICODE_UNTRANSLATABLE:
/* note: this case is only reachable in backend not frontend */
return psprintf(_("Unicode escape value could not be translated to the server's encoding %s."),
GetDatabaseEncodingName());
case JSON_UNICODE_HIGH_SURROGATE: case JSON_UNICODE_HIGH_SURROGATE:
return _("Unicode high surrogate must not follow a high surrogate."); return _("Unicode high surrogate must not follow a high surrogate.");
case JSON_UNICODE_LOW_SURROGATE: case JSON_UNICODE_LOW_SURROGATE:

View File

@ -51,6 +51,7 @@ typedef enum JsonParseErrorType
JSON_UNICODE_CODE_POINT_ZERO, JSON_UNICODE_CODE_POINT_ZERO,
JSON_UNICODE_ESCAPE_FORMAT, JSON_UNICODE_ESCAPE_FORMAT,
JSON_UNICODE_HIGH_ESCAPE, JSON_UNICODE_HIGH_ESCAPE,
JSON_UNICODE_UNTRANSLATABLE,
JSON_UNICODE_HIGH_SURROGATE, JSON_UNICODE_HIGH_SURROGATE,
JSON_UNICODE_LOW_SURROGATE, JSON_UNICODE_LOW_SURROGATE,
JSON_SEM_ACTION_FAILED /* error should already be reported */ JSON_SEM_ACTION_FAILED /* error should already be reported */

View File

@ -704,6 +704,10 @@ extern bool InputFunctionCallSafe(FmgrInfo *flinfo, char *str,
Oid typioparam, int32 typmod, Oid typioparam, int32 typmod,
fmNodePtr escontext, fmNodePtr escontext,
Datum *result); Datum *result);
extern bool DirectInputFunctionCallSafe(PGFunction func, char *str,
Oid typioparam, int32 typmod,
fmNodePtr escontext,
Datum *result);
extern Datum OidInputFunctionCall(Oid functionId, char *str, extern Datum OidInputFunctionCall(Oid functionId, char *str,
Oid typioparam, int32 typmod); Oid typioparam, int32 typmod);
extern char *OutputFunctionCall(FmgrInfo *flinfo, Datum val); extern char *OutputFunctionCall(FmgrInfo *flinfo, Datum val);

View File

@ -650,6 +650,7 @@ extern char *pg_any_to_server(const char *s, int len, int encoding);
extern char *pg_server_to_any(const char *s, int len, int encoding); extern char *pg_server_to_any(const char *s, int len, int encoding);
extern void pg_unicode_to_server(pg_wchar c, unsigned char *s); extern void pg_unicode_to_server(pg_wchar c, unsigned char *s);
extern bool pg_unicode_to_server_noerror(pg_wchar c, unsigned char *s);
extern unsigned short BIG5toCNS(unsigned short big5, unsigned char *lc); extern unsigned short BIG5toCNS(unsigned short big5, unsigned char *lc);
extern unsigned short CNStoBIG5(unsigned short cns, unsigned char lc); extern unsigned short CNStoBIG5(unsigned short cns, unsigned char lc);

View File

@ -39,11 +39,16 @@ typedef text *(*JsonTransformStringValuesAction) (void *state, char *elem_value,
/* build a JsonLexContext from a text datum */ /* build a JsonLexContext from a text datum */
extern JsonLexContext *makeJsonLexContext(text *json, bool need_escapes); extern JsonLexContext *makeJsonLexContext(text *json, bool need_escapes);
/* try to parse json, and ereport(ERROR) on failure */ /* try to parse json, and errsave(escontext) on failure */
extern void pg_parse_json_or_ereport(JsonLexContext *lex, JsonSemAction *sem); extern bool pg_parse_json_or_errsave(JsonLexContext *lex, JsonSemAction *sem,
struct Node *escontext);
/* report an error during json lexing or parsing */ #define pg_parse_json_or_ereport(lex, sem) \
extern void json_ereport_error(JsonParseErrorType error, JsonLexContext *lex); (void) pg_parse_json_or_errsave(lex, sem, NULL)
/* save an error during json lexing or parsing */
extern void json_errsave_error(JsonParseErrorType error, JsonLexContext *lex,
struct Node *escontext);
extern uint32 parse_jsonb_index_flags(Jsonb *jb); extern uint32 parse_jsonb_index_flags(Jsonb *jb);
extern void iterate_jsonb_values(Jsonb *jb, uint32 flags, void *state, extern void iterate_jsonb_values(Jsonb *jb, uint32 flags, void *state,

View File

@ -320,6 +320,25 @@ LINE 1: SELECT '{
DETAIL: Expected JSON value, but found "}". DETAIL: Expected JSON value, but found "}".
CONTEXT: JSON data, line 4: ...yveryveryveryveryveryveryveryverylongfieldname":} CONTEXT: JSON data, line 4: ...yveryveryveryveryveryveryveryverylongfieldname":}
-- ERROR missing value for last field -- ERROR missing value for last field
-- test non-error-throwing input
select pg_input_is_valid('{"a":true}', 'json');
pg_input_is_valid
-------------------
t
(1 row)
select pg_input_is_valid('{"a":true', 'json');
pg_input_is_valid
-------------------
f
(1 row)
select pg_input_error_message('{"a":true', 'json');
pg_input_error_message
------------------------------------
invalid input syntax for type json
(1 row)
--constructors --constructors
-- array_to_json -- array_to_json
SELECT array_to_json(array(select 1 as a)); SELECT array_to_json(array(select 1 as a));

View File

@ -260,3 +260,10 @@ SELECT jsonb '{ "a": "null \\u0000 escape" }' ->> 'a' as not_an_escape;
null \u0000 escape null \u0000 escape
(1 row) (1 row)
-- soft error for input-time failure
select pg_input_error_message('{ "a": "\ud83d\ude04\ud83d\udc36" }', 'jsonb');
pg_input_error_message
------------------------
(1 row)

View File

@ -48,7 +48,9 @@ SELECT '"\uaBcD"'::json; -- OK, uppercase and lower case both OK
-- handling of unicode surrogate pairs -- handling of unicode surrogate pairs
select json '{ "a": "\ud83d\ude04\ud83d\udc36" }' -> 'a' as correct_in_utf8; select json '{ "a": "\ud83d\ude04\ud83d\udc36" }' -> 'a' as correct_in_utf8;
ERROR: conversion between UTF8 and SQL_ASCII is not supported ERROR: unsupported Unicode escape sequence
DETAIL: Unicode escape value could not be translated to the server's encoding SQL_ASCII.
CONTEXT: JSON data, line 1: { "a":...
select json '{ "a": "\ud83d\ud83d" }' -> 'a'; -- 2 high surrogates in a row select json '{ "a": "\ud83d\ud83d" }' -> 'a'; -- 2 high surrogates in a row
ERROR: invalid input syntax for type json ERROR: invalid input syntax for type json
DETAIL: Unicode high surrogate must not follow a high surrogate. DETAIL: Unicode high surrogate must not follow a high surrogate.
@ -97,7 +99,9 @@ select json '{ "a": "null \\u0000 escape" }' as not_an_escape;
(1 row) (1 row)
select json '{ "a": "the Copyright \u00a9 sign" }' ->> 'a' as correct_in_utf8; select json '{ "a": "the Copyright \u00a9 sign" }' ->> 'a' as correct_in_utf8;
ERROR: conversion between UTF8 and SQL_ASCII is not supported ERROR: unsupported Unicode escape sequence
DETAIL: Unicode escape value could not be translated to the server's encoding SQL_ASCII.
CONTEXT: JSON data, line 1: { "a":...
select json '{ "a": "dollar \u0024 character" }' ->> 'a' as correct_everywhere; select json '{ "a": "dollar \u0024 character" }' ->> 'a' as correct_everywhere;
correct_everywhere correct_everywhere
-------------------- --------------------
@ -155,14 +159,18 @@ CONTEXT: JSON data, line 1: ...
-- use octet_length here so we don't get an odd unicode char in the -- use octet_length here so we don't get an odd unicode char in the
-- output -- output
SELECT octet_length('"\uaBcD"'::jsonb::text); -- OK, uppercase and lower case both OK SELECT octet_length('"\uaBcD"'::jsonb::text); -- OK, uppercase and lower case both OK
ERROR: conversion between UTF8 and SQL_ASCII is not supported ERROR: unsupported Unicode escape sequence
LINE 1: SELECT octet_length('"\uaBcD"'::jsonb::text); LINE 1: SELECT octet_length('"\uaBcD"'::jsonb::text);
^ ^
DETAIL: Unicode escape value could not be translated to the server's encoding SQL_ASCII.
CONTEXT: JSON data, line 1: ...
-- handling of unicode surrogate pairs -- handling of unicode surrogate pairs
SELECT octet_length((jsonb '{ "a": "\ud83d\ude04\ud83d\udc36" }' -> 'a')::text) AS correct_in_utf8; SELECT octet_length((jsonb '{ "a": "\ud83d\ude04\ud83d\udc36" }' -> 'a')::text) AS correct_in_utf8;
ERROR: conversion between UTF8 and SQL_ASCII is not supported ERROR: unsupported Unicode escape sequence
LINE 1: SELECT octet_length((jsonb '{ "a": "\ud83d\ude04\ud83d\udc3... LINE 1: SELECT octet_length((jsonb '{ "a": "\ud83d\ude04\ud83d\udc3...
^ ^
DETAIL: Unicode escape value could not be translated to the server's encoding SQL_ASCII.
CONTEXT: JSON data, line 1: { "a":...
SELECT jsonb '{ "a": "\ud83d\ud83d" }' -> 'a'; -- 2 high surrogates in a row SELECT jsonb '{ "a": "\ud83d\ud83d" }' -> 'a'; -- 2 high surrogates in a row
ERROR: invalid input syntax for type json ERROR: invalid input syntax for type json
LINE 1: SELECT jsonb '{ "a": "\ud83d\ud83d" }' -> 'a'; LINE 1: SELECT jsonb '{ "a": "\ud83d\ud83d" }' -> 'a';
@ -189,9 +197,11 @@ DETAIL: Unicode low surrogate must follow a high surrogate.
CONTEXT: JSON data, line 1: { "a":... CONTEXT: JSON data, line 1: { "a":...
-- handling of simple unicode escapes -- handling of simple unicode escapes
SELECT jsonb '{ "a": "the Copyright \u00a9 sign" }' as correct_in_utf8; SELECT jsonb '{ "a": "the Copyright \u00a9 sign" }' as correct_in_utf8;
ERROR: conversion between UTF8 and SQL_ASCII is not supported ERROR: unsupported Unicode escape sequence
LINE 1: SELECT jsonb '{ "a": "the Copyright \u00a9 sign" }' as corr... LINE 1: SELECT jsonb '{ "a": "the Copyright \u00a9 sign" }' as corr...
^ ^
DETAIL: Unicode escape value could not be translated to the server's encoding SQL_ASCII.
CONTEXT: JSON data, line 1: { "a":...
SELECT jsonb '{ "a": "dollar \u0024 character" }' as correct_everywhere; SELECT jsonb '{ "a": "dollar \u0024 character" }' as correct_everywhere;
correct_everywhere correct_everywhere
----------------------------- -----------------------------
@ -217,9 +227,11 @@ SELECT jsonb '{ "a": "null \\u0000 escape" }' as not_an_escape;
(1 row) (1 row)
SELECT jsonb '{ "a": "the Copyright \u00a9 sign" }' ->> 'a' as correct_in_utf8; SELECT jsonb '{ "a": "the Copyright \u00a9 sign" }' ->> 'a' as correct_in_utf8;
ERROR: conversion between UTF8 and SQL_ASCII is not supported ERROR: unsupported Unicode escape sequence
LINE 1: SELECT jsonb '{ "a": "the Copyright \u00a9 sign" }' ->> 'a'... LINE 1: SELECT jsonb '{ "a": "the Copyright \u00a9 sign" }' ->> 'a'...
^ ^
DETAIL: Unicode escape value could not be translated to the server's encoding SQL_ASCII.
CONTEXT: JSON data, line 1: { "a":...
SELECT jsonb '{ "a": "dollar \u0024 character" }' ->> 'a' as correct_everywhere; SELECT jsonb '{ "a": "dollar \u0024 character" }' ->> 'a' as correct_everywhere;
correct_everywhere correct_everywhere
-------------------- --------------------
@ -244,3 +256,10 @@ SELECT jsonb '{ "a": "null \\u0000 escape" }' ->> 'a' as not_an_escape;
null \u0000 escape null \u0000 escape
(1 row) (1 row)
-- soft error for input-time failure
select pg_input_error_message('{ "a": "\ud83d\ude04\ud83d\udc36" }', 'jsonb');
pg_input_error_message
-------------------------------------
unsupported Unicode escape sequence
(1 row)

View File

@ -310,6 +310,31 @@ LINE 1: SELECT '{
DETAIL: Expected JSON value, but found "}". DETAIL: Expected JSON value, but found "}".
CONTEXT: JSON data, line 4: ...yveryveryveryveryveryveryveryverylongfieldname":} CONTEXT: JSON data, line 4: ...yveryveryveryveryveryveryveryverylongfieldname":}
-- ERROR missing value for last field -- ERROR missing value for last field
-- test non-error-throwing input
select pg_input_is_valid('{"a":true}', 'jsonb');
pg_input_is_valid
-------------------
t
(1 row)
select pg_input_is_valid('{"a":true', 'jsonb');
pg_input_is_valid
-------------------
f
(1 row)
select pg_input_error_message('{"a":true', 'jsonb');
pg_input_error_message
------------------------------------
invalid input syntax for type json
(1 row)
select pg_input_error_message('{"a":1e1000000}', 'jsonb');
pg_input_error_message
--------------------------------
value overflows numeric format
(1 row)
-- make sure jsonb is passed through json generators without being escaped -- make sure jsonb is passed through json generators without being escaped
SELECT array_to_json(ARRAY [jsonb '{"a":1}', jsonb '{"b":[2,3]}']); SELECT array_to_json(ARRAY [jsonb '{"a":1}', jsonb '{"b":[2,3]}']);
array_to_json array_to_json

View File

@ -81,6 +81,11 @@ SELECT '{
"averyveryveryveryveryveryveryveryveryverylongfieldname":}'::json; "averyveryveryveryveryveryveryveryveryverylongfieldname":}'::json;
-- ERROR missing value for last field -- ERROR missing value for last field
-- test non-error-throwing input
select pg_input_is_valid('{"a":true}', 'json');
select pg_input_is_valid('{"a":true', 'json');
select pg_input_error_message('{"a":true', 'json');
--constructors --constructors
-- array_to_json -- array_to_json

View File

@ -76,3 +76,7 @@ SELECT jsonb '{ "a": "dollar \u0024 character" }' ->> 'a' as correct_everywhere
SELECT jsonb '{ "a": "dollar \\u0024 character" }' ->> 'a' as not_an_escape; SELECT jsonb '{ "a": "dollar \\u0024 character" }' ->> 'a' as not_an_escape;
SELECT jsonb '{ "a": "null \u0000 escape" }' ->> 'a' as fails; SELECT jsonb '{ "a": "null \u0000 escape" }' ->> 'a' as fails;
SELECT jsonb '{ "a": "null \\u0000 escape" }' ->> 'a' as not_an_escape; SELECT jsonb '{ "a": "null \\u0000 escape" }' ->> 'a' as not_an_escape;
-- soft error for input-time failure
select pg_input_error_message('{ "a": "\ud83d\ude04\ud83d\udc36" }', 'jsonb');

View File

@ -86,6 +86,12 @@ SELECT '{
"averyveryveryveryveryveryveryveryveryverylongfieldname":}'::jsonb; "averyveryveryveryveryveryveryveryveryverylongfieldname":}'::jsonb;
-- ERROR missing value for last field -- ERROR missing value for last field
-- test non-error-throwing input
select pg_input_is_valid('{"a":true}', 'jsonb');
select pg_input_is_valid('{"a":true', 'jsonb');
select pg_input_error_message('{"a":true', 'jsonb');
select pg_input_error_message('{"a":1e1000000}', 'jsonb');
-- make sure jsonb is passed through json generators without being escaped -- make sure jsonb is passed through json generators without being escaped
SELECT array_to_json(ARRAY [jsonb '{"a":1}', jsonb '{"b":[2,3]}']); SELECT array_to_json(ARRAY [jsonb '{"a":1}', jsonb '{"b":[2,3]}']);