Skip to content

Commit 47937c4

Browse files
committed
more overflows
1 parent 6b17f07 commit 47937c4

File tree

11 files changed

+27
-23
lines changed

11 files changed

+27
-23
lines changed

sqlx-core/src/postgres/message/bind.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ impl Encode<'_> for Bind<'_> {
5252

5353
buf.extend(self.params);
5454

55-
buf.extend(&(self.result_formats.len() as i16).to_be_bytes());
56-
57-
for &format in self.result_formats {
58-
buf.extend(&(format as i16).to_be_bytes());
55+
if let Ok(len) = i16::try_from(self.result_formats.len()) {
56+
buf.extend(&len.to_be_bytes());
57+
for &format in self.result_formats {
58+
buf.extend(&(format as i16).to_be_bytes());
59+
}
5960
}
6061
});
6162
}

sqlx-core/src/postgres/message/copy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl Decode<'_> for CopyData<Bytes> {
4343
impl<B: Deref<Target = [u8]>> Encode<'_> for CopyData<B> {
4444
fn encode_with(&self, buf: &mut Vec<u8>, _context: ()) {
4545
buf.push(b'd');
46-
buf.put_u32(self.0.len() as u32 + 4);
46+
buf.put_u32(u32::try_from(self.0.len() + 4).expect("copydata too large"));
4747
buf.extend_from_slice(&self.0);
4848
}
4949
}
@@ -61,7 +61,7 @@ impl Encode<'_> for CopyFail {
6161
let len = 4 + self.message.len() + 1;
6262

6363
buf.push(b'f'); // to pay respects
64-
buf.put_u32(len as u32);
64+
buf.put_u32(u32::try_from(len).expect("copyfail too large"));
6565
buf.put_str_nul(&self.message);
6666
}
6767
}

sqlx-core/src/postgres/message/data_row.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ impl Decode<'_> for DataRow {
4040
let length = BigEndian::read_i32(&buf[(offset as usize)..]);
4141
offset += 4;
4242

43-
if length < 0 {
44-
values.push(None);
45-
} else {
46-
values.push(Some(offset..(offset + length as u32)));
43+
if let Ok(length) = u32::try_from(length) {
44+
values.push(Some(offset..(offset + length)));
4745
offset += length as u32;
46+
} else {
47+
values.push(None);
4848
}
4949
}
5050

sqlx-core/src/postgres/message/parse.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ impl Encode<'_> for Parse<'_> {
2828
buf.put_str_nul(self.query);
2929

3030
// TODO: Return an error here instead
31-
assert!(self.param_types.len() <= (u16::MAX as usize));
31+
let param_len = i16::try_from(self.param_types.len()).expect("too many params");
3232

33-
buf.extend(&(self.param_types.len() as i16).to_be_bytes());
33+
buf.extend_from_slice(&param_len.to_be_bytes());
3434

3535
for &oid in self.param_types {
36-
buf.extend(&oid.0.to_be_bytes());
36+
buf.extend_from_slice(&oid.0.to_be_bytes());
3737
}
3838
})
3939
}

sqlx-core/src/postgres/message/query.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ pub struct Query<'a>(pub &'a str);
66
impl Encode<'_> for Query<'_> {
77
fn encode_with(&self, buf: &mut Vec<u8>, _: ()) {
88
let len = 4 + self.0.len() + 1;
9-
9+
let len_i32 = i32::try_from(len).expect("buffer too large");
1010
buf.reserve(len + 1);
1111
buf.push(b'Q');
12-
buf.extend(&(len as i32).to_be_bytes());
12+
buf.extend_from_slice(&len_i32.to_be_bytes());
1313
buf.put_str_nul(self.0);
1414
}
1515
}

sqlx-core/src/postgres/message/response.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ impl<'a> Iterator for Fields<'a> {
192192
return None;
193193
}
194194

195-
let nul = memchr(b'\0', &self.storage[(self.offset + 1) as usize..])? as u16;
195+
let nul =
196+
u16::try_from(memchr(b'\0', &self.storage[(self.offset + 1) as usize..])?).ok()?;
196197
let offset = self.offset;
197198

198199
self.offset += nul + 2;

sqlx-core/src/postgres/message/sasl.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ impl Encode<'_> for SaslInitialResponse<'_> {
1616
} else {
1717
"SCRAM-SHA-256"
1818
});
19-
20-
buf.extend(&(self.response.as_bytes().len() as i32).to_be_bytes());
21-
buf.extend(self.response.as_bytes());
19+
let bytes = self.response.as_bytes();
20+
let len_i32 = i32::try_from(bytes.len()).expect("buffer too large");
21+
buf.extend_from_slice(&len_i32.to_be_bytes());
22+
buf.extend_from_slice(bytes);
2223
});
2324
}
2425
}

sqlx-core/src/postgres/options/pgpass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ fn find_next_field<'a>(line: &mut &'a str) -> Option<Cow<'a, str>> {
178178
}
179179
}
180180

181-
return None;
181+
None
182182
}
183183

184184
#[cfg(test)]

sqlx-core/src/postgres/types/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ where
189189
return Err(format!("encountered an array with a lower bound of {} in the first dimension; only arrays starting at one are supported", lower).into());
190190
}
191191

192-
let mut elements = Vec::with_capacity(len as usize);
192+
let mut elements = Vec::with_capacity(usize::try_from(len).unwrap_or_default());
193193

194194
for _ in 0..len {
195195
elements.push(T::decode(PgValueRef::get(

sqlx-core/src/postgres/types/bigdecimal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl TryFrom<PgNumeric> for BigDecimal {
5252
};
5353

5454
// weight is 0 if the decimal point falls after the first base-10000 digit
55-
let scale = (digits.len() as i64 - weight as i64 - 1) * 4;
55+
let scale = (i64::try_from(digits.len())? - i64::from(weight) - 1) * 4;
5656

5757
// no optimized algorithm for base-10 so use base-100 for faster processing
5858
let mut cents = Vec::with_capacity(digits.len() * 2);

0 commit comments

Comments
 (0)