On Sat, 22 Dec 2018 18:33:46 +0900
Namhyung Kim <namhy...@kernel.org> wrote:

> > +static __always_inline size_t str_has_prefix(const char *str, const char 
> > *prefix)
> > +{
> > +   size_t len = strlen(prefix);
> > +   return strncmp(str, prefix, len) == 0 ? len : 0;  
> 
> As it already knows the length (and it needs to use it for return
> value), isn't it (slightly) better using memcmp() instead?

No, because we don't know the length of str.


        [ str = "h\0[bad memory]" ]


        str_has_prefix(str, "TEST THIS BIG STRING AT FRONT")


If we use memcmp(), then we are testing way after str has ended, and
that can cause a memory fault.

-- Steve

Reply via email to