Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3dd8e59

Browse files
committed
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.
1 parent d9b31e4 commit 3dd8e59

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

src/backend/utils/adt/json.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ json_lex_string(JsonLexContext *lex)
419419
for (s = lex->token_start + 1; *s != '"'; ++s)
420420
{
421421
/* Per RFC4627, these characters MUST be escaped. */
422-
if (*s < 32)
422+
if ((unsigned char) *s < 32)
423423
{
424424
/* A NUL byte marks the (premature) end of the string. */
425425
if (*s == '\0')
@@ -430,8 +430,8 @@ json_lex_string(JsonLexContext *lex)
430430
ereport(ERROR,
431431
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
432432
errmsg("invalid input syntax for type json"),
433-
errdetail_internal("line %d: Character \"%c\" must be escaped.",
434-
lex->line_number, *s)));
433+
errdetail_internal("line %d: Character with value \"0x%02x\" must be escaped.",
434+
lex->line_number, (unsigned char) *s)));
435435
}
436436
else if (*s == '\\')
437437
{
@@ -637,7 +637,7 @@ report_parse_error(JsonParseStack *stack, JsonLexContext *lex)
637637
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
638638
errmsg("invalid input syntax for type json: \"%s\"",
639639
lex->input),
640-
errdetail_internal(detail, lex->line_number, token)));
640+
detail ? errdetail_internal(detail, lex->line_number, token) : 0));
641641
}
642642

643643
/*

src/test/regress/expected/json.out

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ def"'::json; -- ERROR, unescaped newline in string constant
2626
ERROR: invalid input syntax for type json
2727
LINE 1: SELECT '"abc
2828
^
29-
DETAIL: line 1: Character "
30-
" must be escaped.
29+
DETAIL: line 1: Character with value "0x0a" must be escaped.
3130
SELECT '"\n\"\\"'::json; -- OK, legal escapes
3231
json
3332
----------

0 commit comments

Comments
 (0)