postgresql/src/common
Michael Paquier 855517307d 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:37 +09:00
..
unicode Update src/common/unicode/.gitignore 2024-04-22 09:16:33 +02:00
.gitignore Replace the data structure used for keyword lookup. 2019-01-06 17:02:57 -05:00
archive.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
base64.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
binaryheap.c Revert indexed and enlargable binary heap implementation. 2024-04-11 17:18:05 +09:00
blkreftable.c Fix incorrect calculation in BlockRefTableEntryGetBlocks. 2024-04-05 13:41:19 -04:00
checksum_helper.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
compression.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
config_info.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
controldata_utils.c Expose new function get_controlfile_by_exact_path(). 2024-03-13 12:06:44 -04:00
cryptohash.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
cryptohash_openssl.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
d2s.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
d2s_full_table.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
d2s_intrinsics.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
digit_table.h Change floating-point output format for improved performance. 2019-02-13 15:20:33 +00:00
encnames.c Simplify pg_enc2gettext_tbl[] with C99-designated initializer syntax 2024-03-01 18:03:48 +09:00
exec.c Fix errorhandling for reading from a pipe 2024-03-08 22:53:06 +01:00
f2s.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
fe_memutils.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
file_perm.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
file_utils.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
hashfn.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
hmac.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
hmac_openssl.c Unwind #if spaghetti in hmac_openssl.c a bit. 2024-04-02 10:41:44 -04:00
ip.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
jsonapi.c Fix overread in JSON parsing errors for incomplete byte sequences 2024-05-09 12:45:37 +09:00
keywords.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
kwlookup.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
link-canary.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
logging.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
Makefile Unicode case mapping tables and functions. 2024-03-07 11:15:06 -08:00
md5.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
md5_common.c Fix documentation comment for pg_md5_hash 2024-03-14 09:23:37 +01:00
md5_int.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
meson.build Unicode case mapping tables and functions. 2024-03-07 11:15:06 -08:00
parse_manifest.c Assorted minor cleanups in the test_json_parser module 2024-04-12 10:32:30 -04:00
percentrepl.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
pg_get_line.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
pg_lzcompress.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
pg_prng.c Add functions to generate random numbers in a specified range. 2024-03-27 10:12:39 +00:00
pgfnames.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
protocol_openssl.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
psprintf.c Use printf's %m format instead of strerror(errno) in more places 2024-03-12 10:02:54 +09:00
relpath.c Replace BackendIds with 0-based ProcNumbers 2024-03-03 19:38:22 +02:00
restricted_token.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
rmtree.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
ryu_common.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
saslprep.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
scram-common.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
sha1.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
sha1_int.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
sha2.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
sha2_int.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
sprompt.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
string.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
stringinfo.c Add destroyStringInfo function for cleaning up StringInfos 2024-03-16 23:18:28 +01:00
unicode_case.c Add unicode_strtitle() for Unicode Default Case Conversion. 2024-03-29 17:35:07 -07:00
unicode_category.c Fix typos and duplicate words 2024-04-18 21:28:07 +02:00
unicode_norm.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
username.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
wait_error.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
wchar.c Inline basic UTF-8 functions. 2024-03-20 09:40:57 -07:00