lkml.org 
[lkml]   [2018]   [Dec]   [22]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [PATCH v4] string.h: Add str_has_prefix() helper function
On Sat, Dec 22, 2018 at 07:24:04AM -0500, Steven Rostedt wrote:
> On Sat, 22 Dec 2018 18:33:46 +0900
> Namhyung Kim <namhyung@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.

I don't know what's the bad memory causing memory fault but anyway
memcpy() should stop at the NUL character first as it's different, no?

Thanks,
Namhyung

\
 
 \ /
  Last update: 2018-12-22 18:42    [W:0.077 / U:0.352 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site