Skip to content

Commit 6b17f07

Browse files
committed
more overflow errors
1 parent eb7f6a4 commit 6b17f07

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ impl TryFrom<PgNumeric> for BigDecimal {
5757
// no optimized algorithm for base-10 so use base-100 for faster processing
5858
let mut cents = Vec::with_capacity(digits.len() * 2);
5959
for digit in &digits {
60-
cents.push((digit / 100) as u8);
61-
cents.push((digit % 100) as u8);
60+
cents.push(u8::try_from(digit / 100).unwrap());
61+
cents.push(u8::try_from(digit % 100).unwrap());
6262
}
6363

6464
let bigint = BigInt::from_radix_be(sign, &cents, 100)
@@ -83,7 +83,7 @@ impl TryFrom<&'_ BigDecimal> for PgNumeric {
8383

8484
// weight is positive power of 10000
8585
// exp is the negative power of 10
86-
let weight_10 = base_10.len() as i64 - exp;
86+
let weight_10 = i64::try_from(base_10.len())? - exp;
8787

8888
// scale is only nonzero when we have fractional digits
8989
// since `exp` is the _negative_ decimal exponent, it tells us
@@ -105,7 +105,7 @@ impl TryFrom<&'_ BigDecimal> for PgNumeric {
105105
base_10.len() / 4
106106
};
107107

108-
let offset = weight_10.rem_euclid(4) as usize;
108+
let offset = usize::try_from(weight_10.rem_euclid(4))?;
109109

110110
let mut digits = Vec::with_capacity(digits_len);
111111

@@ -114,14 +114,16 @@ impl TryFrom<&'_ BigDecimal> for PgNumeric {
114114
digits.push(base_10_to_10000(first));
115115
}
116116
} else if offset != 0 {
117-
digits.push(base_10_to_10000(&base_10) * 10i16.pow((offset - base_10.len()) as u32));
117+
digits.push(
118+
base_10_to_10000(&base_10) * 10i16.pow(u32::try_from(offset - base_10.len())?),
119+
);
118120
}
119121

120122
if let Some(rest) = base_10.get(offset..) {
121-
digits.extend(
122-
rest.chunks(4)
123-
.map(|chunk| base_10_to_10000(chunk) * 10i16.pow(4 - chunk.len() as u32)),
124-
);
123+
digits.extend(rest.chunks(4).map(|chunk| {
124+
base_10_to_10000(chunk)
125+
* 10i16.pow(4 - u32::try_from(chunk.len()).unwrap_or(u32::MAX))
126+
}));
125127
}
126128

127129
while let Some(&0) = digits.last() {
@@ -154,7 +156,7 @@ impl Encode<'_, Postgres> for BigDecimal {
154156
fn size_hint(&self) -> usize {
155157
// BigDecimal::digits() gives us base-10 digits, so we divide by 4 to get base-10000 digits
156158
// and since this is just a hint we just always round up
157-
8 + (self.digits() / 4 + 1) as usize * 2
159+
8 + usize::try_from(self.digits() / 4 + 1).unwrap() * 2
158160
}
159161
}
160162

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ impl TryFrom<PgNumeric> for Decimal {
5353
};
5454

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

5858
// no optimized algorithm for base-10 so use base-100 for faster processing
5959
let mut cents = Vec::with_capacity(digits.len() * 2);
6060
for digit in &digits {
61-
cents.push((digit / 100) as u8);
62-
cents.push((digit % 100) as u8);
61+
cents.push(u8::try_from(digit / 100)?);
62+
cents.push(u8::try_from(digit % 100)?);
6363
}
6464

6565
let bigint = BigInt::from_radix_be(sign, &cents, 100)
@@ -69,11 +69,11 @@ impl TryFrom<PgNumeric> for Decimal {
6969
// A negative scale, meaning we have nothing on the right and must
7070
// add zeroes to the left.
7171
(Some(num), scale) if scale < 0 => Ok(Decimal::from_i128_with_scale(
72-
num * 10i128.pow(scale.abs() as u32),
72+
num * 10i128.pow(u32::try_from(scale.abs())?),
7373
0,
7474
)),
7575
// A positive scale, so we have decimals on the right.
76-
(Some(num), _) => Ok(Decimal::from_i128_with_scale(num, scale as u32)),
76+
(Some(num), _) => Ok(Decimal::from_i128_with_scale(num, u32::try_from(scale)?)),
7777
(None, _) => Err("Decimal's integer part out of range.".into()),
7878
}
7979
}
@@ -111,8 +111,8 @@ impl TryFrom<&'_ Decimal> for PgNumeric {
111111
// multiple.
112112
let groups_diff = scale % 4;
113113
if groups_diff > 0 {
114-
let remainder = 4 - groups_diff as u32;
115-
let power = 10u32.pow(remainder as u32) as u128;
114+
let remainder = 4 - groups_diff;
115+
let power = 10u32.pow(remainder) as u128;
116116

117117
mantissa *= power;
118118
}

0 commit comments

Comments
 (0)