aboutsummaryrefslogtreecommitdiff
path: root/bin/date/date.c
diff options
context:
space:
mode:
authorEd Maste <emaste@FreeBSD.org>2017-07-20 15:28:48 +0000
committerEd Maste <emaste@FreeBSD.org>2017-07-20 15:28:48 +0000
commit819129649da42cda4b66484ca358e2c8119195df (patch)
tree113f803ecf9c2f1cf4a4a9f4807ae9a8d1c58d82 /bin/date/date.c
parent9dd6ca960263f753a17841219abcda81e5f147b5 (diff)
downloadsrc-819129649da42cda4b66484ca358e2c8119195df.tar.gz
src-819129649da42cda4b66484ca358e2c8119195df.zip
date: avoid crash on invalid time
localtime(3) returns NULL when passed an invalid time_t but date(1) previously did not handle it. Exit with an error in that case. PR: 220828 Reported by: Vinícius Zavam Reviewed by: cem, kevans Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D11660
Notes
Notes: svn path=/head/; revision=321293
Diffstat (limited to 'bin/date/date.c')
-rw-r--r--bin/date/date.c12
1 files changed, 8 insertions, 4 deletions
diff --git a/bin/date/date.c b/bin/date/date.c
index 79ef2253a6e7..7f2e1b28a64c 100644
--- a/bin/date/date.c
+++ b/bin/date/date.c
@@ -85,7 +85,7 @@ main(int argc, char *argv[])
int set_timezone;
struct vary *v;
const struct vary *badv;
- struct tm lt;
+ struct tm *lt;
struct stat sb;
v = NULL;
@@ -174,8 +174,10 @@ main(int argc, char *argv[])
if (*argv && **argv == '+')
format = *argv + 1;
- lt = *localtime(&tval);
- badv = vary_apply(v, &lt);
+ lt = localtime(&tval);
+ if (lt == NULL)
+ errx(1, "invalid time");
+ badv = vary_apply(v, lt);
if (badv) {
fprintf(stderr, "%s: Cannot apply date adjustment\n",
badv->arg);
@@ -191,7 +193,7 @@ main(int argc, char *argv[])
*/
setlocale(LC_TIME, "C");
- (void)strftime(buf, sizeof(buf), format, &lt);
+ (void)strftime(buf, sizeof(buf), format, lt);
(void)printf("%s\n", buf);
if (fflush(stdout))
err(1, "stdout");
@@ -210,6 +212,8 @@ setthetime(const char *fmt, const char *p, int jflag, int nflag)
int century;
lt = localtime(&tval);
+ if (lt == NULL)
+ errx(1, "invalid time");
lt->tm_isdst = -1; /* divine correct DST */
if (fmt != NULL) {