NetBSD-Bugs archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: lib/59198: Reject negative input in strtou(3), reporting ERANGE
The following reply was made to PR lib/59198; it has been noted by GNATS.
From: Alejandro Colomar <alx%kernel.org@localhost>
To: gnats-bugs%netbsd.org@localhost
Cc: Bruno Haible <bruno%clisp.org@localhost>
Subject: Re: lib/59198: Reject negative input in strtou(3), reporting ERANGE
Date: Fri, 21 Mar 2025 21:25:36 +0100
--jrofmsi74wrqmdsk
Content-Type: text/plain; protected-headers=v1; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
From: Alejandro Colomar <alx%kernel.org@localhost>
To: gnats-bugs%netbsd.org@localhost
Cc: Bruno Haible <bruno%clisp.org@localhost>
Subject: Re: lib/59198: Reject negative input in strtou(3), reporting ERANGE
References: <pr-misc-59198%gnats.netbsd.org@localhost>
<20250319232212.8609C1A923C%mollari.NetBSD.org@localhost>
<20250319232500.4D8401A923C%mollari.NetBSD.org@localhost>
MIME-Version: 1.0
In-Reply-To: <20250319232500.4D8401A923C%mollari.NetBSD.org@localhost>
Hi,
Actually, considering the name strtou(3), which is about **unsigned**
integers, maybe negative numbers should not trigger an ERANGE error, but
actually an ECANCELED error. The sign, even a positive sign, should not
be part of an unsigned number.
Here are some example implementations for this:
unsigned long
strtoul_nn(const char *restrict s, char **restrict endp, int base)
{
while (isspace((unsigned char) *s))
s++;
if (!isdigit((unsigned char) *s))
return 0;
return strtoul(s, endp, base);
}
uintmax_t
strtou_nn(const char *restrict s, char **restrict endp, int base,
uintmax_t min, uintmax_t max, int *status)
{
while (isspace((unsigned char) *s))
s++;
if (!isdigit((unsigned char) *s)) {
if (status)
*status =3D ECANCELED;
return min;
}
return strtou(s, endp, base, min, max, status);
}
I'm going to expand the BUGS section in the strtoul(3) Linux manual
page, and mention the strtoul_nn() example as a workaround for this bug
in the standard strtoul(3).
The strtou_nn() should be the ideal behavior of strtou(3).
(I hope this arrives at the bug ticket.)
Cheers,
Alex
--=20
<https://www.alejandro-colomar.es/>
--jrofmsi74wrqmdsk
Content-Type: application/pgp-signature; name="signature.asc"
-----BEGIN PGP SIGNATURE-----
iQIzBAABCgAdFiEES7Jt9u9GbmlWADAi64mZXMKQwqkFAmfdyzkACgkQ64mZXMKQ
wqmUkBAAi/WCW17t22OdSSJOMnHOcE4VudxM0Y7PZls5jE9oKxPIUoEcTlTR9566
QKHMCJPswkazqUqrJMb4/4Gsr2RyHcJPJTz9HYqIK7BX4MZWOl/4BXe7Qam6POvn
YIdBhKmUoePKfK+diicGqqfcWWUZXJIqZM1PD1JASveemrs6KxP5Iqjo5RzTJogV
VSNwmLVgnqy3DO50usehl2bEeiDQLxGNPoKsIg9YRY0T3x3dVkcYkcV0guGw3riL
7EhcJWFA6ENbuGrfRgAZhfXw2ltO5vb9KMxjplbjSLIPEDytDAFvtrfKuvV4pcbK
7zEM3ql7lNH8czoeI6t/mkauUjnRkWLJ/dgnZvOm3eo0jjzcNsI3ZUlsjnklayHY
8lzleyTBGLMZ3k76QPSXaQkaj109juWU02U7XKhlF+2CH+X+J3gDuvWZuJt3F6Ik
PMk6Sb5vb12RI0NxS1LSX0NyXMZ7Y7ns+DD6M9aEY89hjhsaVgVtsu94LRyFRv1m
G3fwhVAPPx1zmcZLtigWnv1PLKeYsVHydn85rcAouN3aCqvNLYezg9nXXbirrLOI
yIeSAO1acFXVIprC/RBhBs9NIo8/Qj501pGtQ0TVfxZGhhvWKH51ERghybfQzQ3G
TV5gfMRiSv9YkELM7uSCioXok5Z6sw77oMopRPS5Jj1fxrJYkTo=
=RFuY
-----END PGP SIGNATURE-----
--jrofmsi74wrqmdsk--
Home |
Main Index |
Thread Index |
Old Index