From 68bdd2d2d3c88dd738c2b9a6fabf518111e204a9 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 12 Jul 2017 02:12:48 +0900 Subject: [PATCH] time-util: make parse_timestamp() return -EINVAL if the input is very old date (#6327) This reverts 7635ab8e74ea4a94e81143c3077570a986df375c and makes parse_timestamp() return -EINVAL if the input is older than 1970-01-01. Fixes #6290. --- src/basic/time-util.c | 18 +++++++----------- src/test/test-date.c | 20 ++++++++------------ 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/src/basic/time-util.c b/src/basic/time-util.c index 151b44a8d9d..68ba86f6a55 100644 --- a/src/basic/time-util.c +++ b/src/basic/time-util.c @@ -241,7 +241,7 @@ usec_t timeval_load(const struct timeval *tv) { struct timeval *timeval_store(struct timeval *tv, usec_t u) { assert(tv); - if (u == USEC_INFINITY|| + if (u == USEC_INFINITY || u / USEC_PER_SEC > TIME_T_MAX) { tv->tv_sec = (time_t) -1; tv->tv_usec = (suseconds_t) -1; @@ -847,31 +847,27 @@ parse_usec: from_tm: x = mktime_or_timegm(&tm, utc); - if (x == (time_t) -1) - return -EOVERFLOW; + if (x < 0) + return -EINVAL; if (weekday >= 0 && tm.tm_wday != weekday) return -EINVAL; - if (x < 0) - ret = 0; - else - ret = (usec_t) x * USEC_PER_SEC + x_usec; - + ret = (usec_t) x * USEC_PER_SEC + x_usec; if (ret > USEC_TIMESTAMP_FORMATTABLE_MAX) return -EINVAL; finish: if (ret + plus < ret) /* overflow? */ - return -EOVERFLOW; + return -EINVAL; ret += plus; if (ret > USEC_TIMESTAMP_FORMATTABLE_MAX) return -EINVAL; - if (ret > minus) + if (ret >= minus) ret -= minus; else - ret = 0; + return -EINVAL; *usec = ret; diff --git a/src/test/test-date.c b/src/test/test-date.c index 4984b023797..0e7d44fade9 100644 --- a/src/test/test-date.c +++ b/src/test/test-date.c @@ -25,22 +25,17 @@ static void test_should_pass(const char *p) { usec_t t, q; - char buf[FORMAT_TIMESTAMP_MAX], buf_relative[FORMAT_TIMESTAMP_RELATIVE_MAX], *sp; + char buf[FORMAT_TIMESTAMP_MAX], buf_relative[FORMAT_TIMESTAMP_RELATIVE_MAX]; log_info("Test: %s", p); assert_se(parse_timestamp(p, &t) >= 0); - format_timestamp_us(buf, sizeof(buf), t); + assert_se(format_timestamp_us(buf, sizeof(buf), t)); log_info("\"%s\" → \"%s\"", p, buf); - /* Chop off timezone */ - sp = strrchr(buf, ' '); - assert_se(sp); - *sp = 0; - assert_se(parse_timestamp(buf, &q) >= 0); assert_se(q == t); - format_timestamp_relative(buf_relative, sizeof(buf_relative), t); + assert_se(format_timestamp_relative(buf_relative, sizeof(buf_relative), t)); log_info("%s", strna(buf_relative)); } @@ -92,18 +87,19 @@ int main(int argc, char *argv[]) { test_one("2012-12-30 18:42"); test_one("2012-10-02"); test_one("Tue 2012-10-02"); - test_one_noutc("now"); test_one("yesterday"); test_one("today"); test_one("tomorrow"); + test_one_noutc("now"); test_one_noutc("+2d"); test_one_noutc("+2y 4d"); test_one_noutc("5months ago"); test_one_noutc("@1395716396"); - test_should_fail("today UTC UTC"); test_should_parse("1970-1-1 UTC"); - test_should_parse("1969-12-31 UTC"); - test_should_parse("-100y"); + test_should_pass("1970-1-1 00:00:01 UTC"); + test_should_fail("1969-12-31 UTC"); + test_should_fail("-100y"); + test_should_fail("today UTC UTC"); #if SIZEOF_TIME_T == 8 test_should_pass("9999-12-30 23:59:59 UTC"); test_should_fail("9999-12-31 00:00:00 UTC"); -- 2.47.3