Re: Implementation errors in strtol()

From: Joerg Wunsch <freebsd-current_at_uriah.heep.sax.de>
Date: Fri, 21 Jan 2005 15:44:28 +0100
As Joerg Wunsch wrote:

> As a demonstration, consider this test program:
...
> Below are the results for Solaris 8, FreeBSD 5, Linux 2.x, and HP-UX
> 10.20.
...

> It's quite obvious that any other system differs from FreeBSD here.
> (OK, HP-UX doesn't throw EINVAL at all, even for clearly
> inconvertible strings.  But then, it's a pretty old system, more
> than ten years.)

Btw., I have re-checked HP-UX once more.  They do set errno to EINVAL
in the case of an unacceptable value for base, as required by
Posix/SUSP.  They only never set it to EINVAL for a conversion error.
So they are *fully* compliant with Posix/SUSP.  Hmm, maybe not.  For
"0x", they do not set endptr to point to the `x', so they're still in
violation.  Seems they are using the old BSD code which had the same
bug.  But that's another matter.

Btw., Solaris never sets errno to EINVAL except for the unacceptable
base case (where SUSP requires it).

This is the best demonstration that the entire ``may set to EINVAL''
thing in SUSP is pointless crap, sorry.  As no application developer
can rely on it, they could as well have left this option completely
out.

If someone wants to use strot<something>() to verify their input data,
they'd be best to follow the advise from the FreeBSD manpage:

``(Thus, if *nptr is not `\0' but **endptr is `\0' on return, the
entire string was valid.)''

This is the only reliable method.  (OK, if you care, you can then
check for errno being set to ERANGE in order to detect an
under-/overflow situation.)

As Andrey Chernov wrote:

> This is just your speculation about what "expected form" is. But
> POSIX clearly says, what expected form is:

> "If the value of base is 0, the expected form of the subject
> sequence is ^^^^^^^^^^^^^^^^^^^ that of a decimal constant, octal
> constant, or hexadecimal constant, any of which may be preceded by a
> .+. or .-. sign."

> No CONSTANT found in just +- case.

OK, I agree on this.

I still find the wording of the standard ambiguous, as some forms of
conversion errors (the empty string) are explicitly mentioned, while
other forms (a single sign) have to be derived, but I agree.  It of
course makes most sense that way anyway, I've only been surprised
about the mentioned ambiguity in the standard.

> > Is not SUS v3 == "POSIX" 2001 edition (IEEE Std 1003.1-2001)?

> Yes, with two technical corrigendum, as I read their announce. But
> Joerg mention older SUSv2 and I mean it too.

I didn't have SUSPv3 handy, so I referred to v2 (where I have been
registering years ago).  Thanks for the v3 pointer, that might be
handy as well.

The question about whether the C standard allows setting errno to
EINVAL was merely out of curiosity.  I still think that the C standard
forbids it, but I do find that stupid as well, and never meant to
change that part of the FreeBSD behaviour.


I agree that fixing the 0x problem would fix the part were FreeBSD
currently violates the standard.  However, as the majority of other
SUSP compliant systems seem to prefer to not set errno in the empty
string case, I suggest we follow that instead of our current
behaviour.  Maybe we should even follow the Solaris behaviour, and not
mess with EINVAL at all except where required by SUSP.  See above,
EINVAL is completely pointless for error checking as it is not
portable.

-- 
cheers, J"org               .-.-.   --... ...--   -.. .  DL8DTL

http://www.sax.de/~joerg/                        NIC: JW11-RIPE
Never trust an operating system you don't have sources for. ;-)
Received on Fri Jan 21 2005 - 13:50:11 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:26 UTC