commit b6f7a3fe15f2f253a1653454f514b467aa20f821
parent 3dbdf1ccfa5f109f8d7489f913ed57aa8e9b5f11
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Mon, 14 Mar 2022 13:25:52 +0100
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.
Diffstat:
7 files changed, 14 insertions(+), 13 deletions(-)
diff --git 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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;