mailing list of musl libc
 help / color / mirror / code / Atom feed
* Different behavior (strtod) between musl and glibc
@ 2017-06-13 14:43 Xiaowei Zhan
  2017-06-13 14:53 ` Khem Raj
  2017-06-14 13:44 ` Florian Weimer
  0 siblings, 2 replies; 4+ messages in thread
From: Xiaowei Zhan @ 2017-06-13 14:43 UTC (permalink / raw)
  To: musl

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

Hello,

I notice that when pass a non-numeric char to strtod, musl will set errno
to non-zero, but glibc will set errno to zero.
I am curious why this difference exists, and whether it is necessary to
make strtod in musl behave similarly to glibc.

Here is the toy program:


#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv) {
  errno = 0;
  char input[] = "NA023";
  strtod(input, NULL);
  printf("errno = %d\n", errno);
  perror("strtod");

  return 0;
}

Output (musl):

bash-4.3# ./main
errno = 22
strtod: Invalid argument

Output (glibc, Ubuntu 16.04):
> ./main
errno = 0
strtod: Success



Best,
Xiaowei

[-- Attachment #2: Type: text/html, Size: 1199 bytes --]

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

* Re: Different behavior (strtod) between musl and glibc
  2017-06-13 14:43 Different behavior (strtod) between musl and glibc Xiaowei Zhan
@ 2017-06-13 14:53 ` Khem Raj
  2017-06-14 13:44 ` Florian Weimer
  1 sibling, 0 replies; 4+ messages in thread
From: Khem Raj @ 2017-06-13 14:53 UTC (permalink / raw)
  To: musl

On Tue, Jun 13, 2017 at 7:43 AM, Xiaowei Zhan <zhanxw@gmail.com> wrote:
> Hello,
>
> I notice that when pass a non-numeric char to strtod, musl will set errno to
> non-zero, but glibc will set errno to zero.

Musl is detecting that it can not perform the conversion due to invalid input.
It seems that musl is conforming to posix definition here
http://pubs.opengroup.org/onlinepubs/009695399/functions/strtod.html

> I am curious why this difference exists, and whether it is necessary to make
> strtod in musl behave similarly to glibc.
>
> Here is the toy program:
>
>
> #include <errno.h>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(int argc, char** argv) {
>   errno = 0;
>   char input[] = "NA023";
>   strtod(input, NULL);
>   printf("errno = %d\n", errno);
>   perror("strtod");
>
>   return 0;
> }
>
> Output (musl):
>
> bash-4.3# ./main
> errno = 22
> strtod: Invalid argument
>
> Output (glibc, Ubuntu 16.04):
>> ./main
> errno = 0
> strtod: Success
>
>
>
> Best,
> Xiaowei
>
>


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

* Re: Different behavior (strtod) between musl and glibc
  2017-06-13 14:43 Different behavior (strtod) between musl and glibc Xiaowei Zhan
  2017-06-13 14:53 ` Khem Raj
@ 2017-06-14 13:44 ` Florian Weimer
  2017-06-14 17:26   ` Rich Felker
  1 sibling, 1 reply; 4+ messages in thread
From: Florian Weimer @ 2017-06-14 13:44 UTC (permalink / raw)
  To: Xiaowei Zhan; +Cc: musl

Xiaowei Zhan <zhanxw@gmail.com> writes:

> I notice that when pass a non-numeric char to strtod, musl will set
> errno to non-zero, but glibc will set errno to zero.  I am curious why
> this difference exists, and whether it is necessary to make strtod in
> musl behave similarly to glibc.

I think glibc leaves errno at zero; it does not set it.  For input which
cannot be converted, this seems to be the behavior mandated by C11.
POSIX describes the EINVAL value as an extension to the C standard.
glibc does not appear to implement this extension.

So both behaviors are correct.

Florian


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

* Re: Different behavior (strtod) between musl and glibc
  2017-06-14 13:44 ` Florian Weimer
@ 2017-06-14 17:26   ` Rich Felker
  0 siblings, 0 replies; 4+ messages in thread
From: Rich Felker @ 2017-06-14 17:26 UTC (permalink / raw)
  To: Florian Weimer; +Cc: Xiaowei Zhan, musl

On Wed, Jun 14, 2017 at 03:44:34PM +0200, Florian Weimer wrote:
> Xiaowei Zhan <zhanxw@gmail.com> writes:
> 
> > I notice that when pass a non-numeric char to strtod, musl will set
> > errno to non-zero, but glibc will set errno to zero.  I am curious why
> > this difference exists, and whether it is necessary to make strtod in
> > musl behave similarly to glibc.
> 
> I think glibc leaves errno at zero; it does not set it.  For input which
> cannot be converted, this seems to be the behavior mandated by C11.
> POSIX describes the EINVAL value as an extension to the C standard.
> glibc does not appear to implement this extension.
> 
> So both behaviors are correct.

CX is a normative requirement for POSIX conformance; it indicates a
requirement beyond (but not conflicting with) the C requirements for
an interface defined by C. However, it's a "may fail", not a "shall
fail", so the error is optional, and thus you're right that both musl
and glibc are correct on the matter.

I think you're also correct that glibc leaves errno alone in this
case. Setting it to 0 would be non-conforming. The implementation is
never permitted to set errno to 0 except at initial program entry.

Rich


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

end of thread, other threads:[~2017-06-14 17:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-13 14:43 Different behavior (strtod) between musl and glibc Xiaowei Zhan
2017-06-13 14:53 ` Khem Raj
2017-06-14 13:44 ` Florian Weimer
2017-06-14 17:26   ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).