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:34:42 +0100
--77v3ngsjjqengmmj
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>
<knzempljqvci4o5v4qnunnog7l5yarhc5ttxktgyynkrfzyniv@2yj6ku46u76m>
MIME-Version: 1.0
In-Reply-To: <knzempljqvci4o5v4qnunnog7l5yarhc5ttxktgyynkrfzyniv@2yj6ku46u76m>
On Fri, Mar 21, 2025 at 09:25:38PM +0100, Alejandro Colomar wrote:
> Hi,
>=20
> 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.
>=20
> Here are some example implementations for this:
>=20
> unsigned long
> strtoul_nn(const char *restrict s, char **restrict endp, int base)
> {
> while (isspace((unsigned char) *s))
> s++;
> if (!isdigit((unsigned char) *s))
This should call isxdigit(3).
> return 0;
>=20
> return strtoul(s, endp, base);
> }
>=20
> 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)) {
This should call isxdigit(3).
> if (status)
> *status =3D ECANCELED;
> return min;
> }
>=20
> return strtou(s, endp, base, min, max, status);
> }
>=20
> 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).
>=20
> The strtou_nn() should be the ideal behavior of strtou(3).
>=20
> (I hope this arrives at the bug ticket.)
>=20
>=20
> Cheers,
> Alex
>=20
> --=20
> <https://www.alejandro-colomar.es/>
--=20
<https://www.alejandro-colomar.es/>
--77v3ngsjjqengmmj
Content-Type: application/pgp-signature; name="signature.asc"
-----BEGIN PGP SIGNATURE-----
iQIzBAABCgAdFiEES7Jt9u9GbmlWADAi64mZXMKQwqkFAmfdzWEACgkQ64mZXMKQ
wqkpLg/+LKjyFbj58SpjPDdXb/TKevnJ4mGg5b7x8d1VWuf+AO4zTu8Vqzvc7stH
6nuGZxaurPGkEj+zxfOUUHwWUZzZ+xFIM4y69B9x+ldM8EHyATjzehR08+UPo3cY
gt7pUDVYFVTTQEvcMZmdUXJUX/jV8zS1/ZO/pw9yXk6jgRQl4i1PDrg0Dzobz/zz
WTYqhlBwsr6RYjFwvZFCGUpL5QuFV/8ssWkrF9hIa1O/fbPA1KQm26otIgziIk+k
uHGN1E5IZavAS2Lp0sjVh9akMys945Shp4vk7gT3xMAXfLAiEIJg2BHw4cM3KFHb
oMOCrY6x9GmkipJPGrRS3AbRcFBLOIG6DNfEJRXpKaOpbs7+54CJpAgrFTNKIdtL
uTWxH2qrqUK/j/C3opnjER23poDJJNe6iLh/pERnp1cRAuPv5etzB6eZHQZzJaTx
I420QqfKU2smX/3bKD1s7Le+G0svdRzmzuXz/WhWjkkBpHhrjX+xKBBiEHRSuvlh
NByBBOTtmJ92RisUzJBaFFgBPDz8ywXw76R7u7hbOtS5MazeBljZWL4A65kmsIld
Z5fkff57Q/uSklWUarx1lI+oEcudabY+zOxfo/ChUGOSUVfa99rlSrRwVc4wg+Pd
t6lhf5+h2XnO5/yEMXD6xg8XJWxl/DOYiOnVUNIhJynO604D8ao=
=SOm5
-----END PGP SIGNATURE-----
--77v3ngsjjqengmmj--
Home |
Main Index |
Thread Index |
Old Index