Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of mul_checked to avoid silent overflow in interval arithmetic #3886

Merged
merged 1 commit into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions arrow-cast/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5273,6 +5273,28 @@ mod tests {
IntervalUnit::DayTime,
r#"Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"#
);

// overflow
test_unsafe_string_to_interval_err!(
vec![Some(format!(
"{} century {} year {} month",
i64::MAX - 2,
i64::MAX - 2,
i64::MAX - 2
))],
IntervalUnit::DayTime,
r#"Parser error: Parsed interval field value out of range: 11068046444225730000000 months 331764692165666300000000 days 28663672503769583000000000000000000000 nanos"#
);
test_unsafe_string_to_interval_err!(
vec![Some(format!(
"{} year {} month {} day",
i64::MAX - 2,
i64::MAX - 2,
i64::MAX - 2
))],
IntervalUnit::MonthDayNano,
r#"Parser error: Parsed interval field value out of range: 110680464442257310000 months 3043712772162076000000 days 262179884170819100000000000000000000 nanos"#
);
}

#[test]
Expand Down
27 changes: 16 additions & 11 deletions arrow-cast/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,30 +838,35 @@ fn parse_interval(leading_field: &str, value: &str) -> Result<MonthDayNano, Arro

match it {
IntervalType::Century => {
align_interval_parts(interval_period * 1200_f64, 0.0, 0.0)
align_interval_parts(interval_period.mul_checked(1200_f64)?, 0.0, 0.0)
}
IntervalType::Decade => {
align_interval_parts(interval_period * 120_f64, 0.0, 0.0)
align_interval_parts(interval_period.mul_checked(120_f64)?, 0.0, 0.0)
}
IntervalType::Year => {
align_interval_parts(interval_period * 12_f64, 0.0, 0.0)
align_interval_parts(interval_period.mul_checked(12_f64)?, 0.0, 0.0)
}
IntervalType::Month => align_interval_parts(interval_period, 0.0, 0.0),
IntervalType::Week => align_interval_parts(0.0, interval_period * 7_f64, 0.0),
IntervalType::Day => align_interval_parts(0.0, interval_period, 0.0),
IntervalType::Hour => Ok((
0,
0,
(interval_period * SECONDS_PER_HOUR * NANOS_PER_SECOND) as i64,
(interval_period.mul_checked(SECONDS_PER_HOUR * NANOS_PER_SECOND))?
as i64,
)),
IntervalType::Minute => Ok((
0,
0,
(interval_period.mul_checked(60_f64 * NANOS_PER_SECOND))? as i64,
)),
IntervalType::Second => Ok((
0,
0,
(interval_period.mul_checked(NANOS_PER_SECOND))? as i64,
)),
IntervalType::Minute => {
Ok((0, 0, (interval_period * 60_f64 * NANOS_PER_SECOND) as i64))
}
IntervalType::Second => {
Ok((0, 0, (interval_period * NANOS_PER_SECOND) as i64))
}
IntervalType::Millisecond => {
Ok((0, 0, (interval_period * 1_000_000f64) as i64))
Ok((0, 0, (interval_period.mul_checked(1_000_000f64))? as i64))
}
}
};
Expand Down