FPE in localtime.cc

Corinna Vinschen corinna-cygwin@cygwin.com
Mon Jul 9 19:07:00 GMT 2018


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.

> > 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.


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://cygwin.com/pipermail/cygwin/attachments/20180709/7ab30deb/attachment.sig>


More information about the Cygwin mailing list