diff --git a/sqlx-postgres/src/types/int.rs b/sqlx-postgres/src/types/int.rs index 0e2c73b0d..6c9adb1a7 100644 --- a/sqlx-postgres/src/types/int.rs +++ b/sqlx-postgres/src/types/int.rs @@ -6,6 +6,31 @@ use crate::error::BoxDynError; use crate::types::Type; use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef, Postgres}; +fn int_decode(value: PgValueRef<'_>) -> Result { + Ok(match value.format() { + PgValueFormat::Text => value.as_str()?.parse()?, + PgValueFormat::Binary => { + let buf = value.as_bytes()?; + + // Return error if buf is empty or is more than 8 bytes + match buf.len() { + 0 => { + return Err("Value Buffer found empty while decoding to integer type".into()); + } + buf_len @ 9.. => { + return Err(format!( + "Value Buffer exceeds 8 bytes while decoding to integer type. Buffer size = {} bytes ", buf_len + ) + .into()); + } + _ => {} + } + + BigEndian::read_int(buf, buf.len()) + } + }) +} + impl Type for i8 { fn type_info() -> PgTypeInfo { PgTypeInfo::CHAR @@ -28,8 +53,26 @@ impl Encode<'_, Postgres> for i8 { impl Decode<'_, Postgres> for i8 { fn decode(value: PgValueRef<'_>) -> Result { - // note: in the TEXT encoding, a value of "0" here is encoded as an empty string - Ok(value.as_bytes()?.get(0).copied().unwrap_or_default() as i8) + // note: decoding here is for the `"char"` type as Postgres does not have a native 1-byte integer type. + // https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/char.c#L58-L60 + match value.format() { + PgValueFormat::Binary => int_decode(value)?.try_into().map_err(Into::into), + PgValueFormat::Text => { + let text = value.as_str()?; + + // A value of 0 is represented with the empty string. + if text.is_empty() { + return Ok(0); + } + + if text.starts_with('\\') { + // For values between 0x80 and 0xFF, it's encoded in octal. + return Ok(i8::from_str_radix(text.trim_start_matches('\\'), 8)?); + } + + Ok(text.as_bytes()[0] as i8) + } + } } } @@ -55,10 +98,7 @@ impl Encode<'_, Postgres> for i16 { impl Decode<'_, Postgres> for i16 { fn decode(value: PgValueRef<'_>) -> Result { - Ok(match value.format() { - PgValueFormat::Binary => BigEndian::read_i16(value.as_bytes()?), - PgValueFormat::Text => value.as_str()?.parse()?, - }) + int_decode(value)?.try_into().map_err(Into::into) } } @@ -84,10 +124,7 @@ impl Encode<'_, Postgres> for i32 { impl Decode<'_, Postgres> for i32 { fn decode(value: PgValueRef<'_>) -> Result { - Ok(match value.format() { - PgValueFormat::Binary => BigEndian::read_i32(value.as_bytes()?), - PgValueFormat::Text => value.as_str()?.parse()?, - }) + int_decode(value)?.try_into().map_err(Into::into) } } @@ -113,9 +150,6 @@ impl Encode<'_, Postgres> for i64 { impl Decode<'_, Postgres> for i64 { fn decode(value: PgValueRef<'_>) -> Result { - Ok(match value.format() { - PgValueFormat::Binary => BigEndian::read_i64(value.as_bytes()?), - PgValueFormat::Text => value.as_str()?.parse()?, - }) + int_decode(value) } }