mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Alexander Weps <exander77@pm.me>
To: Markus Wichmann <nullplan@gmx.net>
Cc: musl@lists.openwall.com
Subject: Re: [musl] Broken mktime calculations when crossing DST boundary
Date: Sat, 23 Mar 2024 12:01:31 +0000	[thread overview]
Message-ID: <gwBSRQ41xEoJoCSqYJObIEZeRGs4tVuNFIsLjNQjNs60gtYvSzEVBeZEQxDV9dzlELvDp692HXWIf0QfWwdDx4lZkmeabNBdQ1GhIqHdOBA=@pm.me> (raw)
In-Reply-To: <Yf9tAroDB-Og6dyI5Ccs_aEwhmPuBXvmzllVAtyGCZSlAij_zgUHJyojz0fF-c9kudgI5mSVbFv6iwiPTeywz0lRgtUnLkoQJGw4It22YLg=@pm.me>

This is how it should work as far as I am concerned. After manipulating the date, the tm_isdst is set to -1.

From https://cplusplus.com/reference/ctime/tm/:
The Daylight Saving Time flag (tm_isdst) is greater than zero if Daylight Saving Time is in effect, zero if Daylight Saving Time is not in effect, and less than zero if the information is not available.

And the date should be correctly set as DST or STD in mktime.

I have a date. I make change in fields, I set tm_isdst = -1, I call mktime.

I see only on place where tm_isdst is checked and that's:
time_t mktime(struct tm *tm)
{
    struct tm new;
    long opp;
    long long t = __tm_to_secs(tm);

    __secs_to_zone(t, 1, &new.tm_isdst, &new.__tm_gmtoff, &opp, &new.__tm_zone);

    if (tm->tm_isdst>=0 && new.tm_isdst!=tm->tm_isdst)
        t -= opp - new.__tm_gmtoff;

    t -= new.__tm_gmtoff;
    if ((time_t)t != t) goto error;

    __secs_to_zone(t, 0, &new.tm_isdst, &new.__tm_gmtoff, &opp, &new.__tm_zone);

    if (__secs_to_tm(t + new.__tm_gmtoff, &new) < 0) goto error;

    *tm = new;
    return t;

error:
    errno = EOVERFLOW;
    return -1;
}

So tm->tm_isdst>=0 seems to be the cause here.

AW

On Saturday, March 23rd, 2024 at 13:00, Alexander Weps <exander77@pm.me> wrote:

> > You don't need to set yday or the others before calling mktime().
>
>
> I thought that too, but wanted to test it on exactly the same struct tm. No change in behavior.
>
> So I have found a minimal example:
>
> void test()
> {
> time_t t;
> struct tm tm = {0};
> char buf[64];
>
> tm.tm_year = 2024 - 1900;
> tm.tm_mon = 3 - 1;
> tm.tm_mday = 31;
> tm.tm_hour = 1;
> tm.tm_min = 59;
> tm.tm_sec = 2;
> print_tm(&tm);
>
> mktime(&tm);
> strftime(buf, sizeof buf, "%F %T %Z", &tm);
> printf("before: %s\n", buf);
> tm.tm_isdst = -1; // This is the cause.
> tm.tm_min += 1;
> mktime(&tm);
> strftime(buf, sizeof buf, "%F %T %Z", &tm);
> printf("after: %s\n", buf);
> }
>
> Setting tm_isdst to -1 after first mktime and before second mktime causes the issue.
>
> AW
>
>
>
>
> On Saturday, March 23rd, 2024 at 11:38, Markus Wichmann nullplan@gmx.net wrote:
>
> > Am Sat, Mar 23, 2024 at 09:26:00AM +0000 schrieb Alexander Weps:
> >
> > > This works for me as well even after changes to match struct tm in my
> > > app (setting tm_yday, __tm_gtmoff a __tm_zone):
> >
> > You don't need to set yday or the others before calling mktime().
> > mktime() is defined to only use year, mon, mday, hour, min, sec, and
> > isdst as input, normalize them, and calculate the others (and also the
> > UNIX time stamp).
> >
> > > Any idea how could a previous calculation mess with musl internals so
> > > it would start producing bad results? Because otherwise, I don't see
> > > how this could be happening if you completely extract it out of the
> > > code and it works.
> >
> > The only thing that means is that the isolated code works, and the bug
> > is elsewhere. I'm sorry, but you are going to have to debug this
> > yourself. There may be some static memory getting corrupted (e.g. the TZ
> > and rule caches), but honestly this is just speculation.
> >
> > > And when I compile under glibc, everything is fine.
> >
> > That tends to indicate some undefined behavior. Not that that helps you
> > find the reason. You are going to have to debug it. Finding a minimum
> > reproducer may help in that, or you directly apply liberal amounts of
> > gdb.
> >
> > You seem to have dropped the list from CC, so I'm adding it back.
> >
> > Ciao,
> > Markus

  parent reply	other threads:[~2024-03-23 12:01 UTC|newest]

Thread overview: 76+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-22 19:56 Alexander Weps
2024-03-23  6:41 ` Markus Wichmann
     [not found]   ` <528SeRFaPfDw7fA4kqKDlio1U4RB_t9nmUemPcWw9_t1e2hBDpXYFmOqxAC37szgYvAVtmTuXWsmT64SSN3cSQFVdrQqXUAgkdTMPZQ0bg0=@pm.me>
2024-03-23 10:38     ` Markus Wichmann
2024-03-23 11:59       ` Alexander Weps
2024-03-23 12:00         ` Alexander Weps
2024-03-23 12:31           ` Rich Felker
2024-03-23 13:49             ` Alexander Weps
2024-03-23 15:31               ` Rich Felker
2024-03-23 16:54                 ` Alexander Weps
2024-03-23 18:57                   ` Alexander Weps
2024-03-23 19:33                     ` Alexander Weps
2024-03-23 20:18                     ` Rich Felker
2024-03-23 20:40                       ` Alexander Weps
2024-03-24  0:36                         ` Eric Pruitt
2024-03-24  2:04                         ` Rich Felker
2024-03-24  3:32                           ` Daniel Gutson
2024-03-24 11:05                             ` Alexander Weps
2024-03-24 13:24                               ` Alexander Weps
2024-03-23 12:01         ` Alexander Weps [this message]
2024-03-24 13:36 Alexander Weps
2024-03-24 16:59 ` Alexander Weps
2024-03-24 17:04 ` Rich Felker
2024-03-24 17:12   ` Alexander Weps
2024-03-24 18:00     ` Alexander Weps
2024-03-24 18:02     ` Rich Felker
2024-03-24 18:16       ` Alexander Weps
2024-03-24 18:24         ` Rich Felker
2024-03-24 18:36           ` Alexander Weps
2024-03-24 19:01             ` Joakim Sindholt
2024-03-24 19:05               ` Alexander Weps
2024-03-24 19:06             ` Alexander Weps
2024-03-24 19:13               ` Alexander Weps
2024-03-24 19:13               ` Alexander Weps
2024-03-24 19:22             ` Rich Felker
2024-03-24 19:57               ` Alexander Weps
2024-03-24 20:22                 ` Rich Felker
2024-03-24 20:50                   ` Alexander Weps
2024-03-24 21:43                     ` Alexander Weps
2024-03-24 23:51                 ` Thorsten Glaser
2024-03-25  0:36                   ` Alexander Weps
2024-03-25 11:52                     ` Alexander Weps
2024-03-25 12:21                       ` Rich Felker
2024-03-25 12:55                         ` Alexander Weps
2024-03-25 13:08                           ` Rich Felker
2024-03-25 13:13                             ` Alexander Weps
2024-03-25 13:13                           ` Rich Felker
2024-03-25 13:24                             ` Alexander Weps
2024-03-25 13:42                               ` Rich Felker
2024-03-25 13:48                                 ` Alexander Weps
2024-03-25 13:50                                   ` Alexander Weps
2024-03-25 18:02                                 ` Rich Felker
2024-03-25 18:28                                   ` Alexander Weps
2024-03-25 18:53                                     ` Rich Felker
2024-03-25 18:57                                       ` Alexander Weps
2024-03-25 19:38                                         ` Rich Felker
2024-03-25 19:47                                           ` Rich Felker
2024-03-25 20:05                                             ` Alexander Weps
2024-03-25 20:12                                               ` Alexander Weps
2024-03-25 20:00                                           ` Alexander Weps
2024-03-25 20:23                                             ` Rich Felker
2024-03-25 20:31                                               ` Rich Felker
2024-03-25 23:19                                     ` Thorsten Glaser
2024-03-25 23:16                                 ` Thorsten Glaser
2024-03-25 13:44                               ` Alexander Weps
2024-03-25 22:40                           ` Thorsten Glaser
2024-03-25 22:59                             ` Alexander Weps
2024-03-25 23:34                               ` Thorsten Glaser
2024-03-26 12:45                                 ` Alexander Weps
2024-03-26 21:59                                   ` Thorsten Glaser
2024-03-27  0:14                                     ` Alexander Weps
2024-03-27  0:38                                       ` Alexander Weps
2024-03-27  1:35                                       ` Thorsten Glaser
2024-03-27  2:45                                         ` Alexander Weps
2024-03-27  4:42                                           ` Thorsten Glaser
2024-03-26 18:56                                 ` Alexander Weps
2024-03-25 23:13                             ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='gwBSRQ41xEoJoCSqYJObIEZeRGs4tVuNFIsLjNQjNs60gtYvSzEVBeZEQxDV9dzlELvDp692HXWIf0QfWwdDx4lZkmeabNBdQ1GhIqHdOBA=@pm.me' \
    --to=exander77@pm.me \
    --cc=musl@lists.openwall.com \
    --cc=nullplan@gmx.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).