Re: alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD

Alejandro Colomar <[email protected]> Wed, 19 Mar 2025 19:01:17 +0100
Newsgroups dev.linux.lists.liba2i
Message-ID <6oyljvsenypqnrmgjbcwskqpdsag677h2dzay6hvfoosju4224@3j7iczm4d7nw>
--l2h3skcelow4sawy
Content-Type: text/plain; protected-headers=v1; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
From: Alejandro Colomar <[email protected]>
To: Bruno Haible <[email protected]>
Cc: [email protected], [email protected], 
	[email protected], [email protected], christos <[email protected]>, 
	=?utf-8?B?xJBvw6BuIFRy4bqnbiBDw7RuZw==?= Danh <[email protected]>, Paul Eggert <[email protected]>, 
	Eli Schwartz <[email protected]>, Guillem Jover <[email protected]>, 
	Iker Pedrosa <[email protected]>, Michael Vetter <[email protected]>, Robert Elz <[email protected]>, 
	[email protected], Sam James <[email protected]>, "Serge E. Hallyn" <[email protected]>
Subject: Re: alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD
References: <mgcfwxfmv3kpfnkkf6uj63kx5tdzl64p2zg2us4ntsu6q5xkwj@k52z5yyiywoo>
 <[email protected]>
 <6alwxn5mchma25qxvmzebx5vfheulfvgev7f7xjcjshma3hfok@3qtsvbzoljb2>
 <28568979.gRfpFWEtPU@nimes>
MIME-Version: 1.0
In-Reply-To: <28568979.gRfpFWEtPU@nimes>

Hi Bruno,

On Wed, Mar 19, 2025 at 06:35:29PM +0100, Bruno Haible wrote:
> Alejandro Colomar wrote:
> > > >> To address this adoption problem, how about changing these functio=
n to
> > > >> generic functions (in the sense of <tgmath.h>)? In such a way that
> > > >>     strtoi (n, &end, base, LONG_MIN, LONG_MAX, &status)
> > > >> is known to return a 'long' rather than 'intmax_t', and
> > > >>     strtoi (n, &end, base, INT_MIN, INT_MAX, &status)
> > >=20
> > > That, and especially=E2=80=A6
> >=20
> > Please propose an implementation of the overload-selectinging macro, and
> > clarify how this should work:
> >=20
> > 	n =3D strto*(s, NULL, 0, SHRT_MIN, UINT_MAX, &status);
>=20
> Indeed, the "usual arithmetic conversions" (ISO C 23 =C2=A7 6.3.1.8, =C2=
=A7 7.27.(7))
> would not work well in this case. Instead, one needs to distinguish strtoi
> and strtou:
>   - For strtoi, the first of the types 'signed char', 'short', 'int', 'lo=
ng',
>     'long long', 'intmax_t' that contains both the min and the max value.
>   - For strtou, the first of the types 'unsigned char', 'unsigned short',
>     'unsigned int', 'unsigned long', 'unsigned long long', 'uintmax_t' th=
at
>     contains both the min and the max value.

What if none contain the value?

E.g.:

	n =3D strtoi(s, NULL, 0, SHRT_MIN, ULLONG_MAX, &status);
	n =3D strtou(s, NULL, 0, 3, -1, &status);

This first one is a bug, but we should still decide what to do with it.
The second one could be a short-hand to say UINTMAX_MAX.

Anyway, for this, I'm working on a better API.  I don't think we should
make strtoi(3) type-generic.  a2i() is better for that, accepting the
type as a parameter.


Cheers,
Alex

--=20
<https://www.alejandro-colomar.es/>

--l2h3skcelow4sawy
Content-Type: application/pgp-signature; name="signature.asc"

-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEES7Jt9u9GbmlWADAi64mZXMKQwqkFAmfbBmcACgkQ64mZXMKQ
wqmBlxAAtpA6HPNVgI5tQ1YU3Un5z7VClEH4hu0fq7Yvq3m4+FJsZQUyKQQJlDfz
wtcLqyGHByDJuKT38rEUdaBnIwCimSdbzhsKIY1Q09og9iCWyu26JTFv5VSd3HVa
3YejxhNDYH/3/lO96fDSwVbzLBrSzSa6PqvWUcwiz81On6HQaRX/Rj7v5yXlZus9
TEiJcpsbf007CoZac3bElSObU8nsfqlXyK+kPhLhorr4XqMU5JT/5GXVX33fLYPz
/Sue16mHlOYn11Z+koj1F1r7uABhBlCCifR3R8hmn4kS94/9Y6+mbekf+Ee70wyc
hLkyX71X6m/oKxST7x21g0wqGMNyPuvQAYna1L8zktJCAnQtE2Cl6FTPsDHxr2Yh
VWGPdf3I/X9Nui+xYPFNHhi2FrVmOeS4Htq0aQM6pKrbTdIEcJXAKhHrW3c69LN5
aGPMLlazVurRZ8fl9LI+cS6RGk5v4oeqGnmuZl0QmgtSwxryuZAgAA95EVcvmW8X
UbRq2jSQnbwZQZewLpolA2ec190uFw6bpqZi6oQBc1zU29tBFU8XAr7S+w4UJ77h
J0/JU0na0yzjSe8I7fNi/dXmoFs4HqWQU6NcFAQ/MwAYHH6NS3xRsIx6p3/pMkNA
GqgiWAOzUnzLdd88CTTN8Zv9vPQylC8TXVwqCoJcyYVBR37UMMk=
=8jh7
-----END PGP SIGNATURE-----

--l2h3skcelow4sawy--