Skip to content

Commit

Permalink
Restrict ISO parsing to be closer to standard
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
berland committed Oct 20, 2022
1 parent c69f900 commit 93cc585
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
12 changes: 11 additions & 1 deletion lib/util/tests/ert_util_sscan_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
6 changes: 5 additions & 1 deletion lib/util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 93cc585

Please sign in to comment.