mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-04-08 11:17:12 +00:00
fix(sqlite): audit for bad casts
This commit is contained in:
@@ -167,10 +167,20 @@ fn decode_datetime_from_float(value: f64) -> Option<DateTime<FixedOffset>> {
|
||||
let epoch_in_julian_days = 2_440_587.5;
|
||||
let seconds_in_day = 86400.0;
|
||||
let timestamp = (value - epoch_in_julian_days) * seconds_in_day;
|
||||
let seconds = timestamp as i64;
|
||||
let nanos = (timestamp.fract() * 1E9) as u32;
|
||||
|
||||
Utc.fix().timestamp_opt(seconds, nanos).single()
|
||||
if !timestamp.is_finite() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// We don't really have a choice but to do lossy casts for this conversion
|
||||
// We checked above if the value is infinite or NaN which could otherwise cause problems
|
||||
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
|
||||
{
|
||||
let seconds = timestamp.trunc() as i64;
|
||||
let nanos = (timestamp.fract() * 1E9).abs() as u32;
|
||||
|
||||
Utc.fix().timestamp_opt(seconds, nanos).single()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'r> Decode<'r, Sqlite> for NaiveDateTime {
|
||||
|
||||
@@ -24,6 +24,8 @@ impl<'q> Encode<'q, Sqlite> for f32 {
|
||||
|
||||
impl<'r> Decode<'r, Sqlite> for f32 {
|
||||
fn decode(value: SqliteValueRef<'r>) -> Result<f32, BoxDynError> {
|
||||
// Truncation is intentional
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
Ok(value.double() as f32)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user