Index | Thread | Search

From:
Florian Obser <florian@openbsd.org>
Subject:
Re: date & touch: timegm/mktime checks
To:
Theo Buehler <tb@theobuehler.org>
Cc:
tech@openbsd.org
Date:
Sun, 21 Jun 2026 16:59:03 +0200

Download raw body.

Thread
reads correct, OK florian
On 2026-06-21 10:11 +02, Theo Buehler <tb@theobuehler.org> wrote:
> Another bunch of straightforward changes. The checks in touch don't make
> that code prettier, though.
>
> Index: bin/date/date.c
> ===================================================================
> RCS file: /cvs/src/bin/date/date.c,v
> diff -u -p -r1.60 date.c
> --- bin/date/date.c	28 Apr 2024 16:43:15 -0000	1.60
> +++ bin/date/date.c	21 Jun 2026 08:03:07 -0000
> @@ -223,11 +223,12 @@ setthetime(char *p, const char *pformat)
>  	}
>  
>  	/* convert broken-down time to UTC clock time */
> +	lt->tm_wday = -1;
>  	if (pformat != NULL && strstr(pformat, "%s") != NULL)
>  		tval = timegm(lt);
>  	else
>  		tval = mktime(lt);
> -	if (tval == -1)
> +	if (tval == -1 && lt->tm_wday == -1)
>  		errx(1, "specified date is outside allowed range");
>  
>  	if (jflag)
> Index: usr.bin/touch/touch.c
> ===================================================================
> RCS file: /cvs/src/usr.bin/touch/touch.c,v
> diff -u -p -r1.27 touch.c
> --- usr.bin/touch/touch.c	29 Jan 2022 00:06:26 -0000	1.27
> +++ usr.bin/touch/touch.c	21 Jun 2026 08:06:55 -0000
> @@ -223,8 +223,9 @@ stime_arg1(char *arg, struct timespec *t
>  	}
>  
>  	lt->tm_isdst = -1;		/* Figure out DST. */
> +	lt->tm_wday = -1;
>  	tsp[0].tv_sec = tsp[1].tv_sec = mktime(lt);
> -	if (tsp[0].tv_sec == -1)
> +	if (tsp[0].tv_sec == -1 && lt->tm_wday == -1)
>  terr:		errx(1,
>  	"out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
>  
> @@ -264,8 +265,9 @@ stime_arg2(char *arg, int year, struct t
>  	lt->tm_sec = 0;
>  
>  	lt->tm_isdst = -1;		/* Figure out DST. */
> +	lt->tm_wday = -1;
>  	tsp[0].tv_sec = tsp[1].tv_sec = mktime(lt);
> -	if (tsp[0].tv_sec == -1)
> +	if (tsp[0].tv_sec == -1 && lt->tm_wday == -1)
>  terr:		errx(1,
>  	"out of range or illegal time specification: MMDDhhmm[YY]");
>  
> @@ -323,8 +325,9 @@ stime_argd(char *arg, struct timespec *t
>  		goto terr;
>  
>  	tm.tm_isdst = -1;
> +	tm.tm_wday = -1;
>  	tsp[0].tv_sec = utc ? timegm(&tm) : mktime(&tm);
> -	if (tsp[0].tv_sec == -1)
> +	if (tsp[0].tv_sec == -1 && tm.tm_wday == -1)
>  terr:		errx(1,
>    "out of range or illegal time specification: YYYY-MM-DDThh:mm:ss[.frac][Z]");
>  	tsp[1] = tsp[0];
>

-- 
In my defence, I have been left unsupervised.