From a7d28605bf305dea40e98343276556202573b77d Mon Sep 17 00:00:00 2001 From: YgorSouza <43298013+YgorSouza@users.noreply.github.com> Date: Tue, 16 Jul 2024 09:27:20 +0200 Subject: [PATCH] fix(postgres): don't panic if `M` or `C` Notice fields are not UTF-8 (#3346) * fix(postgres): don't panic if `M` or `C` Notice fields are not UTF-8 This has been observed with an old version of PostgreSQL (11.0.4) running on Windows Server 2016 with windows-1252 encoding and French locale. This change replaces invalid UTF-8 fields with a default string, so the other fields can still be read if they are valid. * Revert "fix(postgres): don't panic if `M` or `C` Notice fields are not UTF-8" This reverts commit 362ca98bbdfef14f7f85f53e6d12fdcd8120ea22. * Check that Notice M and C fields are valid UTF-8 Otherwise, we return the invalid UTF-8 error to avoid panicking later. --- sqlx-postgres/src/message/response.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sqlx-postgres/src/message/response.rs b/sqlx-postgres/src/message/response.rs index bb79ddd46d..ec3c880886 100644 --- a/sqlx-postgres/src/message/response.rs +++ b/sqlx-postgres/src/message/response.rs @@ -153,13 +153,19 @@ impl Decode<'_> for Notice { } b'M' => { + _ = from_utf8(&buf[v.0 as usize..v.1 as usize]) + .map_err(|_| notice_protocol_err())?; message = v; } b'C' => { + _ = from_utf8(&buf[v.0 as usize..v.1 as usize]) + .map_err(|_| notice_protocol_err())?; code = v; } + // If more fields are added, make sure to check that they are valid UTF-8, + // otherwise the get_cached_str method will panic. _ => {} } }