Make escaping functions retain trailing bytes of an invalid character.
authorTom Lane <[email protected]>
Sat, 15 Feb 2025 21:20:21 +0000 (16:20 -0500)
committerTom Lane <[email protected]>
Sat, 15 Feb 2025 21:20:21 +0000 (16:20 -0500)
commitc08309584ac58a28072fa2c4765fb4c4c01a901d
tree7dc8f57153401df25955316cc8caa191f6bff593
parent985908df18116b5d2a496b1581c337aab23ea64e
Make escaping functions retain trailing bytes of an invalid character.

Instead of dropping the trailing byte(s) of an invalid or incomplete
multibyte character, replace only the first byte with a known-invalid
sequence, and process the rest normally.  This seems less likely to
confuse incautious callers than the behavior adopted in 5dc1e42b4.

While we're at it, adjust PQescapeStringInternal to produce at most
one bleat about invalid multibyte characters per string.  This
matches the behavior of PQescapeInternal, and avoids the risk of
producing tons of repetitive junk if a long string is simply given
in the wrong encoding.

This is a followup to the fixes for CVE-2025-1094, and should be
included if cherry-picking those fixes.

Author: Andres Freund <[email protected]>
Co-authored-by: Tom Lane <[email protected]>
Reported-by: Jeff Davis <[email protected]>
Discussion: https://postgr.es/m/20250215012712[email protected]
Backpatch-through: 13
src/fe_utils/string_utils.c
src/interfaces/libpq/fe-exec.c