Re: [PATCH v2 3/6] match_basename: use strncmp instead of strcmp
- From
Duy Nguyen <pclouds@gmail.com>
- Date
- Mar 10, 2013, 12:06 UTC
- Message-ID
- <CACsJy8A4+2t=PMJ+iSFFz-fafkAHYGvm6G4M-qpiO9674sanEQ@mail.gmail.com>
- In-Reply-To
- <CALWbr2x6V6TB9g_nQCgG2r9L__a2wxG28Qi5KTXChHxj5JSQ8w@mail.gmail.com>
On Sun, Mar 10, 2013 at 6:54 PM, Antoine Pelisse <apelisse@gmail.com> wrote:
Show 15 quoted lines
> On Sun, Mar 10, 2013 at 12:43 PM, Antoine Pelisse <apelisse@gmail.com> wrote: >> On Sun, Mar 10, 2013 at 11:38 AM, Duy Nguyen <pclouds@gmail.com> wrote: >>> glibc's C strncmp version does 4-byte comparison at a time when n >=4, >>> then fall back to 1-byte for the rest. >> >> Looking at this >> (http://fossies.org/dox/glibc-2.17/strncmp_8c_source.html), it's not >> exactly true. >> >> It would rather be while (n >= 4), manually unroll the loop. > > By the way, if we know the length of the string, we could use memcmp. > This one is allowed to compare 4-bytes at a time (he doesn't care > about end of string). This is true because the value of the length > parameter is no longer "at most".
We still need to worry about access violation after NUL when two strings have different lengths. That could be avoided in this particular case, but I think it's too fragile.
-- Duy