Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <7RSpN12xzL2-U16pTCJqpy9JLwjl4wbnT-YbJ8jbFGQRp7FfUposZOQ4RIFQw2vTrWDKkYhTPbjdPCmZH-_uCLQUU2Nubvp5wkaGKqai_yo=@pm.me>
Date: Mon, 25 Mar 2024 13:44:57 +0000
From: Alexander Weps <exander77@...me>
To: Rich Felker <dalias@...c.org>
Cc: musl@...ts.openwall.com
Subject: Re: Broken mktime calculations when crossing DST boundary

>
> What did you do that got glibc to output 2012-01-01? I guess you wrote
> code to do some wacky arithmetic after the original code you already
> had, rather than changing the code to start with 2011-12-31 as I
> suggested to get a look at what's happening.
>

To address this. I would appreciate you not to lie or make some nonsense up.

There was only change related to calculations in the code and it was a change you requested:
    tm.tm_mday = 31;

Complete code to run:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void test10()
{
    time_t t = 0;
    struct tm tm = {0};
    char buf[64];

    tm.tm_year = 2011 - 1900;
    tm.tm_mon = 12 - 1;
    tm.tm_mday = 31; // <-- here is the change
    tm.tm_hour = 0;
    tm.tm_min = 0;
    tm.tm_sec = 0;
    tm.tm_isdst = 0;

    strftime(buf, sizeof buf, "%F %T %Z", &tm);
    printf("before: %s %ld\n", buf, t);

    t = mktime(&tm);

    strftime(buf, sizeof buf, "%F %T %Z", &tm);
    printf("after1: %s %ld\n", buf, t);

    tm.tm_mday -= 1;
    t = mktime(&tm);

    strftime(buf, sizeof buf, "%F %T %Z", &tm);
    printf("after2: %s %ld\n", buf, t);

    tm.tm_mday += 1;
    t = mktime(&tm);

    strftime(buf, sizeof buf, "%F %T %Z", &tm);
    printf("after3: %s %ld\n", buf, t);
}

int main(int argc, char *argv[])
{
    test10();
    return 0;
}

$  musl-gcc foo.c -o foo && TZ=Pacific/Apia ./foo
before: 2011-12-31 00:00:00  0 <-- before first mktime call to verify
after1: 2011-12-31 01:00:00 +14 1325242800 <-- mktime to normalize
after2: 2011-12-29 01:00:00 -10 1325156400 <-- -day & mktime
after3: 2011-12-29 01:00:00 -10 1325156400 <-- +day & mktime

$  gcc foo.c -o foo && TZ=Pacific/Apia ./foo
before: 2011-12-31 00:00:00 +13 0 <-- before first mktime call to verify
after1: 2012-01-01 01:00:00 +14 1325329200 <-- mktime to normalize
after2: 2011-12-31 01:00:00 +14 1325242800 <-- -day & mktime
after3: 2012-01-01 01:00:00 +14 1325329200 <-- +day & mktime

So this is a bug in struct tm interpretation.

AW

On Monday, March 25th, 2024 at 14:24, Alexander Weps <exander77@...me> wrote:

> See below.
>
> AW
>
>
>
>
> On Monday, March 25th, 2024 at 14:13, Rich Felker dalias@...c.org wrote:
>
> > On Mon, Mar 25, 2024 at 12:55:28PM +0000, Alexander Weps wrote:
> >
> > > > If you take your test program and switch it to initialize with
> > > > tm_mday=31, then do -=1 instead of +=1, you'll find that it gives
> > > > 2011-12-29 01:00:00 -10 as well, only now it seems like the correct,
> > > > expected thing to happen. Any change to "fix" the case you're
> > > > complaining about would necessarily break this case.
> > >
> > > So (- day, +day):
> > >
> > > Musl:
> > > 2011-12-31 01:00:00 +14
> > > 2011-12-29 01:00:00 -10
> > > 2011-12-29 01:00:00 -10
> > >
> > > Glibc:
> > > 2012-01-01 01:00:00 +14
> > > 2011-12-31 01:00:00 +14
> > > 2012-01-01 01:00:00 +14
> > >
> > > Seems like musl doesn't even interpret the initial struct tm
> > > correctly in that case. It is off by day.
> > >
> > > Because December only had 30 days, 31s day after normalization is
> > > January 1st.
> >
> > This is nonsense. December has a day 31, which you can clearly see
> > from the glibc output. For this particular year in this zone, with the
> > zone rule change, there are "only 30 days" in December, but they are
> > numbered 1-29 and 31, not 1-30.
>
>
> You confuse day of month which is represented in tm_mday with calendar day that is interpreted by strftime.
>
> You said to set tm_mday = 31, which would be January 1st after normalization.
> December 31s is 30th day of month represented as tm_mday = 30.
>
> > What did you do that got glibc to output 2012-01-01? I guess you wrote
> > code to do some wacky arithmetic after the original code you already
> > had, rather than changing the code to start with 2011-12-31 as I
> > suggested to get a look at what's happening.
> >
> > > > In any case, the core issue you're hitting here is that time zones are
> > > > HARD to work with and that there is inherent complexity that libc
> > > > cannot save you from. You only got lucky that what you were trying to
> > > > do "worked" with glibc because you were iterating days forward; if you
> > > > were doing reverse, it would break exactly the same way.
> > >
> > > I am not really commenting on this, until you sort out the above
> > > inconsistencies.
> >
> > I already have but you refuse to look.
>
>
> It was addressed, do didn't scroll at the end of the e-mail.
>
> > 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.