[PATCH v1 2/2] xstrtol: Add defensive check against undocumented errors

Alejandro Colomar <[email protected]> Fri, 19 Jul 2024 14:53:39 +0200
Newsgroups dev.linux.lists.liba2i
Message-ID <[email protected]>
--puq4vj2xrpvcbjdz
Content-Type: text/plain; protected-headers=v1; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
From: Alejandro Colomar <[email protected]>
To: [email protected]
Cc: Alejandro Colomar <[email protected]>, Paul Eggert <[email protected]>, 
	Bruno Haible <[email protected]>, 
	=?utf-8?B?xJBvw6BuIFRy4bqnbiBDw7RuZw==?= Danh <[email protected]>, Eli Schwartz <[email protected]>, Sam James <[email protected]>, 
	Serge Hallyn <[email protected]>, Iker Pedrosa <[email protected]>, 
	Michael Vetter <[email protected]>, [email protected]
Subject: [PATCH v1 2/2] xstrtol: Add defensive check against undocumented
 errors
References: <[email protected]>
MIME-Version: 1.0
In-Reply-To: <[email protected]>

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 <[email protected]>
Cc: Bruno Haible <[email protected]>
Cc: =C4=90o=C3=A0n Tr=E1=BA=A7n C=C3=B4ng Danh <[email protected]>
Cc: Eli Schwartz <[email protected]>
Cc: Sam James <[email protected]>
Cc: Serge Hallyn <[email protected]>
Cc: Iker Pedrosa <[email protected]>
Cc: Michael Vetter <[email protected]>
Cc: <[email protected]>
Signed-off-by: Alejandro Colomar <[email protected]>
---
 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--