|
|
Message-ID: <20261121122816.GN5716@brightrain.aerifal.cx>
Date: Sat, 21 Nov 2026 07:28:16 -0500
From: Rich Felker <dalias@...c.org>
To: AyadiCH <ayadich@...ton.me>
Cc: musl@...ts.openwall.com
Subject: Re: getdate() returns invalid date
On Sat, Nov 21, 2026 at 10:28:47AM +0000, AyadiCH wrote:
>
>
>
>
> Sent with Proton Mail secure email.
>
> On Friday, November 20th, 2026 at 3:46 PM, Bruno Haible <bruno@...sp.org> wrote:
>
> > Hi,
> >
> > POSIX:2024 specifies getdate() in
> > https://pubs.opengroup.org/onlinepubs/9799919799/functions/getdate.html
> >
> > Among others, in the 4th example, last line of the table, it has an example
> > where
> > Line in template = %H:%M
> >
> > Here's my test case:
> >
> > $ cat datemsk
> > %m
> > %A %B %d, %Y, %H:%M:%S
> > %A
> > %B
> > %m/%d/%y %I %p
> > %d,%m,%Y %H:%M
> > %m/%d/%y
> > %d.%m.%y
> > %y-%m-%d
> > %A %H:%M:%S
> > %a
> > %b %a
> > %b %a %Y
> > %a %H
> > %b %H:%S
> > %H:%M
> >
> > $ cat foo.c
> > #define _GNU_SOURCE 1
> > #include <time.h>
> > #include <stdio.h>
> >
> > int
> > main ()
> > {
> > struct tm *t = getdate ("13:30");
> > if (t == NULL)
> > printf ("getdate failed\n");
> > else
> > {
> > char buf[100];
> > strftime (buf, sizeof (buf), "getdate returned %Y-%m-%d %H:%M:%S", t);
> > puts (buf);
> > }
> > }
> >
> > Build and run it:
> > $ gcc -Wall foo.c
> > $ DATEMSK=`pwd`/datemsk ./a.out
> >
> > Expected output:
> > getdate returned 2026-11-21 13:30:00
> >
> > Actual output:
> > getdate returned 1900-14-13 13:30:00
> >
> > Not only the year and the day field are wrong; also the month field is
> > out-of-range.
> >
> > If I reduce 'datemsk' to a single line "%H:%M":
> >
> > Expected output:
> > getdate returned 2026-11-21 13:30:00
> >
> > Actual output:
> > getdate returned 1900-01-00 13:30:00
> >
> > In this case the day field is out-of-range.
> >
> > Seen in Alpine Linux 3.24 (i.e. musl 1.2.6-r3, according to [1]).
> >
> > Bruno
> >
> > [1] https://pkgs.alpinelinux.org/packages?name=musl&branch=v3.24&repo=&arch=x86_64&origin=&flagged=&maintainer=
> >
> >
> >
> >
>
> hmm, i think that this caused by undifned vriables, due to unset date value, so the values u see is random, or depending on the prevuce stack value, i made a quick fix, it works for me:
>
> >From 936014c4d90a8ec4f06bc88c557f731b1d0905fd Mon Sep 17 00:00:00 2001
> From: Ayadi <ayadich@...ton.me>
> Date: Sat, 21 Nov 2026 11:23:31 +0100
> Subject: [PATCH] fixing getdate
>
> ---
> src/time/getdate.c | 9 +++++----
> 1 file changed, 5 insertions(+), 4 deletions(-)
>
> diff --git a/src/time/getdate.c b/src/time/getdate.c
> index 420cd8e4..6baa2d21 100644
> --- a/src/time/getdate.c
> +++ b/src/time/getdate.c
> @@ -8,7 +8,7 @@ int getdate_err;
>
> struct tm *getdate(const char *s)
> {
> - static struct tm tmbuf;
> + static struct tm* tmbuf;
> struct tm *ret = 0;
> char *datemsk = getenv("DATEMSK");
> FILE *f = 0;
> @@ -28,11 +28,12 @@ struct tm *getdate(const char *s)
> else getdate_err = 2;
> goto out;
> }
> -
> + time_t rtm;time(&rtm);
> + tmbuf = localtime(&rtm);
> while (fgets(fmt, sizeof fmt, f)) {
> - p = strptime(s, fmt, &tmbuf);
> + p = strptime(s, fmt, tmbuf);
> if (p && !*p) {
> - ret = &tmbuf;
> + ret = tmbuf;
> goto out;
> }
> }
> --
Aside from giving the wrong result (which you found) because of how
the logic for getdate filling in the missing information is specified,
you also can't use localtime like this, because it will clobber the
result buffer that belongs to the last caller of localtime. You'd need
to use localtime_r.
Rich
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.