public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* FPE in localtime.cc
@ 2018-07-09 15:47 Lavrentiev, Anton (NIH/NLM/NCBI) [C]
  2018-07-09 16:49 ` Corinna Vinschen
  0 siblings, 1 reply; 6+ messages in thread
From: Lavrentiev, Anton (NIH/NLM/NCBI) [C] @ 2018-07-09 15:47 UTC (permalink / raw)
  To: 'cygwin@cygwin.com'

Hello,

the following sample coredumps with FPE at localhost.cc:1962 with the latest snapshot (6/29/2018):

#define _GNU_SOURCE
#include <fenv.h>
#include <time.h>
#include <stdio.h>

static time_t s_Time;

static void fun(void)
{
  char buf[40];
  strftime(buf, sizeof(buf), "%m/%d/%y %H:%M:%S", localtime(&s_Time));
  printf("%s\n", buf);
}

int main()
{
  feenableexcept(FE_ALL_EXCEPT);
  time(&s_Time);
  fun();
}

$ cat a.exe.stackdump
Exception: STATUS_FLOAT_INEXACT_RESULT at rip=001800BBA24
rax=000000000000016D rbx=000000005B43498E rcx=00000000000001EC
rdx=1845C8A0CE512957 rsi=0000000051EB851F rdi=2CC3D8D4A245F203
r8 =0000000000F92B80 r9 =00000000FFFFFFED r10=00000000000007E2
r11=000000000000000C r12=00000000FFFFFFFF r13=0000000080000000
r14=00000000000000BD r15=00000000000007E2
rbp=000000007FFFFFFF rsp=00000000FFFFCA70
program=C:\Cygwin64\...\a.exe, pid 14232, thread main
cs=0033 ds=002B es=002B fs=0053 gs=002B ss=002B

Removing the first line of the main() function lets the program run successfully.
The same code runs fine unmodified on Linux.

$ gcc -Wall sample.c -lm
$ ./a.out
07/09/18 11:44:26

Any ideas?

Thanks,
Anton Lavrentiev


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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: FPE in localtime.cc
  2018-07-09 15:47 FPE in localtime.cc Lavrentiev, Anton (NIH/NLM/NCBI) [C]
@ 2018-07-09 16:49 ` Corinna Vinschen
  2018-07-09 17:17   ` Brian Inglis
  0 siblings, 1 reply; 6+ messages in thread
From: Corinna Vinschen @ 2018-07-09 16:49 UTC (permalink / raw)
  To: cygwin

[-- Attachment #1: Type: text/plain, Size: 2333 bytes --]

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):
> 
> #define _GNU_SOURCE
> #include <fenv.h>
> #include <time.h>
> #include <stdio.h>
> 
> static time_t s_Time;
> 
> static void fun(void)
> {
>   char buf[40];
>   strftime(buf, sizeof(buf), "%m/%d/%y %H:%M:%S", localtime(&s_Time));
>   printf("%s\n", buf);
> }
> 
> int main()
> {
>   feenableexcept(FE_ALL_EXCEPT);
>   time(&s_Time);
>   fun();
> }
> 
> $ cat a.exe.stackdump
> Exception: STATUS_FLOAT_INEXACT_RESULT at rip=001800BBA24
> rax=000000000000016D rbx=000000005B43498E rcx=00000000000001EC
> rdx=1845C8A0CE512957 rsi=0000000051EB851F rdi=2CC3D8D4A245F203
> r8 =0000000000F92B80 r9 =00000000FFFFFFED r10=00000000000007E2
> r11=000000000000000C r12=00000000FFFFFFFF r13=0000000080000000
> r14=00000000000000BD r15=00000000000007E2
> rbp=000000007FFFFFFF rsp=00000000FFFFCA70
> program=C:\Cygwin64\...\a.exe, pid 14232, thread main
> cs=0033 ds=002B es=002B fs=0053 gs=002B ss=002B
> 
> Removing the first line of the main() function lets the program run successfully.
> The same code runs fine unmodified on Linux.
> 
> $ gcc -Wall sample.c -lm
> $ ./a.out
> 07/09/18 11:44:26
> 
> Any ideas?

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.

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.


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: FPE in localtime.cc
  2018-07-09 16:49 ` Corinna Vinschen
@ 2018-07-09 17:17   ` Brian Inglis
  2018-07-09 19:07     ` Corinna Vinschen
  0 siblings, 1 reply; 6+ messages in thread
From: Brian Inglis @ 2018-07-09 17:17 UTC (permalink / raw)
  To: cygwin

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?

> 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 ;^>

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: FPE in localtime.cc
  2018-07-09 17:17   ` Brian Inglis
@ 2018-07-09 19:07     ` Corinna Vinschen
  2018-07-10 13:35       ` cyg Simple
  0 siblings, 1 reply; 6+ messages in thread
From: Corinna Vinschen @ 2018-07-09 19:07 UTC (permalink / raw)
  To: cygwin

[-- Attachment #1: Type: text/plain, Size: 2109 bytes --]

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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: FPE in localtime.cc
  2018-07-09 19:07     ` Corinna Vinschen
@ 2018-07-10 13:35       ` cyg Simple
  2018-07-11  1:30         ` Brian Inglis
  0 siblings, 1 reply; 6+ messages in thread
From: cyg Simple @ 2018-07-10 13:35 UTC (permalink / raw)
  To: cygwin

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: FPE in localtime.cc
  2018-07-10 13:35       ` cyg Simple
@ 2018-07-11  1:30         ` Brian Inglis
  0 siblings, 0 replies; 6+ messages in thread
From: Brian Inglis @ 2018-07-11  1:30 UTC (permalink / raw)
  To: cygwin

On 2018-07-10 07:35, cyg Simple wrote:
> 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:
>>>>> 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

Nasty: I didn't realize decimal fraction to binary conversions would raise
FE_INEXACT!

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2018-07-11  1:30 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-09 15:47 FPE in localtime.cc 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
2018-07-11  1:30         ` Brian Inglis

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