From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/8538 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: out of range struct tm fields in strftime Date: Mon, 21 Sep 2015 16:28:36 -0400 Message-ID: <20150921202836.GT17773@brightrain.aerifal.cx> References: <20150920124450.GA10551@port70.net> <20150920163629.GL17773@brightrain.aerifal.cx> <20150920164435.GB10551@port70.net> <20150920195450.GD10551@port70.net> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1442867340 1740 80.91.229.3 (21 Sep 2015 20:29:00 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 21 Sep 2015 20:29:00 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-8550-gllmg-musl=m.gmane.org@lists.openwall.com Mon Sep 21 22:28:59 2015 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1Ze7hl-0002F4-Vq for gllmg-musl@m.gmane.org; Mon, 21 Sep 2015 22:28:54 +0200 Original-Received: (qmail 23809 invoked by uid 550); 21 Sep 2015 20:28:51 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 23769 invoked from network); 21 Sep 2015 20:28:49 -0000 Content-Disposition: inline In-Reply-To: <20150920195450.GD10551@port70.net> User-Agent: Mutt/1.5.21 (2010-09-15) Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:8538 Archived-At: On Sun, Sep 20, 2015 at 09:54:50PM +0200, Szabolcs Nagy wrote: > * Szabolcs Nagy [2015-09-20 18:44:35 +0200]: > > * Rich Felker [2015-09-20 12:36:29 -0400]: > > > On Sun, Sep 20, 2015 at 02:44:50PM +0200, Szabolcs Nagy wrote: > > > > out of range tm fields should not be treated as ub > > > > as noted in the thread > > > > http://sourceware.org/ml/libc-alpha/2015-09/msg00546.html > > > > > > > > i have a patch but there might be simpler approaches > > > > > > Wouldn't it be less invasive to just make some small changes like > > > putting a U on some of the constants so that the arithmetic happens as > > > unsigned? > > > > > > > that might be simpler (and probably generates better > > code for div,mod by const) > > > > but for the nl_langinfo item computation the range > > has to be limited properly (tm_wday and tm_mon are > > affected). > > implemented this approach with wday%7U and using some 0U+ > static int week_num(const struct tm *tm) > { > - int val = (tm->tm_yday + 7 - (tm->tm_wday+6)%7) / 7; > + int val = (tm->tm_yday + 7U - (tm->tm_wday+6U)%7) / 7; > /* If 1 Jan is just 1-3 days past Monday, > * the previous week is also in this year. */ > - if ((tm->tm_wday - tm->tm_yday - 2 + 371) % 7 <= 2) > + if ((0U + tm->tm_wday - tm->tm_yday - 2 + 371) % 7 <= 2) > val++; > if (!val) { > val = 52; > /* If 31 December of prev year a Thursday, > * or Friday of a leap year, then the > * prev year has 53 weeks. */ > - int dec31 = (tm->tm_wday - tm->tm_yday - 1 + 7) % 7; > + int dec31 = (0U + tm->tm_wday - tm->tm_yday - 1 + 7) % 7; This is okay but it might (or might not) be less ugly to just reorder the constants to avoid the 0U. Thoughts? > @@ -57,17 +57,17 @@ const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm * > > switch (f) { > case 'a': > - item = ABDAY_1 + tm->tm_wday; > + item = ABDAY_1 + tm->tm_wday%7U; This is going to be a significant code size increase on many archs, and possibly significant performance cost too. I wonder if there's a better way. > @@ -143,10 +143,10 @@ const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm * > width = 1; > goto number; > case 'U': > - val = (tm->tm_yday + 7 - tm->tm_wday) / 7; > + val = (tm->tm_yday + 7U - tm->tm_wday) / 7; These look fine. > @@ -165,7 +165,7 @@ const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm * > val = tm->tm_year % 100; > goto number; > case 'Y': > - val = tm->tm_year + 1900; > + val = tm->tm_year + 1900LL; Also looks ok. Hopefully the compiler does not actually evaluate in 64-bit. Rich