From 369526ea9a50c99dff93f1d8fd2380919adb5ad2 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Fri, 23 Jun 2023 19:05:33 -0500 Subject: [PATCH] Fix "date -s +abc" segfault pointed out by Franke Busse. While I'm there, enforce max 1 argument, and clean up confused else case. --- toys/posix/date.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/toys/posix/date.c b/toys/posix/date.c index 5a0f27e1..98038425 100644 --- a/toys/posix/date.c +++ b/toys/posix/date.c @@ -7,7 +7,7 @@ * Note: setting a 2 year date is 50 years back/forward from today, * not posix's hardwired magic dates. -USE_DATE(NEWTOY(date, "d:D:I(iso-8601):;r:s:u(utc)[!dr]", TOYFLAG_BIN)) +USE_DATE(NEWTOY(date, ">1d:D:I(iso-8601):;r:s:u(utc)[!dr]", TOYFLAG_BIN)) config DATE bool "date" @@ -176,15 +176,14 @@ void date_main(void) setdate = TT.s; } - // Fall through if no arguments + // Fall through to display if no arguments if (!setdate); - // Display the date? - else if (*setdate == '+') { + // Supplying a format argument means display + else if (toys.optc && *toys.optargs[0] == '+') format_string = toys.optargs[0]+1; - setdate = toys.optargs[1]; // Set the date - } else if (setdate) { + else { struct timeval tv; parse_date(setdate, &t); -- 2.39.2