control character check in JSON type seems broken - Mailing list pgsql-bugs

From Shigeru Hanada
Subject control character check in JSON type seems broken
Date
Msg-id CAEZqfEdbRO8MhH83Fma9uSQhCfWmHpgcbm9Lwucq2HSwCHhbdQ@mail.gmail.com
Whole thread Raw
Responses Re: control character check in JSON type seems broken
List pgsql-bugs
Hi,

A Japanese user found strange behavior in JSON type, so I'd like to
share the issue here.  He simply tested casting a string literal to json
type, and got an unexpected error when he used a Japanese word as name
and/or value of JSON object.

In the example below, "キー" is a Japanese word which means "key", and
its first letter has byte sequence "0xe3 0x82 0xad" in UTF-8.

postgres=# select '{"キー":"value"}'::json;
ERROR:  invalid input syntax for type json
LINE 1: select '{"キー":"value"}'::json;
                ^
DETAIL:  line 1: Character "  ・ must be escaped.

With some debugging, I found that the problem is in json_lex_string().

json_lex_string() misjudges that the token "キー" contains naked
(not-escaped) control character, because its first byte is 0xe3 and it's
-29 for signed char interpreting, and it's less than 32.  We need to
cast to unsigned char (or use unsigned variable) here.

In addition, error message above seems corrupted in my environment.
Here we check not-escaped control character, so printing it with %c
formatting might break log files.  How about using decimal or hex dump
in such cases?

Please see attached patch which contains workaround for this issue.

Regards,
--
Shigeru HANADA

Attachment

pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: BUG #6672: Memory leaks in dumputils.c
Next
From: jo
Date:
Subject: Re: BUG #6669: unique index w/ multiple columns and NULLs