Messages in this thread | | | Subject | Re: [PATCH] Repalce strncmp by memcmp | From | Bernd Petrovitsch <> | Date | Tue, 30 Nov 2010 11:27:51 +0100 |
| |
On Die, 2010-11-30 at 11:51 +1300, Ryan Mallon wrote: [...] > 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 = "".
On the conceptual level: And it will confuse people if strings (read: '\0' terminated char arrays specified by a pointer to the start) with raw memory (read: memory with arbitrary content - including '\0' not at the end - specified by a pointer to the start and the valid size). Even more confusing is that raw memory *may* hold a string ...
Don't get me wrong: every seasoned C programmer should know that. But not everyone is one ...
Bernd -- Bernd Petrovitsch Email : bernd@petrovitsch.priv.at LUGA : http://www.luga.at
| |