]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
time-util: make parse_timestamp() return -EINVAL if the input is very old date (...
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 11 Jul 2017 17:12:48 +0000 (02:12 +0900)
committerLennart Poettering <lennart@poettering.net>
Tue, 11 Jul 2017 17:12:48 +0000 (19:12 +0200)
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
src/test/test-date.c

index 151b44a8d9d9b5fcda65e7218ec332bd149d7c73..68ba86f6a55902ced6484646a325c03fc54fc84d 100644 (file)
@@ -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;
 
index 4984b0237977c048bca56e54a6455df3c9a0927d..0e7d44fade9f5d1e03e3d82c7d5642b7e642027c 100644 (file)
 
 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");