Skip to content

Commit 3bb9e91

Browse files
authored
fix(query): cast string to timestamp overflow (#17376)
1 parent 7fcd306 commit 3bb9e91

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/common/io/src/cursor_ext/cursor_read_datetime_ext.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ const DATE_LEN: usize = 10;
5858

5959
fn parse_time_part(buf: &[u8], size: usize) -> Result<u32> {
6060
if size > 0 && size < 3 {
61-
Ok(lexical_core::FromLexical::from_lexical(buf).unwrap())
61+
Ok(lexical_core::FromLexical::from_lexical(buf)
62+
.map_err_to_code(ErrorCode::BadBytes, || "time part parse error".to_string())?)
6263
} else {
6364
let msg = format!(
6465
"err with parse time part. Format like this:[03:00:00], got {} digits",
@@ -132,7 +133,7 @@ where T: AsRef<[u8]>
132133
match n {
133134
2 => {
134135
let hour_offset: i32 =
135-
lexical_core::FromLexical::from_lexical(buf.as_slice()).unwrap();
136+
lexical_core::FromLexical::from_lexical(buf.as_slice()).map_err_to_code(ErrorCode::BadBytes, || "hour offset parse error".to_string())?;
136137
if (0..15).contains(&hour_offset) {
137138
buf.clear();
138139
if self.ignore_byte(b':') {
@@ -143,7 +144,7 @@ where T: AsRef<[u8]>
143144
));
144145
}
145146
let minute_offset: i32 =
146-
lexical_core::FromLexical::from_lexical(buf.as_slice()).unwrap();
147+
lexical_core::FromLexical::from_lexical(buf.as_slice()).map_err_to_code(ErrorCode::BadBytes, || "minute offset parse error".to_string())?;
147148
// max utc: 14:00, min utc: 00:00
148149
get_hour_minute_offset(
149150
tz,
@@ -166,10 +167,10 @@ where T: AsRef<[u8]>
166167
4 => {
167168
let hour_offset = &buf.as_slice()[..2];
168169
let hour_offset: i32 =
169-
lexical_core::FromLexical::from_lexical(hour_offset).unwrap();
170+
lexical_core::FromLexical::from_lexical(hour_offset).map_err_to_code(ErrorCode::BadBytes, || "hour offset parse error".to_string())?;
170171
let minute_offset = &buf.as_slice()[2..];
171172
let minute_offset: i32 =
172-
lexical_core::FromLexical::from_lexical(minute_offset).unwrap();
173+
lexical_core::FromLexical::from_lexical(minute_offset).map_err_to_code(ErrorCode::BadBytes, || "minute offset parse error".to_string())?;
173174
buf.clear();
174175
// max utc: 14:00, min utc: 00:00
175176
if (0..15).contains(&hour_offset) {
@@ -257,8 +258,10 @@ where T: AsRef<[u8]>
257258
"Microsecond Parsing Error: Expecting a format like [.123456] for microseconds part",
258259
));
259260
}
260-
let mut scales: u64 =
261-
lexical_core::FromLexical::from_lexical(buf.as_slice()).unwrap();
261+
let mut scales: u64 = lexical_core::FromLexical::from_lexical(buf.as_slice())
262+
.map_err_to_code(ErrorCode::BadBytes, || {
263+
"datetime scales parse error".to_string()
264+
})?;
262265
if size <= 9 {
263266
scales *= 10_u64.pow(9 - size as u32)
264267
} else {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1495,4 +1495,5 @@ SELECT add_hours(to_timestamp(710455), 2147483647);
14951495
----
14961496
1000-01-01 00:00:00.000000
14971497

1498-
1498+
statement error 1006
1499+
SELECT CAST('2001-04-20 14:42:11.123000000000000000002001-04-20 14:42:11.12300000000000000000' AS TIMESTAMP);

0 commit comments

Comments
 (0)