public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: cyg Simple <cygsimple@gmail.com>
To: cygwin@cygwin.com
Subject: Re: FPE in localtime.cc
Date: Tue, 10 Jul 2018 13:35:00 -0000	[thread overview]
Message-ID: <f9d5e7f4-7a33-9868-efaf-986eaa4baa2f@gmail.com> (raw)
In-Reply-To: <20180709190729.GF27673@calimero.vinschen.de>

On 7/9/2018 3:07 PM, Corinna Vinschen wrote:
> On Jul  9 11:16, Brian Inglis wrote:
>> On 2018-07-09 10:49, Corinna Vinschen wrote:
>>> On Jul  9 15:47, Lavrentiev, Anton (NIH/NLM/NCBI) [C] wrote:
>>>> Hello,
>>>>
>>>> the following sample coredumps with FPE at localhost.cc:1962 with the latest snapshot (6/29/2018):
>> ...
>>> You can simplify your testcase by not calling any time functions:
>>>
>>>   #define _GNU_SOURCE
>>>   #include <fenv.h>
>>>   #include <stdio.h>
>>>   #include <stdlib.h>
>>>
>>>   #define SECSPERDAY 86400
>>>
>>>   int main(int argc, char **argv)
>>>   {
>>>     feenableexcept(FE_ALL_EXCEPT);
>>>     long tdays = argc > 1 ? strtol (argv[1], NULL, 10) : 189;
>>>     long seconds = tdays * SECSPERDAY + 0.5;
>>>     printf ("%ld\n", seconds);
>>>   }
>>>
>>> This generates a SIGFPE on Linux as well.
>>>
>>> The line computing seconds is the same line as used by the localtime
>>> function.  Cygwin shares the entire localtime code with the various
>>> BSDs, so I guess they would have the same problem.
>> What is that line meant to do? Am I missing something?
>> It should be the equivalent of (tdays*SECSPERDAY*2 + 1)/2!
>> It converts an integer value to double, adds 1/2, and throws it away on
>> conversion back, unless the intermediate has insufficient mantissa bits, in
>> which case, it loses the low bits?
> 
> You may want to ask the original author why he used FP arithmetic in
> this place.  Maybe it's a way to avoid integer overflow.  I'm reluctant
> to change this given that this code is still used in BSD as well.
> 

I don't see a fetestexcept() being used.  Setting all the bits is
definitely going to raise an exception.

>>> Bottom line is, don't bulk enable FP exceptions, but only if you really
>>> need it for certain parts of your code.  Don't expect library functions
>>> to be SIGFPE clean under all circumstances.
>>
>> Maybe selectively enable specific FPEs to check for where needed.
>> Or be careful what you wish for, as you just might get a lot more than you
>> bargained for ;^>
> 
> That's what I meant.

Yes, see:
https://en.cppreference.com/w/c/numeric/fenv/FE_exceptions

-- 
cyg Simple

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

  reply	other threads:[~2018-07-10 13:35 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-09 15:47 Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2018-07-09 16:49 ` Corinna Vinschen
2018-07-09 17:17   ` Brian Inglis
2018-07-09 19:07     ` Corinna Vinschen
2018-07-10 13:35       ` cyg Simple [this message]
2018-07-11  1:30         ` Brian Inglis

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=f9d5e7f4-7a33-9868-efaf-986eaa4baa2f@gmail.com \
    --to=cygsimple@gmail.com \
    --cc=cygwin@cygwin.com \
    /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).