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 1892C39FF3 for ; Fri, 19 Jul 2024 12:53:38 +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=1721393619; cv=none; b=S5l5k2bfGzc0aVuO+NuNEu2auUTFL2Fvj2W+yJHNJQ4HJqijKQqcDnq+Cq3Xrdip7udgU5wab2/Jm06SGRKtZAOTSYD4lXGt9hOfaCnJALTR/MZbKoJ9xXfFO8LUfZ0q1Y7E8zyRO2Ggjg168SbggMij7aZGB+ncYBMKN7vPeSc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721393619; c=relaxed/simple; bh=SvoOXvvg2t+gRQWCRwwe3pdlM2RnmE0WoPdo9qC3CnA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=iJ0uqPjBcN9p+75ZJuASnsebOIDO7g3z9/VhvMxa+I0jhphUanpC0YplodjPvXMSSMW+uiSQvOEE8K7qgOEy2nBj6rzdUKjf/uoSHhHb1t3tpUNY+Ukw266t14LBcoFeyQ5itr1NC+1sZkqsCKf1ewumMde5UJ1YC36V6Ya3LY4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ODBWfNzq; 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="ODBWfNzq" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 948FEC32782; Fri, 19 Jul 2024 12:53:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721393618; bh=SvoOXvvg2t+gRQWCRwwe3pdlM2RnmE0WoPdo9qC3CnA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=ODBWfNzqnfNKzLLf95bvEAIQqO6BSiqn8eJFzVV0t9CHy2ZBDcEDRSYhtWANuvmQO N2g+REn7v6Vtc2mqyhSLdmX6ogMB9TbD51vlxKTgkH6j/1Z0f/1ORjHMzlmzTglIUp Kj12MtCgBR802Pq9s+cTAHhUWTiSm5LrBqthuLZyfR1ybx92MNXKQkz8cYaxqZKo+g R2tvF2kX9Q1OemLCa2YYBuahamHgKpjme/m7d/JnnccR71knnKAm2csbmSKfWGFZMN HW3rn3ejNs38WiswuyA4fJG4Ufr4PXuX4qyMLHt5abwl4yAJ6i4L+vlIao7igHRjJc ysF4ZSuzTMLqw== Date: Fri, 19 Jul 2024 14:53:34 +0200 From: Alejandro Colomar To: bug-gnulib@gnu.org Cc: Alejandro Colomar , Paul Eggert , Bruno Haible , =?utf-8?B?xJBvw6BuIFRy4bqnbiBDw7RuZw==?= Danh , Eli Schwartz , Sam James , Serge Hallyn , Iker Pedrosa , Michael Vetter , liba2i@lists.linux.dev Subject: [PATCH v1 1/2] xstrtol: Correctly handle an invalid base Message-ID: <20240719125312.15885-3-alx@kernel.org> X-Mailer: git-send-email 2.45.2 References: <20240719125312.15885-2-alx@kernel.org> 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="os5zt5xuey5fttuk" Content-Disposition: inline In-Reply-To: <20240719125312.15885-2-alx@kernel.org> --os5zt5xuey5fttuk Content-Type: text/plain; protected-headers=v1; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable From: Alejandro Colomar To: bug-gnulib@gnu.org Cc: Alejandro Colomar , Paul Eggert , Bruno Haible , =?utf-8?B?xJBvw6BuIFRy4bqnbiBDw7RuZw==?= Danh , Eli Schwartz , Sam James , Serge Hallyn , Iker Pedrosa , Michael Vetter , liba2i@lists.linux.dev Subject: [PATCH v1 1/2] xstrtol: Correctly handle an invalid base References: <20240719125312.15885-2-alx@kernel.org> MIME-Version: 1.0 In-Reply-To: <20240719125312.15885-2-alx@kernel.org> strtol(3) doesn't set the end pointer if the base is invalid. This allows a caller to differentiate between "invalid base" (what strtoi(3bsd) calls EINVAL) and an "no digits seen" (what strtoi(3bsd) calls ECANCELED) in systems that report EINVAL on no digits seen (POSIX allows this). strtol("foo", &e, 0); strtol("0", &e, -1); The former call will set e =3D nptr. The latter will leave e untouched. The caller has no other way to portably differentiate the calls. The way to differentiate those, thus, is to initialize e =3D NULL, to allow reading it after the call. While doing this, change the behavior of this function to only set *endptr if strtol(3) has set it, leaving it untouched otherwise. Fixes: 034a18049cbc (2014-12-20, "assure: new module") Fixes: 64ddc975e72c (2024-07-18, "xstrtol: document and stray less from str= tol") Cc: Paul Eggert Cc: Bruno Haible 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: Signed-off-by: Alejandro Colomar --- lib/xstrtol.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/lib/xstrtol.c b/lib/xstrtol.c index c3145171f3..592673557f 100644 --- a/lib/xstrtol.c +++ b/lib/xstrtol.c @@ -71,9 +71,7 @@ strtol_error __xstrtol (char const *nptr, char **endptr, int base, __strtol_t *val, char const *valid_suffixes) { - char *t_ptr; - char **p =3D endptr ? endptr : &t_ptr; - *p =3D (char *) nptr; + char *e =3D NULL; =20 if (! TYPE_SIGNED (__strtol_t)) { @@ -82,14 +80,21 @@ __xstrtol (char const *nptr, char **endptr, int base, while (isspace (ch)) ch =3D *++q; if (ch =3D=3D '-') - return LONGINT_INVALID; + { + if (endptr) + *endptr =3D (char *) nptr; + return LONGINT_INVALID; + } } =20 errno =3D 0; - __strtol_t tmp =3D __strtol (nptr, p, base); + __strtol_t tmp =3D __strtol (nptr, &e, base); strtol_error err =3D LONGINT_OK; =20 - if (*p =3D=3D nptr) + if (endptr && e) + *endptr =3D e; + + if (e =3D=3D nptr) { /* If there is no number but there is a valid suffix, assume the number is 1. The string is invalid otherwise. */ @@ -113,19 +118,19 @@ __xstrtol (char const *nptr, char **endptr, int base, return err; } =20 - if (**p !=3D '\0') + if (*e !=3D '\0') { int xbase =3D 1024; int suffixes =3D 1; strtol_error overflow; =20 - if (!strchr (valid_suffixes, **p)) + if (!strchr (valid_suffixes, *e)) { *val =3D tmp; return err | LONGINT_INVALID_SUFFIX_CHAR; } =20 - switch (**p) + switch (*e) { case 'E': case 'G': case 'g': case 'k': case 'K': case 'M': case '= m': case 'P': case 'Q': case 'R': case 'T': case 't': case 'Y': case '= Z': @@ -138,10 +143,10 @@ __xstrtol (char const *nptr, char **endptr, int base, power-of-1024. */ =20 if (strchr (valid_suffixes, '0')) - switch (p[0][1]) + switch (e[1]) { case 'i': - if (p[0][2] =3D=3D 'B') + if (e[2] =3D=3D 'B') suffixes +=3D 2; break; =20 @@ -153,7 +158,7 @@ __xstrtol (char const *nptr, char **endptr, int base, } } =20 - switch (**p) + switch (*e) { case 'b': overflow =3D bkm_scale (&tmp, 512); @@ -224,8 +229,10 @@ __xstrtol (char const *nptr, char **endptr, int base, } =20 err |=3D overflow; - *p +=3D suffixes; - if (**p) + e +=3D suffixes; + if (endptr) + *endptr =3D e; + if (*e) err |=3D LONGINT_INVALID_SUFFIX_CHAR; } =20 --=20 2.45.2 --os5zt5xuey5fttuk Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEE6jqH8KTroDDkXfJAnowa+77/2zIFAmaaYc4ACgkQnowa+77/ 2zLvdg/7BpL+T18Odma2CpBhuL/vwdQFyy2UMkWz7gnBjNCxoO/1/2KYxnI/tebu FoaQCN/xdL5rQBg9UATQTV3fco7sw0+CxcIteW5Iu5FGivQMv9Nbs/YTyrX3E84x k16LfkrSi5ZYEBJkjxnY8EqASNVhTisW9/Pcx5tkDwvp1lgiYS6rd08O8Kd3Qo0f fMKW2ZDNMfu5aZJu/3e/WN6B7pfaJxMyTmK4KCRGp7AT5OT6JdVPlmtYxxgt1Tf1 cNDesdHB+yZPXsR28cM2jlyx8/kqqRvaBwt81Iw5/Nb8L1EdAIpjKEBy9NwU2Lh3 kgo9LkvFSFLCO+J3psLdaR4yd6fmZOu3np6Y79/E4r51EWg6W/uKjIvVLU67dW0d t1XfHclx7iBq0bqkSZo3hwrnz58QHT9XfkK81j+uaKTS7KFRYtAcHcZfVpm+JUH9 Gt9rN0/D/tr0T+BHgXLlT2z4HHFE47qWDu3QMmks5JE5HLjFMs2Zpuoxm9TWWq2s surtB9rWWUe3TeTmxFv77GWb0D3h4sfFsXyga1YuR1Tg6qkCOLeJp1k5hR4kKXNu kcRx6EKXqDMbROA1GeoM8kWLlUtVywt04qSTjDHI3I/aGj5/nLn1KiEboDujm9ZP x5jCDyOSJcbQ3TGqIEAeBGQZPlY8a6r1ZZ74rRVvf80Mg1HeoO0= =Sb8Q -----END PGP SIGNATURE----- --os5zt5xuey5fttuk--