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)
commitd6d29b2133f1c2a7d4f332bf68b2f40c8de3044c
tree2c51364806ceaf7388dbb67087b68c44d7581ac0
parent1f7a05324524f015a4a3a4a3fb921b11f8126051
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