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 3D6BD39FF3 for ; Fri, 19 Jul 2024 12:53:43 +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=1721393624; cv=none; b=J8uHH7VzpMNUM1J9LtCUI/MIsgcxm2S1he9dwZEpFh/EKWiRONqDzIbxBHBLoUlSvESg2012gEpet3SnaejXEyi7L+hQaZRyjlxYBu73mJdOaUtpQyl2ZdyJwhGqG/BSGSA3Mph4bg4NYzU35r4xAnjF4yDUNLLCxIOgZMJU0cY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721393624; c=relaxed/simple; bh=tMrQRL9VljiSXKXzRDuMpm1N044FrLq55TFM8jlh1DU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=iSPhVAcTysnwaam/9RPmJ90uBZFh9CyLbB8wPPfw6eQut60RLG7uUmyVhe6NIZN+bl625+mntX5fCVg6YOBBYjwdrugoC75ZPFvetOURDb54oTIyzVbboqgsL+xKKhLsxD4RDQxWHEO9GPmkkELOMxOvcBD1nSv/VTvqghvYy/4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=B72sw5UF; 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="B72sw5UF" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9C858C32782; Fri, 19 Jul 2024 12:53:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721393623; bh=tMrQRL9VljiSXKXzRDuMpm1N044FrLq55TFM8jlh1DU=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=B72sw5UFIfXv3SSmUFlx33RpqI6o753llsGCYyRkJk6gYvzFA85sRhO0yB43E4/W4 UmfRSCG1Eo/TJH0Nh32Gu+zjQUnrj9m/0r6+yvqBCs7Iv0fyFeKJs//CUYiv11+1nt HQhGZMvyASbL0IQa0AaJQL7BTm8MKeA1fEMjGnIA4G5GvxcOYl5CQiZQKr2HMa+5IZ LAnet/t4DirF8SlCrZX2HFjDwoNyTUBSgmWk40IS1Eo6K5TEysPCPDuFYEzrQM4Gfm VUHksDlrXdWi387O8S2aWxzMD+yHWf/hD5WrlN9jfNpan0tS2J87rGE2H3w+YAc4lf yDlJNFxePY1Zw== Date: Fri, 19 Jul 2024 14:53:39 +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 2/2] xstrtol: Add defensive check against undocumented errors Message-ID: <20240719125312.15885-4-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="puq4vj2xrpvcbjdz" Content-Disposition: inline In-Reply-To: <20240719125312.15885-2-alx@kernel.org> --puq4vj2xrpvcbjdz 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 2/2] xstrtol: Add defensive check against undocumented errors References: <20240719125312.15885-2-alx@kernel.org> MIME-Version: 1.0 In-Reply-To: <20240719125312.15885-2-alx@kernel.org> strtod(3) calls malloc(3) in some systems. While strtol(3) doesn't, let's be cautious and write code that would be safe under a theoretical implementation of strtol(3) that could ENOMEM (and let's assume we don't know what 'e' will look like after such an error). Some attempt of defensive error handling had been added in 790855e18a1d, but it was wrong, since it was assuming e!=3Dnptr on such a case, which is not a reasonable assumption. Fixes: 790855e18a1d (2003-10-14, "Handle invalid suffixes and overflow inde= pendently, so that ...") 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 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/xstrtol.c b/lib/xstrtol.c index 592673557f..faa7b9fd4c 100644 --- a/lib/xstrtol.c +++ b/lib/xstrtol.c @@ -94,7 +94,7 @@ __xstrtol (char const *nptr, char **endptr, int base, if (endptr && e) *endptr =3D e; =20 - if (e =3D=3D nptr) + if (e =3D=3D nptr && (errno =3D=3D 0 || errno =3D=3D EINVAL)) { /* If there is no number but there is a valid suffix, assume the number is 1. The string is invalid otherwise. */ --=20 2.45.2 --puq4vj2xrpvcbjdz Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEE6jqH8KTroDDkXfJAnowa+77/2zIFAmaaYdMACgkQnowa+77/ 2zJuGg/9FIvK1M0uaalejOganJKObN8WzRMFwbP70tv8SCh/b1L2P6laMtee853n jhAUcaP6uSTQrzfAiE+1PMqok82NBXQqQEIJe1bxKrGN3BZs5PILA7iaw89knHm6 CmsCTcXG29T0i4McGKabItuUx/pRxaqNS5cW4Fz9yk48rxx45imS4oSuMhVs/W1G Y9W2raU2STuECQDynT8plr9zb+zq0g0AW8doExQLmcZPULZJTWtkEf5u3JI3AVQ8 Rbkcke2yzk57nbBMH5KEVVrN1IV/TnOEGRfypJnUzUvE3b2E2pnMRKfevq3BXLkT kih49MjQG36W5G00IQp9uKUsltW0aqdkxzD24PaiIeaXKp6jQMvJ5xHjYVkn8fIg Ogqmu9g9jSPWsABhxtRKw45dN7FKJzebMOC7idxn4TqGCKGwqkaVsc38LG1X3z9F Q8bz7RFdWMrPFKE5OGoYySEEp/38cLMDjIHoFzBoJzP5HVqq8dHp78pI+ppBzafR m5Jw1X4WdkFEUR35OIo9cN0ZolwSMMD13pUAYQG1wqRwT6haUbjdfoZEJyrlBSYG fXaAUiu4rz8Yg5px1tTHGjiqqu37e/WUt+WubLu9vewse9Of8Vv57NW+FlOlXZJl XfSJcBFdzG4LzOqnDhn1R6i6d1FIytkQ7CEqF9Jy1xIES+WJwB0= =E/pe -----END PGP SIGNATURE----- --puq4vj2xrpvcbjdz--