time-util: make parse_timestamp() return -EINVAL if the input is very old date (#6327)

This reverts 7635ab8e74 and makes parse_timestamp()
return -EINVAL if the input is older than 1970-01-01.

Fixes #6290.
This commit is contained in:
Yu Watanabe 2017-07-12 02:12:48 +09:00 committed by Lennart Poettering
parent 634735b56b
commit 68bdd2d2d3
2 changed files with 15 additions and 23 deletions

View 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;

View file

@ -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");