Skip to content

Commit 105809b

Browse files
authored
fix(query): add_hours function may panic if the argument is too big (#16929)
fix(query): add_hours function may panic if the argument is too big
1 parent 794ec0e commit 105809b

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/query/expression/src/utils/date_helper.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ impl EvalDaysImpl {
293293
}
294294

295295
pub fn eval_timestamp(date: i64, delta: impl AsPrimitive<i64>) -> i64 {
296-
let mut value = date.wrapping_add(delta.as_() * MICROSECS_PER_DAY);
296+
let mut value = date.wrapping_add(delta.as_().wrapping_mul(MICROSECS_PER_DAY));
297297
clamp_timestamp(&mut value);
298298
value
299299
}
@@ -311,12 +311,13 @@ pub struct EvalTimesImpl;
311311
impl EvalTimesImpl {
312312
pub fn eval_date(date: i32, delta: impl AsPrimitive<i64>, factor: i64) -> i32 {
313313
clamp_date(
314-
(date as i64 * MICROSECS_PER_DAY).wrapping_add(delta.as_() * factor * MICROS_PER_SEC),
314+
(date as i64 * MICROSECS_PER_DAY)
315+
.wrapping_add(delta.as_().wrapping_mul(factor * MICROS_PER_SEC)),
315316
)
316317
}
317318

318319
pub fn eval_timestamp(us: i64, delta: impl AsPrimitive<i64>, factor: i64) -> i64 {
319-
let mut ts = us.wrapping_add(delta.as_() * factor * MICROS_PER_SEC);
320+
let mut ts = us.wrapping_add(delta.as_().wrapping_mul(factor * MICROS_PER_SEC));
320321
clamp_timestamp(&mut ts);
321322
ts
322323
}

tests/sqllogictests/suites/query/functions/02_0012_function_datetimes.test

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,3 +1466,23 @@ query T
14661466
select TRY_TO_TIMESTAMP(1, 0), TRY_TO_TIMESTAMP(1, null);
14671467
----
14681468
1970-01-01 00:00:01.000000 NULL
1469+
1470+
query T
1471+
SELECT add_hours(to_date(710455), 1512263452497496403);
1472+
----
1473+
1000-01-01 00:00:00.000000
1474+
1475+
query T
1476+
SELECT add_hours(to_timestamp(710455), 1512263452497496403);
1477+
----
1478+
1000-01-01 00:00:00.000000
1479+
1480+
query T
1481+
SELECT add_hours(to_date(710455), 2147483647);
1482+
----
1483+
1000-01-01 00:00:00.000000
1484+
1485+
query T
1486+
SELECT add_hours(to_timestamp(710455), 2147483647);
1487+
----
1488+
1000-01-01 00:00:00.000000

0 commit comments

Comments
 (0)