postgresql/src/common
Michael Paquier 5396a2987c Fix overread in JSON parsing errors for incomplete byte sequences
json_lex_string() relies on pg_encoding_mblen_bounded() to point to the
end of a JSON string when generating an error message, and the input it
uses is not guaranteed to be null-terminated.

It was possible to walk off the end of the input buffer by a few bytes
when the last bytes consist of an incomplete multi-byte sequence, as
token_terminator would point to a location defined by
pg_encoding_mblen_bounded() rather than the end of the input.  This
commit switches token_terminator so as the error uses data up to the
end of the JSON input.

More work should be done so as this code could rely on an equivalent of
report_invalid_encoding() so as incorrect byte sequences can show in
error messages in a readable form.  This requires work for at least two
cases in the JSON parsing API: an incomplete token and an invalid escape
sequence.  A more complete solution may be too invasive for a backpatch,
so this is left as a future improvement, taking care of the overread
first.

A test is added on HEAD as test_json_parser makes this issue
straight-forward to check.

Note that pg_encoding_mblen_bounded() no longer has any callers.  This
will be removed on HEAD with a separate commit, as this is proving to
encourage unsafe coding.

Author: Jacob Champion
Discussion: https://postgr.es/m/CAOYmi+ncM7pwLS3AnKCSmoqqtpjvA8wmCdoBtKA3ZrB2hZG6zA@mail.gmail.com
Backpatch-through: 13
2024-05-09 12:45:43 +09:00
..
unicode meson: Add dependencies to perl modules to various script invocations 2023-06-09 20:12:16 -07:00
.gitignore Replace the data structure used for keyword lookup. 2019-01-06 17:02:57 -05:00
Makefile Revert refactoring of restore command code to shell_restore.c 2023-02-06 08:28:42 +09:00
archive.c Revert refactoring of restore command code to shell_restore.c 2023-02-06 08:28:42 +09:00
base64.c Fix small overestimation of base64 encoding output length. 2023-06-08 11:24:31 -04:00
checksum_helper.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
compression.c Message style improvements 2023-05-19 18:45:29 +02:00
config_info.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
controldata_utils.c Try to handle torn reads of pg_control in frontend. 2023-10-16 17:21:41 +13:00
cryptohash.c Improve/correct comments 2023-03-09 09:59:46 +01:00
cryptohash_openssl.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
d2s.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
d2s_full_table.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
d2s_intrinsics.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
digit_table.h Change floating-point output format for improved performance. 2019-02-13 15:20:33 +00:00
encnames.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
exec.c Implement find_my_exec()'s path normalization using realpath(3). 2023-03-23 18:17:49 -04:00
f2s.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
fe_memutils.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
file_perm.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
file_utils.c Introduce PG_IO_ALIGN_SIZE and align all I/O buffers. 2023-04-08 16:34:50 +12:00
hashfn.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
hmac.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
hmac_openssl.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
ip.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
jsonapi.c Fix overread in JSON parsing errors for incomplete byte sequences 2024-05-09 12:45:43 +09:00
keywords.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
kwlookup.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
link-canary.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
logging.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
md5.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
md5_common.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
md5_int.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
meson.build meson: Add dependencies to perl modules to various script invocations 2023-06-09 20:12:16 -07:00
percentrepl.c Fix error message wordings 2023-05-17 21:33:47 +02:00
pg_get_line.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
pg_lzcompress.c Improve pglz_decompress's defenses against corrupt compressed data. 2023-10-18 20:43:17 -04:00
pg_prng.c Invent random_normal() to provide normally-distributed random numbers. 2023-01-09 12:44:00 -05:00
pgfnames.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
protocol_openssl.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
psprintf.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
relpath.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
restricted_token.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
rmtree.c Message style improvements 2023-05-19 18:45:29 +02:00
ryu_common.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
saslprep.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
scram-common.c Add CHECK_FOR_INTERRUPTS() in scram_SaltedPassword() for the backend 2023-11-28 08:35:56 +09:00
sha1.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
sha1_int.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
sha2.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
sha2_int.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
sprompt.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
string.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
stringinfo.c Use appendStringInfoSpaces in more places 2023-01-20 13:07:24 +13:00
unicode_norm.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
username.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
wait_error.c Add SHELL_ERROR and SHELL_EXIT_CODE magic variables to psql. 2023-03-21 13:03:56 -04:00
wchar.c Move is_valid_ascii() to ascii.h. 2024-01-29 12:09:03 -06:00