Skip to content

Commit

Permalink
fix(postgres): don't panic if M or C Notice fields are not UTF-8
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
YgorSouza committed Jul 13, 2024
1 parent 0db12a9 commit 362ca98
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions sqlx-postgres/src/message/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ use sqlx_core::bytes::Bytes;
use crate::error::Error;
use crate::io::Decode;

const INVALID_UTF8: &str = "Postgres returned a non-UTF-8 string for its error message. \
This is most likely due to an error that occurred during authentication and \
the default lc_messages locale is not binary-compatible with UTF-8. \
See the server logs for the error details.";

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[repr(u8)]
pub enum PgSeverity {
Expand Down Expand Up @@ -100,8 +105,7 @@ impl Notice {

#[inline]
fn get_cached_str(&self, cache: (u16, u16)) -> &str {
// unwrap: this cannot fail at this stage
from_utf8(&self.storage[cache.0 as usize..cache.1 as usize]).unwrap()
from_utf8(&self.storage[cache.0 as usize..cache.1 as usize]).unwrap_or(INVALID_UTF8)
}
}

Expand Down Expand Up @@ -203,13 +207,7 @@ impl<'a> Iterator for Fields<'a> {

fn notice_protocol_err() -> Error {
// https://github.com/launchbadge/sqlx/issues/1144
Error::Protocol(
"Postgres returned a non-UTF-8 string for its error message. \
This is most likely due to an error that occurred during authentication and \
the default lc_messages locale is not binary-compatible with UTF-8. \
See the server logs for the error details."
.into(),
)
Error::Protocol(INVALID_UTF8.into())
}

#[test]
Expand Down

0 comments on commit 362ca98

Please sign in to comment.