public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Richard Damon <Richard@Damon-Family.org>
To: newlib@sourceware.org
Subject: Re: Bug in UTC -> localtime conversion after 2038
Date: Wed, 4 Nov 2020 07:59:26 -0500	[thread overview]
Message-ID: <2ff07bdd-d9d2-b761-023e-c41f3ee1905f@Damon-Family.org> (raw)
In-Reply-To: <5d87b235-f246-1bab-f225-484c6fd93b4d@ubero.nl>

On 11/4/20 6:36 AM, Udo de Boer wrote:
> Hi,
>
> UTC time to local timezone conversion did not work for years after
> 2038. Daylight saving in the timezone was not recognized. Tested on
> 32bit xtensa architecture with GCC 8.4 (esp32) and with 64bit time_t
> enabled.
>
> The struct tm is filled correct. So year, day etc are all filled with
> the correct values. But the tm.tm_isdst is 0 and no daylight saving is
> applied.
>
> It was caused by the following calculation in time/tzcalc_limits.c at
> line 68
>
>        /* store the change-over time in GMT form by adding offset */
>        tz->__tzrule[i].change = days * SECSPERDAY +
>        tz->__tzrule[i].s + tz->__tzrule[i].offset;
>
> Here tz->__tzrule[i].change is a time_t (64bit). For some reason the
> compiler does the calculation in 32 bit. All variables used in the
> calculation are 32bit.
>
> I solved this locally by changing the lines to.
>
>        /* store the change-over time in GMT form by adding offset */
>        tz->__tzrule[i].change = (time_t)days * SECSPERDAY +
>        tz->__tzrule[i].s + tz->__tzrule[i].offset;
>
> Adding the cast time_t should not cause a problem when time_t is still
> 32bit.
>
> I don't know if this is correct solution. I also only needed this
> function. So more bugs like this could be hidden away in the time code.
>
C arithmetic is defined based on the size of the operands, not the
destination, so math on 32 bit quantities is done at 32 bits (unless int
is bigger, as int is the smallest size used).

The casting of one of the operands to the bigger size is the right solution.

-- 
Richard Damon


  reply	other threads:[~2020-11-04 12:59 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-04 11:36 Udo de Boer
2020-11-04 12:59 ` Richard Damon [this message]
2020-11-04 16:20 ` Ivan Grokhotkov
2020-11-04 18:37   ` Jeff Johnston

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=2ff07bdd-d9d2-b761-023e-c41f3ee1905f@Damon-Family.org \
    --to=richard@damon-family.org \
    --cc=newlib@sourceware.org \
    /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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).