From 508bfbf8922fa4668418babdfeb9ed3551f10af0 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 27 Dec 2022 12:58:43 +0200 Subject: [PATCH] time: do not panic for `time.parse("2020-02-02 20.02.20")!`, just return an error instead (fix #16779) --- vlib/time/parse.c.v | 3 +++ vlib/time/parse_test.v | 17 ++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/vlib/time/parse.c.v b/vlib/time/parse.c.v index 6f6d695509..67e25c6317 100644 --- a/vlib/time/parse.c.v +++ b/vlib/time/parse.c.v @@ -70,6 +70,9 @@ pub fn parse(s string) !Time { } shms := s[pos..] hms := shms.split(':') + if hms.len != 3 { + return error_invalid_time(9) + } hour_ := hms[0][1..] minute_ := hms[1] second_ := hms[2] diff --git a/vlib/time/parse_test.v b/vlib/time/parse_test.v index 24fd647d65..3586a42446 100644 --- a/vlib/time/parse_test.v +++ b/vlib/time/parse_test.v @@ -13,12 +13,15 @@ fn test_parse() { } fn test_parse_invalid() { - s := 'Invalid time string' - time.parse(s) or { - assert true - return + if x := time.parse('Invalid time string') { + assert false } - assert false + assert true + + if x := time.parse('2020-02-02 02.20.02') { + assert false + } + assert true } fn test_parse_rfc2822() { @@ -186,14 +189,14 @@ fn test_parse_rfc3339() { } fn test_ad_second_to_parse_result_in_2001() { - now_tm := time.parse('2001-01-01 04:00:00')? + now_tm := time.parse('2001-01-01 04:00:00')! future_tm := now_tm.add_seconds(60) assert future_tm.str() == '2001-01-01 04:01:00' assert now_tm.unix < future_tm.unix } fn test_ad_second_to_parse_result_pre_2001() { - now_tm := time.parse('2000-01-01 04:00:00')? + now_tm := time.parse('2000-01-01 04:00:00')! future_tm := now_tm.add_seconds(60) assert future_tm.str() == '2000-01-01 04:01:00' assert now_tm.unix < future_tm.unix