Skip to content

Commit

Permalink
fix(postgres) : int type conversion while decoding (launchbadge#3173)
Browse files Browse the repository at this point in the history
* fix(postgres) : int type conversion while decoding

* Check value buffer len when decoding integer type in postgres

* decode from octal for postgres i8

---------

Co-authored-by: RaghavRox <66472843+RaghavRox@users.noreply.github.com>
  • Loading branch information
2 people authored and jayy-lmao committed Jun 6, 2024
1 parent e740c54 commit cd4042c
Showing 1 changed file with 48 additions and 14 deletions.
62 changes: 48 additions & 14 deletions sqlx-postgres/src/types/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i64, BoxDynError> {
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<Postgres> for i8 {
fn type_info() -> PgTypeInfo {
PgTypeInfo::CHAR
Expand All @@ -28,8 +53,26 @@ impl Encode<'_, Postgres> for i8 {

impl Decode<'_, Postgres> for i8 {
fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
// 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)
}
}
}
}

Expand All @@ -55,10 +98,7 @@ impl Encode<'_, Postgres> for i16 {

impl Decode<'_, Postgres> for i16 {
fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
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)
}
}

Expand All @@ -84,10 +124,7 @@ impl Encode<'_, Postgres> for i32 {

impl Decode<'_, Postgres> for i32 {
fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
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)
}
}

Expand All @@ -113,9 +150,6 @@ impl Encode<'_, Postgres> for i64 {

impl Decode<'_, Postgres> for i64 {
fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
Ok(match value.format() {
PgValueFormat::Binary => BigEndian::read_i64(value.as_bytes()?),
PgValueFormat::Text => value.as_str()?.parse()?,
})
int_decode(value)
}
}

0 comments on commit cd4042c

Please sign in to comment.