Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E2E5815AF1 for ; Sat, 20 Jul 2024 19:03:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721502215; cv=none; b=FPgFmtbwlVVR7MuumVfIuNKAkEz2QK2d+E/nBzj/BOe88jwwRGcIzFaXBPM2gmEkck7TL65pGbqBTp8ocnj89d9jcpyAC5eSpdBZniwNRz2Srk++VXfH+YEDrLrT8O5te/yGchChsDz3m1BAbTTJWeFi7yVI/TV8Ac4l34P7Q+w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721502215; c=relaxed/simple; bh=ySppU1jrN6t07rc6ry+53lDWfNaJROAfpNgR9WCAqJw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=CRqRrU/iRgjYnMRuS+/ilplUajZUOm5wnu7y+eZLhzMEaxECuxjNBR1svuvqNa1npy7tD9cFY0FmCbwnJPmtNssq3ASoC+ZS3n8ojz8We4igwh/HFXDwTSURB0v/3eKWX/ehY5pBm5BVJhYkVo25mlG9XzJ2jCW0GSttMOZ29hQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CieluMVH; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="CieluMVH" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 289F9C2BD10; Sat, 20 Jul 2024 19:03:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721502214; bh=ySppU1jrN6t07rc6ry+53lDWfNaJROAfpNgR9WCAqJw=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=CieluMVHeNHXkLZnZLrMlhYbgZmd+pRdY2QrBM19seSlW9TXxl/3fTsQRMPhdBdQM kaAaqb64pCGhTlqF6um5QS0WxC5qyWCdClSjbZWM8xSQx7hOrdK5n4f0AP5/WdS8TK ZAHpemwYxW0G3qBv7KDth5ncfLw2jHAx5Ic3l7+8nGbu1As3xdQm+M/Y3sr8NQaSst x88G1k+lE4CGe47/jOvgPBUNyVwNdvS63HA0aXUE5GProUEagP0E6nh4Oq6IoWRyAs YVSypttLuuS0YVH5vlgY7gn2aKKVdu5JgjEQe1fSDs23+IEsEbZHQ0/amG6aL8Mfos nFAi+riBtAfBw== Date: Sat, 20 Jul 2024 21:03:30 +0200 From: Alejandro Colomar To: tech-misc@netbsd.org Cc: Alejandro Colomar , Guillem Jover , christos , =?utf-8?B?xJBvw6BuIFRy4bqnbiBDw7RuZw==?= Danh , Eli Schwartz , Sam James , Serge Hallyn , Iker Pedrosa , Michael Vetter , libbsd@lists.freedesktop.org, liba2i@lists.linux.dev Subject: [PATCH] strto[iu](3): Make the implementation portable Message-ID: <20240720190321.550121-2-alx@kernel.org> X-Mailer: git-send-email 2.45.2 References: Precedence: bulk X-Mailing-List: liba2i@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="xqoscqehcavmmfyf" Content-Disposition: inline In-Reply-To: --xqoscqehcavmmfyf Content-Type: text/plain; protected-headers=v1; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable From: Alejandro Colomar To: tech-misc@netbsd.org Cc: Alejandro Colomar , Guillem Jover , christos , =?utf-8?B?xJBvw6BuIFRy4bqnbiBDw7RuZw==?= Danh , Eli Schwartz , Sam James , Serge Hallyn , Iker Pedrosa , Michael Vetter , libbsd@lists.freedesktop.org, liba2i@lists.linux.dev Subject: [PATCH] strto[iu](3): Make the implementation portable References: MIME-Version: 1.0 In-Reply-To: POSIX allows systems that report EINVAL when no digits are found. On such systems the only way to differentiate EINVAL and ECANCELED is to initialized the end pointer to NULL before the call. On EINVAL cases, strto*max(3) will leave the pointer unmodified, so we'll read back the original NULL. On ECANCELED cases, strto*max(3) will set it to nptr. Link: Cc: Guillem Jover Cc: christos Cc: =C4=90o=C3=A0n Tr=E1=BA=A7n C=C3=B4ng Danh Cc: Eli Schwartz Cc: Sam James Cc: Serge Hallyn Cc: Iker Pedrosa Cc: Michael Vetter Cc: Cc: Signed-off-by: Alejandro Colomar --- Hi Christos, I'm not sure if this patch is wanted in NetBSD. It doesn't fix any bugs there. It would be interesting for those pasting this code in other systems (which is currently done by libbsd). Just in case you're interested, here it is. I didn't test it (I don't have a NetBSD build around), so please review thoroughly. BTW, the line=20 + if (nptr =3D=3D e && (*rstatus =3D=3D 0 || *rstatus =3D=3D EINVAL)) You could also choose to simplify it as just `if (nptr =3D=3D e)`, since all existing implementations (AFAIK) only arrive at nptr =3D=3D e with errno being either 0 or EINVAL. However, for preventing ENOMEM or other strange errors that an implementation might add, those tests are there. Feel free to drop them (I didn't add them in my own strtoi(3) implementation). I've kept them here for completeness (and because you already had a test `*rstatus =3D=3D 0` in that line, which was already superfluous, so I guessed you were preventing that kind of problems. Have a lovely day! Alex common/lib/libc/stdlib/_strtoi.h | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/common/lib/libc/stdlib/_strtoi.h b/common/lib/libc/stdlib/_str= toi.h index b838608f6b52..bea6a9f285a7 100644 --- a/common/lib/libc/stdlib/_strtoi.h +++ b/common/lib/libc/stdlib/_strtoi.h @@ -3,6 +3,7 @@ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. + * Copyright (c) 2024, Alejandro Colomar * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -69,7 +70,7 @@ INT_FUNCNAME(_int_, _FUNCNAME, _l)(const char * __restric= t nptr, int serrno; #endif __TYPE im; - char *ep; + char *e; int rep; =20 _DIAGASSERT(hi >=3D lo); @@ -77,8 +78,7 @@ INT_FUNCNAME(_int_, _FUNCNAME, _l)(const char * __restric= t nptr, _DIAGASSERT(nptr !=3D NULL); /* endptr may be NULL */ =20 - if (endptr =3D=3D NULL) - endptr =3D &ep; + e =3D NULL; =20 if (rstatus =3D=3D NULL) rstatus =3D &rep; @@ -90,9 +90,9 @@ INT_FUNCNAME(_int_, _FUNCNAME, _l)(const char * __restric= t nptr, =20 #if defined(_KERNEL) || defined(_STANDALONE) || \ defined(HAVE_NBTOOL_CONFIG_H) || defined(BCS_ONLY) - im =3D __WRAPPED(nptr, endptr, base); + im =3D __WRAPPED(nptr, &e, base); #else - im =3D __WRAPPED_L(nptr, endptr, base, loc); + im =3D __WRAPPED_L(nptr, &e, base, loc); #endif =20 #if !defined(_KERNEL) && !defined(_STANDALONE) @@ -100,8 +100,11 @@ INT_FUNCNAME(_int_, _FUNCNAME, _l)(const char * __rest= rict nptr, errno =3D serrno; #endif =20 + if (endptr !=3D NULL && e !=3D NULL) + *endptr =3D e; + /* No digits were found */ - if (*rstatus =3D=3D 0 && nptr =3D=3D *endptr) + if (nptr =3D=3D e && (*rstatus =3D=3D 0 || *rstatus =3D=3D EINVAL)) *rstatus =3D ECANCELED; =20 if (im < lo) { @@ -117,7 +120,7 @@ INT_FUNCNAME(_int_, _FUNCNAME, _l)(const char * __restr= ict nptr, } =20 /* There are further characters after number */ - if (*rstatus =3D=3D 0 && **endptr !=3D '\0') + if (*rstatus =3D=3D 0 && *e !=3D '\0') *rstatus =3D ENOTSUP; =20 return im; base-commit: 7a4c6afd05862bf8c28f0730d8d9cd7e2dce2a50 --=20 2.45.2 --xqoscqehcavmmfyf Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEE6jqH8KTroDDkXfJAnowa+77/2zIFAmacCgIACgkQnowa+77/ 2zIWNA/+N35BUXlzTp4a/e/ttbMBSxGuqZhG/+bqiq0NXof9raGbxW9sISXXT+7l HvsvVDuZGtd1zmZEoZZgE5NfP/8aGL1eAO9xPXfzle2mmG7mEqHHI+lV93LPj0ks cN1rTIJ6fDGqoWfJZ5qtstKQM7BydIQ6VrLclwqu7tDVX3BU/twYpyHz9xaej51Y 2lGLgQqsPbCUsNTLx7vMavqkaHS8Bm//9rzpqKPMdxAeEzirExFcNtrwKnpOncO2 Z1t5XvPqw4fvs1gLT/D6N5bNhKUuCbFuPb/P3Xalp+VAmzrCGaRKYgnHj/vbWWXX C48jOQwE2Y75lbPB/bbY5HTz6e7ckiACXXKfOu/fhAP289iGwSZNjdyK1tTdz1ZE ph5c4AcuWalGJerpxFpBHG5QKKk3IocDs5qJa6zQ9xeeAu0RiuGrQ9mnLc0LCgR4 d41ByE7Yod+v0QK3lDZow90XhDgDT+TzkBxA36Q+lXxWQf5SJPuo7efXcjP0FwTj k6iKX+qreNJOaXHqf8b+vuO7Twegjn8792UyFjWByVPv4jzBdrlRnGoM+/+pUSKw KcFNt422wS1KQpLKyW5dh88gXfRRhdQDnoOIqZSK+jcq1C5an66hJjeSvQ42yIJG xvPR52zwBQFeCBtiCEfjCC3BpkghjyLbPB9O7JmmFj7b/OcurO8= =6RjC -----END PGP SIGNATURE----- --xqoscqehcavmmfyf--