From 93cc585619e70dcaabd0080f1365d4cbe8299215 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Berland?= Date: Wed, 19 Oct 2022 15:44:20 +0200 Subject: [PATCH] Restrict ISO parsing to be closer to standard ISO8601 requires exactly four digits for the year, while the current ISO parser in ecl allowed any number of digits. This commit will restrict the parser on this detail. --- lib/util/tests/ert_util_sscan_test.cpp | 12 +++++++++++- lib/util/util.cpp | 6 +++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/util/tests/ert_util_sscan_test.cpp b/lib/util/tests/ert_util_sscan_test.cpp index 7f90f5998..f743b1ba9 100644 --- a/lib/util/tests/ert_util_sscan_test.cpp +++ b/lib/util/tests/ert_util_sscan_test.cpp @@ -306,12 +306,22 @@ void test_sscanf_isodate() { time_t expected = util_make_date_utc(10, 11, 2011); check_iso_date(expected, "2011-11-10", true); + /* Valid dates, but incorrectly formatted */ test_assert_false(util_sscanf_isodate("2017.10.07", NULL)); + test_assert_false(util_sscanf_isodate("07.10.2017", NULL)); + test_assert_false(util_sscanf_isodate("7.10.2017", NULL)); + test_assert_false(util_sscanf_isodate("17.1.2017", NULL)); + test_assert_false(util_sscanf_isodate("17-01-2017", NULL)); test_assert_false(util_sscanf_isodate("2017-10.7", NULL)); test_assert_false(util_sscanf_isodate("2017/10/07", NULL)); test_assert_false(util_sscanf_isodate("07/10/2017", NULL)); - /* Invalid numeric values */ + test_assert_false(util_sscanf_isodate("217-07-10", NULL)); // year 217 + + /* ISO8601 does not support year 10000 */ + test_assert_false(util_sscanf_isodate("10000-01-01", NULL)); + + /* Invalid dates, correctly formatted */ test_assert_false(util_sscanf_isodate("2017-15-07", NULL)); test_assert_false(util_sscanf_isodate("2017-10-47", NULL)); diff --git a/lib/util/util.cpp b/lib/util/util.cpp index 402d18739..370e841e8 100644 --- a/lib/util/util.cpp +++ b/lib/util/util.cpp @@ -2652,8 +2652,12 @@ bool util_is_first_day_in_month_utc(time_t t) { bool util_sscanf_isodate(const char *date_token, time_t *t) { int day, month, year; + int year_digit1, year_digit2, year_digit3, year_digit4; - if (date_token && sscanf(date_token, "%d-%d-%d", &year, &month, &day) == 3) + if (date_token && + sscanf(date_token, "%1d%1d%1d%1d-%d-%d", &year_digit1, &year_digit2, + &year_digit3, &year_digit4, &month, &day) == 6 && + sscanf(date_token, "%d-%d-%d", &year, &month, &day) == 3) return util_make_datetime_utc__(0, 0, 0, day, month, year, false, t); if (t)