From b6f7a3fe15f2f253a1653454f514b467aa20f821 Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Mon, 14 Mar 2022 13:25:52 +0100 Subject: improve time(NULL) error checking Use errx, time(NULL) does not set errno. For sfeed_curses reset errno so it doesn't print a random error if it failed. POSIX recommends checking against (time_t)-1 on failure. Note that some implementation, like the OpenBSD man page says time() cannot fail. --- sfeed_atom.c | 4 ++-- sfeed_curses.c | 3 ++- sfeed_frames.c | 4 ++-- sfeed_gopher.c | 4 ++-- sfeed_html.c | 4 ++-- sfeed_mbox.c | 4 ++-- sfeed_plain.c | 4 ++-- 7 files changed, 14 insertions(+), 13 deletions(-) diff --git a/sfeed_atom.c b/sfeed_atom.c index 1dff75b..2aea54e 100644 --- a/sfeed_atom.c +++ b/sfeed_atom.c @@ -124,8 +124,8 @@ main(int argc, char *argv[]) if (pledge(argc == 1 ? "stdio" : "stdio rpath", NULL) == -1) err(1, "pledge"); - if ((now = time(NULL)) == -1) - err(1, "time"); + if ((now = time(NULL)) == (time_t)-1) + errx(1, "time"); if (!(tm = gmtime_r(&now, &tmnow))) err(1, "gmtime_r"); diff --git a/sfeed_curses.c b/sfeed_curses.c index 4d77504..1c6bc24 100644 --- a/sfeed_curses.c +++ b/sfeed_curses.c @@ -1308,7 +1308,8 @@ feeds_load(struct feed *feeds, size_t nfeeds) struct feed *f; size_t i; - if ((comparetime = time(NULL)) == -1) + errno = 0; + if ((comparetime = time(NULL)) == (time_t)-1) die("time"); /* 1 day is old news */ comparetime -= 86400; diff --git a/sfeed_frames.c b/sfeed_frames.c index 89838cc..d345c60 100644 --- a/sfeed_frames.c +++ b/sfeed_frames.c @@ -86,8 +86,8 @@ main(int argc, char *argv[]) if (!(feeds = calloc(argc, sizeof(struct feed)))) err(1, "calloc"); - if ((comparetime = time(NULL)) == -1) - err(1, "time"); + if ((comparetime = time(NULL)) == (time_t)-1) + errx(1, "time"); /* 1 day is old news */ comparetime -= 86400; diff --git a/sfeed_gopher.c b/sfeed_gopher.c index 9e8e10a..0b95b3d 100644 --- a/sfeed_gopher.c +++ b/sfeed_gopher.c @@ -137,8 +137,8 @@ main(int argc, char *argv[]) err(1, "pledge"); } - if ((comparetime = time(NULL)) == -1) - err(1, "time"); + if ((comparetime = time(NULL)) == (time_t)-1) + errx(1, "time"); /* 1 day is old news */ comparetime -= 86400; diff --git a/sfeed_html.c b/sfeed_html.c index d8abda7..240ea66 100644 --- a/sfeed_html.c +++ b/sfeed_html.c @@ -86,8 +86,8 @@ main(int argc, char *argv[]) if (!(feeds = calloc(argc, sizeof(struct feed)))) err(1, "calloc"); - if ((comparetime = time(NULL)) == -1) - err(1, "time"); + if ((comparetime = time(NULL)) == (time_t)-1) + errx(1, "time"); /* 1 day is old news */ comparetime -= 86400; diff --git a/sfeed_mbox.c b/sfeed_mbox.c index 6419d16..c2827d4 100644 --- a/sfeed_mbox.c +++ b/sfeed_mbox.c @@ -152,8 +152,8 @@ main(int argc, char *argv[]) user = "you"; if (gethostname(host, sizeof(host)) == -1) err(1, "gethostname"); - if ((now = time(NULL)) == -1) - err(1, "time"); + if ((now = time(NULL)) == (time_t)-1) + errx(1, "time"); if (!gmtime_r(&now, &tmnow)) err(1, "gmtime_r: can't get current time"); if (!strftime(mtimebuf, sizeof(mtimebuf), "%a %b %d %H:%M:%S %Y", &tmnow)) diff --git a/sfeed_plain.c b/sfeed_plain.c index df93a5a..45785e7 100644 --- a/sfeed_plain.c +++ b/sfeed_plain.c @@ -62,8 +62,8 @@ main(int argc, char *argv[]) if (pledge(argc == 1 ? "stdio" : "stdio rpath", NULL) == -1) err(1, "pledge"); - if ((comparetime = time(NULL)) == -1) - err(1, "time"); + if ((comparetime = time(NULL)) == (time_t)-1) + errx(1, "time"); /* 1 day is old news */ comparetime -= 86400; -- cgit v1.2.3