From e46ef96d680fe3d720a54ff1c178cce03d3a980a Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Fri, 7 Aug 2015 21:23:58 +0200 Subject: util: strtotime: stricter time parsing as input: an empty string or non-digit characters are digits are considered an error now. Still, for the format tools output the formatted time string as time_t 0 on a parse error. --- sfeed_frames.c | 9 ++++----- sfeed_html.c | 7 ++++--- sfeed_mbox.c | 9 +++++---- sfeed_plain.c | 6 ++++-- util.c | 5 +++-- 5 files changed, 20 insertions(+), 16 deletions(-) diff --git a/sfeed_frames.c b/sfeed_frames.c index e5252a5..70c531d 100644 --- a/sfeed_frames.c +++ b/sfeed_frames.c @@ -105,8 +105,7 @@ static void printfeed(FILE *fpitems, FILE *fpin, struct feed *f) { char dirpath[PATH_MAX], filepath[PATH_MAX]; - char *fields[FieldLast], *feedname; - char name[128]; + char *fields[FieldLast], *feedname, name[128]; size_t namelen; struct stat st; FILE *fpcontent = NULL; @@ -177,14 +176,14 @@ printfeed(FILE *fpitems, FILE *fpin, struct feed *f) } /* set modified and access time of file to time of item. */ - r = strtotime(fields[FieldUnixTimestamp], &parsedtime); - if (r != -1) { + parsedtime = 0; + if (strtotime(fields[FieldUnixTimestamp], &parsedtime) != -1) { contenttime.actime = parsedtime; contenttime.modtime = parsedtime; utime(filepath, &contenttime); } - isnew = (r != -1 && parsedtime >= comparetime) ? 1 : 0; + isnew = (parsedtime >= comparetime) ? 1 : 0; totalnew += isnew; f->totalnew += isnew; f->total++; diff --git a/sfeed_html.c b/sfeed_html.c index fb4ed8b..2f729d7 100644 --- a/sfeed_html.c +++ b/sfeed_html.c @@ -20,7 +20,6 @@ printfeed(FILE *fp, struct feed *f) char *fields[FieldLast]; time_t parsedtime; unsigned int islink, isnew; - int r; if (f->name[0] != '\0') { fputs("

\n", stdout); while (parseline(&line, &linesize, fields, fp) > 0) { - r = strtotime(fields[FieldUnixTimestamp], &parsedtime); - isnew = (r != -1 && parsedtime >= comparetime) ? 1 : 0; + parsedtime = 0; + strtotime(fields[FieldUnixTimestamp], &parsedtime); + + isnew = (parsedtime >= comparetime) ? 1 : 0; islink = (fields[FieldLink][0] != '\0') ? 1 : 0; totalnew += isnew; diff --git a/sfeed_mbox.c b/sfeed_mbox.c index 878a3f6..020416c 100644 --- a/sfeed_mbox.c +++ b/sfeed_mbox.c @@ -81,7 +81,6 @@ printfeed(FILE *fp, const char *feedname) char *fields[FieldLast], timebuf[32], mtimebuf[32]; char host[HOST_NAME_MAX + 1], *user; time_t parsedtime; - int r; if (!(user = getenv("USER"))) user = "you"; @@ -96,11 +95,13 @@ printfeed(FILE *fp, const char *feedname) errx(1, "can't format current time"); while (parseline(&line, &linesize, fields, fp) > 0) { - if ((r = strtotime(fields[FieldUnixTimestamp], &parsedtime)) == -1 || - !gmtime_r(&parsedtime, &tm) || + parsedtime = 0; + strtotime(fields[FieldUnixTimestamp], &parsedtime); + /* can't convert: default to formatted time for time_t 0. */ + if (!gmtime_r(&parsedtime, &tm) || !strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M +0000", &tm)) - continue; /* invalid date */ + strlcpy(timebuf, "Thu, 01 Jan 1970 00:00 +0000", sizeof(timebuf)); /* mbox + mail header */ printf("From MAILER-DAEMON %s\n" diff --git a/sfeed_plain.c b/sfeed_plain.c index dd7b063..23cb868 100644 --- a/sfeed_plain.c +++ b/sfeed_plain.c @@ -42,8 +42,10 @@ printfeed(FILE *fp, const char *feedname) time_t parsedtime; while (parseline(&line, &size, fields, fp) > 0) { - if (strtotime(fields[FieldUnixTimestamp], &parsedtime) != -1 && - parsedtime >= comparetime) + parsedtime = 0; + strtotime(fields[FieldUnixTimestamp], &parsedtime); + + if (parsedtime >= comparetime) fputs("N ", stdout); else fputs(" ", stdout); diff --git a/util.c b/util.c index a22b235..6f4b04e 100644 --- a/util.c +++ b/util.c @@ -188,10 +188,11 @@ int strtotime(const char *s, time_t *t) { long l; + char *e; errno = 0; - l = strtol(s, NULL, 10); - if (errno != 0) + l = strtol(s, &e, 10); + if (*s == '\0' || *e != '\0') return -1; if (t) *t = (time_t)l; -- cgit v1.2.3