mailing list of musl libc
 help / color / mirror / code / Atom feed
* Edge case: strtol with base==1
@ 2017-01-01 10:45 Ryan Prichard
  2017-01-02  2:04 ` Rich Felker
  0 siblings, 1 reply; 3+ messages in thread
From: Ryan Prichard @ 2017-01-01 10:45 UTC (permalink / raw)
  To: musl

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

Hi,

I noticed that musl's strtol function allows a base of 1 by parsing strings
of only zeros.  With all other bases less than 0 or greater than 36, strtol
fails and sets errno to EINVAL.

Is this an oversight?

Not that it especially *matters*, but my guess is that the behavior isn't
POSIX conforming:
 - POSIX defines valid "subject sequences" for bases of 0 and 2-36.
 - "In other than the C or POSIX locale, additional locale-specific subject
sequence forms may be accepted."

In the C locale, musl is accepting a subject sequence of all zeroes, which
POSIX doesn't define for a base of 1.

http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtol.html

e.g.:

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

int main() {
    errno = 0;
    char *endptr = "unset";
    long val = strtol("  0001", &endptr, 1);
    printf("errno=[%s], val=%ld, endptr=[%s]\n",
        strerror(errno), val, endptr ? endptr : "(null)");
    return 0;
}

$ gcc test.c && ./a.out
errno=[Invalid argument], val=0, endptr=[unset]

$ musl-gcc test.c && ./a.out
errno=[No error information], val=0, endptr=[1]

-Ryan

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

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

* Re: Edge case: strtol with base==1
  2017-01-01 10:45 Edge case: strtol with base==1 Ryan Prichard
@ 2017-01-02  2:04 ` Rich Felker
  2017-01-05  0:52   ` Rich Felker
  0 siblings, 1 reply; 3+ messages in thread
From: Rich Felker @ 2017-01-02  2:04 UTC (permalink / raw)
  To: musl

On Sun, Jan 01, 2017 at 04:45:19AM -0600, Ryan Prichard wrote:
> Hi,
> 
> I noticed that musl's strtol function allows a base of 1 by parsing strings
> of only zeros.  With all other bases less than 0 or greater than 36, strtol
> fails and sets errno to EINVAL.
> 
> Is this an oversight?
> 
> Not that it especially *matters*, but my guess is that the behavior isn't
> POSIX conforming:
>  - POSIX defines valid "subject sequences" for bases of 0 and 2-36.
>  - "In other than the C or POSIX locale, additional locale-specific subject
> sequence forms may be accepted."
> 
> In the C locale, musl is accepting a subject sequence of all zeroes, which
> POSIX doesn't define for a base of 1.
> 
> http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtol.html

Indeed, I think you're right. ISO C does not specify the behavior when
the base is invalid (it's UB), but POSIX does impose an error
requirement. It's not clear that it requires other bases to be
unsupported, just that an error be produced if the base is
unsupported, but I think the right behavior is not to support a
nonsense "base 1".

Rich


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

* Re: Edge case: strtol with base==1
  2017-01-02  2:04 ` Rich Felker
@ 2017-01-05  0:52   ` Rich Felker
  0 siblings, 0 replies; 3+ messages in thread
From: Rich Felker @ 2017-01-05  0:52 UTC (permalink / raw)
  To: musl

On Sun, Jan 01, 2017 at 09:04:48PM -0500, Rich Felker wrote:
> On Sun, Jan 01, 2017 at 04:45:19AM -0600, Ryan Prichard wrote:
> > Hi,
> > 
> > I noticed that musl's strtol function allows a base of 1 by parsing strings
> > of only zeros.  With all other bases less than 0 or greater than 36, strtol
> > fails and sets errno to EINVAL.
> > 
> > Is this an oversight?
> > 
> > Not that it especially *matters*, but my guess is that the behavior isn't
> > POSIX conforming:
> >  - POSIX defines valid "subject sequences" for bases of 0 and 2-36.
> >  - "In other than the C or POSIX locale, additional locale-specific subject
> > sequence forms may be accepted."
> > 
> > In the C locale, musl is accepting a subject sequence of all zeroes, which
> > POSIX doesn't define for a base of 1.
> > 
> > http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtol.html
> 
> Indeed, I think you're right. ISO C does not specify the behavior when
> the base is invalid (it's UB), but POSIX does impose an error
> requirement. It's not clear that it requires other bases to be
> unsupported, just that an error be produced if the base is
> unsupported, but I think the right behavior is not to support a
> nonsense "base 1".

Committing a fix. Thanks.

Rich


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

end of thread, other threads:[~2017-01-05  0:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-01 10:45 Edge case: strtol with base==1 Ryan Prichard
2017-01-02  2:04 ` Rich Felker
2017-01-05  0:52   ` 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).