time-util: make parse_timestamp() set 0 if the input is very old date (#6297)
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 8 Jul 2017 19:59:07 +0000 (04:59 +0900)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 8 Jul 2017 19:59:07 +0000 (15:59 -0400)
If the input is older than "1970-01-01 UTC", then `parse_timestamp()`
fails and returns -EINVAL. However, if the input is e.g. `-100years`,
then the function succeeds and sets `usec = 0`.
This commit makes the function also succeed for old dates and set
`usec = 0`.

Fixes #6290.

src/basic/time-util.c
src/test/test-date.c

index 3b44985..151b44a 100644 (file)
@@ -847,19 +847,23 @@ parse_usec:
 
 from_tm:
         x = mktime_or_timegm(&tm, utc);
-        if (x < 0)
-                return -EINVAL;
+        if (x == (time_t) -1)
+                return -EOVERFLOW;
 
         if (weekday >= 0 && tm.tm_wday != weekday)
                 return -EINVAL;
 
-        ret = (usec_t) x * USEC_PER_SEC + x_usec;
+        if (x < 0)
+                ret = 0;
+        else
+                ret = (usec_t) x * USEC_PER_SEC + x_usec;
+
         if (ret > USEC_TIMESTAMP_FORMATTABLE_MAX)
                 return -EINVAL;
 
 finish:
         if (ret + plus < ret) /* overflow? */
-                return -EINVAL;
+                return -EOVERFLOW;
         ret += plus;
         if (ret > USEC_TIMESTAMP_FORMATTABLE_MAX)
                 return -EINVAL;
index a952f77..4984b02 100644 (file)
@@ -27,6 +27,7 @@ static void test_should_pass(const char *p) {
         usec_t t, q;
         char buf[FORMAT_TIMESTAMP_MAX], buf_relative[FORMAT_TIMESTAMP_RELATIVE_MAX], *sp;
 
+        log_info("Test: %s", p);
         assert_se(parse_timestamp(p, &t) >= 0);
         format_timestamp_us(buf, sizeof(buf), t);
         log_info("\"%s\" → \"%s\"", p, buf);
@@ -41,25 +42,32 @@ static void test_should_pass(const char *p) {
 
         format_timestamp_relative(buf_relative, sizeof(buf_relative), t);
         log_info("%s", strna(buf_relative));
-        assert_se(parse_timestamp(buf, &q) >= 0);
 }
 
 static void test_should_parse(const char *p) {
         usec_t t;
 
+        log_info("Test: %s", p);
         assert_se(parse_timestamp(p, &t) >= 0);
+        log_info("\"%s\" → \"@%" PRI_USEC "\"", p, t);
 }
 
 static void test_should_fail(const char *p) {
         usec_t t;
+        int r;
 
-        assert_se(parse_timestamp(p, &t) < 0);
+        log_info("Test: %s", p);
+        r = parse_timestamp(p, &t);
+        if (r >= 0)
+                log_info("\"%s\" → \"@%" PRI_USEC "\" (unexpected)", p, t);
+        else
+                log_info("parse_timestamp() returns %d (expected)", r);
+        assert_se(r < 0);
 }
 
 static void test_one(const char *p) {
         _cleanup_free_ char *with_utc;
 
-        log_info("Test: %s", p);
         with_utc = strjoin(p, " UTC");
         test_should_pass(p);
         test_should_pass(with_utc);
@@ -68,7 +76,6 @@ static void test_one(const char *p) {
 static void test_one_noutc(const char *p) {
         _cleanup_free_ char *with_utc;
 
-        log_info("Test: %s", p);
         with_utc = strjoin(p, " UTC");
         test_should_pass(p);
         test_should_fail(with_utc);
@@ -93,17 +100,17 @@ int main(int argc, char *argv[]) {
         test_one_noutc("+2y 4d");
         test_one_noutc("5months ago");
         test_one_noutc("@1395716396");
-        test_should_parse("today UTC");
         test_should_fail("today UTC UTC");
         test_should_parse("1970-1-1 UTC");
-        test_should_fail("1969-1-1 UTC");
+        test_should_parse("1969-12-31 UTC");
+        test_should_parse("-100y");
 #if SIZEOF_TIME_T == 8
-        test_should_parse("9999-12-30 23:59:59 UTC");
+        test_should_pass("9999-12-30 23:59:59 UTC");
         test_should_fail("9999-12-31 00:00:00 UTC");
         test_should_fail("10000-01-01 00:00:00 UTC");
 #elif SIZEOF_TIME_T == 4
-        test_should_parse("2038-01-19 03:14:07 UTC");
-        test_should_fail( "2038-01-19 03:14:08 UTC");
+        test_should_pass("2038-01-19 03:14:07 UTC");
+        test_should_fail("2038-01-19 03:14:08 UTC");
 #endif
 
         return 0;