Skip to content

Commit 31fc4ed

Browse files
committed
fix: audit sqlx_postgres::types::bit_vec for casts involving sign loss
1 parent dd92def commit 31fc4ed

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

sqlx-postgres/src/types/bit_vec.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,10 @@ impl Decode<'_, Postgres> for BitVec {
5252
let mut bytes = value.as_bytes()?;
5353
let len = bytes.get_i32();
5454

55-
if len < 0 {
56-
Err(io::Error::new(
57-
io::ErrorKind::InvalidData,
58-
"Negative VARBIT length.",
59-
))?
60-
}
55+
let len = usize::try_from(len).map_err(|_| format!("invalid VARBIT len: {len}"))?;
6156

6257
// The smallest amount of data we can read is one byte
63-
let bytes_len = (len as usize + 7) / 8;
58+
let bytes_len = (len + 7) / 8;
6459

6560
if bytes.remaining() != bytes_len {
6661
Err(io::Error::new(
@@ -74,7 +69,7 @@ impl Decode<'_, Postgres> for BitVec {
7469
// Chop off zeroes from the back. We get bits in bytes, so if
7570
// our bitvec is not in full bytes, extra zeroes are added to
7671
// the end.
77-
while bitvec.len() > len as usize {
72+
while bitvec.len() > len {
7873
bitvec.pop();
7974
}
8075

0 commit comments

Comments
 (0)