Fix bogus handling of control characters in json_lex_string().

The original coding misbehaved if "char" is signed, and also made the
extremely poor decision to print control characters literally when trying
to complain about them.  Report and patch by Shigeru Hanada.

In passing, also fix core dump risk in report_parse_error() should the
parse state be something other than what it expects.
This commit is contained in:
Tom Lane 2012-06-04 20:43:57 -04:00
parent d9b31e4859
commit 3dd8e59681
2 changed files with 5 additions and 6 deletions

View File

@ -419,7 +419,7 @@ json_lex_string(JsonLexContext *lex)
for (s = lex->token_start + 1; *s != '"'; ++s)
{
/* Per RFC4627, these characters MUST be escaped. */
if (*s < 32)
if ((unsigned char) *s < 32)
{
/* A NUL byte marks the (premature) end of the string. */
if (*s == '\0')
@ -430,8 +430,8 @@ json_lex_string(JsonLexContext *lex)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type json"),
errdetail_internal("line %d: Character \"%c\" must be escaped.",
lex->line_number, *s)));
errdetail_internal("line %d: Character with value \"0x%02x\" must be escaped.",
lex->line_number, (unsigned char) *s)));
}
else if (*s == '\\')
{
@ -637,7 +637,7 @@ report_parse_error(JsonParseStack *stack, JsonLexContext *lex)
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type json: \"%s\"",
lex->input),
errdetail_internal(detail, lex->line_number, token)));
detail ? errdetail_internal(detail, lex->line_number, token) : 0));
}
/*

View File

@ -26,8 +26,7 @@ def"'::json; -- ERROR, unescaped newline in string constant
ERROR: invalid input syntax for type json
LINE 1: SELECT '"abc
^
DETAIL: line 1: Character "
" must be escaped.
DETAIL: line 1: Character with value "0x0a" must be escaped.
SELECT '"\n\"\\"'::json; -- OK, legal escapes
json
----------