Re: [PATCH RESEND] staging: nvec: use strcmp() instead of strncmp() with magic length

Thierry Reding <[email protected]> Wed, 29 Jul 2026 12:15:53 +0200
Newsgroups org.kernel.vger.linux-tegra,dev.linux.lists.linux-staging
Message-ID <amnPL9LvsNgWW0HA@orome>
--ojp6wdg7qyvwg6nq
Content-Type: text/plain; protected-headers=v1; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
Subject: Re: [PATCH RESEND] staging: nvec: use strcmp() instead of strncmp()
 with magic length
MIME-Version: 1.0

On Tue, Jul 28, 2026 at 06:29:06PM +0300, Dan Carpenter wrote:
> On Tue, Jul 28, 2026 at 09:09:23AM +0200, Greg KH wrote:
> > On Thu, Jul 23, 2026 at 07:18:23PM +0300, Artem Lytkin wrote:
> > > Replace strncmp() with a hardcoded length of 30 with strcmp().
> >=20
> > I thought we were trying to get rid of strcmp() usage?  Why add it
> > back?
>=20
> I don't think we're trying to get rid of strcmp().
>=20
> strncmp() is for prefixes and and strcmp() is for whole words.
> They're not the same.  strncmp() with a fixed string always
> seemed like nonsense to me and it causes a static checker in
> unpublished Smatch checks because we've seen that bug where
> people wanted to check the prefix but used the wrong number of
> characters.

Maybe the commit message should be updated. It makes the argument that
bat_type is NUL-terminated and that that is the reason why changing this
to strcmp() is safe. But it's not. There doesn't seem to be any sanity
checking on the EC data that serves as input to this, so the length of
the string that is copied could be any arbitrary value for all we know,
meaning we may already be writing past the end of bat_type and insert
the NUL some place invalid to begin with (we should probably fix that at
some point).

The real reason the conversion is safe is because strcmp() stops
searching the haystack if the needle is exhausted. Since the needle is
only 2 characters in this case, *that's* why we don't have to worry
about the limiting the search.

Thierry

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

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

iQIzBAABCgAdFiEEiOrDCAFJzPfAjcif3SOs138+s6EFAmpp0tQACgkQ3SOs138+
s6GhJg//SLWlLDA0U7aX3DzUW6YR2GmeIn/r3wr9R7fXgXxx/dpPVh2N6Vfo4Zxp
hb8dhVS5/FjPSD0QtT9sfO7tsTzwcim4FFKM8J+KfnKP4u+s0LNMKrUmp7GZUMow
x/Wofbhq4opHEixqMJEyQcUd428CYYe9JFZB/uA+2iyTjBW5vQxamO+FXdSwYL47
W4YcFlU5Ycl1HeZTxvlnDPctHJbuIzoNvkm0i6d02p/OfMib7eUH5J7zBBsHq8if
dTVZ67y9fd62dPO0D8MJl4BdPHTSJNBGGffik+6tIRL+6teM5UHfC17jmwhmC3je
UxQrDhprVdj+Pcti2u9LbmF65JGQmMYjgQShumBwglLsYsnblosgVfvO+Ey3zHk0
3tpCIwHV2cz2iKFcCsvGswCBTCcfX2VR7c79EkmHPS2X/KmhH8ZoSMqSjvPOMXxx
QP8CfrSd4Bu7ZzJd/xVSgkEosDNlLwFQHIgNR3zgRQ58iOYy2Uz2ce+Ih2GExmhP
3CiiR3hlpdcqyqP+/PDxVD03NnrHolJtaIfsc5eM6bXkJqR7d85xD8Ht4oG7q1sH
fN/46k451eiH6dwvSMFsNl/oPrIO8O0BxXGWibKGk+QH+FXdssOLxEisLAou91Q8
rUancrvWrde4TUSA6EhUV/DsO0LBcE+ThEo7BqOtS738lP79dsA=
=b+P8
-----END PGP SIGNATURE-----

--ojp6wdg7qyvwg6nq--