Re: [PATCH] Repalce strncmp by memcmp

From: Ryan Mallon
Date: Mon Nov 29 2010 - 17:49:55 EST


On 11/30/2010 11:26 AM, Steven Rostedt wrote:
> On Mon, 2010-11-29 at 17:18 -0500, Steven Rostedt wrote:
>
>> Um, do you realize that the kernel does not always use the same memcmp
>> as gcc.
>>
>
> Note, I'm not against the change, because in 99% of the cases, memcmp()
> can and will be faster, and we don't need to worry about these strange
> cases.

It can still break things in subtle ways. Lots of the replacements are
of the form:

if (strncmp(string, "foo", 3) == 0)

Which can only be replaced with memcmp if the minimum length of string
is _always_ 3. This may be true for some callsites (with careful audit),
but in general I doubt it is and it will lead to subtle bugs.

I hardly think it is worth auditing a bunch of strncmp calls to ensure
that the minimum length of the checked string is always n in order to
remove a single instruction. Making such a change will also introduce
subtle bugs if the rules for the string ever change, eg. a change is
made to allow string = "".

~Ryan

--
Bluewater Systems Ltd - ARM Technology Solution Centre

Ryan Mallon 5 Amuri Park, 404 Barbadoes St
ryan@xxxxxxxxxxxxxxxx PO Box 13 889, Christchurch 8013
http://www.bluewatersys.com New Zealand
Phone: +64 3 3779127 Freecall: Australia 1800 148 751
Fax: +64 3 3779135 USA 1800 261 2934
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/