From 3b394781b5b9301fc489d9bfff6fbefb4db190f3 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 21 Jan 2014 07:58:18 +0100 Subject: libbb: fix parsing of "10101010" date/time form Signed-off-by: Denys Vlasenko --- libbb/time.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'libbb') diff --git a/libbb/time.c b/libbb/time.c index ea2f72e..aa19a47 100644 --- a/libbb/time.c +++ b/libbb/time.c @@ -68,15 +68,23 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) /* else end != NUL and we error out */ } } else - /* yyyy-mm-dd HH */ - if (sscanf(date_str, "%u-%u-%u %u%c", &ptm->tm_year, + if (strchr(date_str, '-') + /* Why strchr('-') check? + * sscanf below will trash ptm->tm_year, this breaks + * if parse_str is "10101010" (iow, "MMddhhmm" form) + * because we destroy year. Do these sscanf + * only if we saw a dash in parse_str. + */ + /* yyyy-mm-dd HH */ + && (sscanf(date_str, "%u-%u-%u %u%c", &ptm->tm_year, &ptm->tm_mon, &ptm->tm_mday, &ptm->tm_hour, &end) >= 4 - /* yyyy-mm-dd */ - || sscanf(date_str, "%u-%u-%u%c", &ptm->tm_year, + /* yyyy-mm-dd */ + || sscanf(date_str, "%u-%u-%u%c", &ptm->tm_year, &ptm->tm_mon, &ptm->tm_mday, &end) >= 3 + ) ) { ptm->tm_year -= 1900; /* Adjust years */ ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */ -- cgit v1.1