[systemd-devel] [PATCH] shared: rename hasprefix() to startswith(), which is functionally identical and removed

Lennart Poettering lennart at poettering.net
Fri Jul 12 09:21:17 PDT 2013


On Fri, 12.07.13 18:09, Kay Sievers (kay at vrfy.org) wrote:

> 
> On Fri, Jul 12, 2013 at 5:35 PM, Shawn Landden <shawnlandden at gmail.com> wrote:
> > as most (if not all) of the prefix strings are static, these will get
> > forward constant propagation optimized into single memcmp() calls, which
> > should be much better than the non-SIMD hand-rolled version.
> 
> > -#define hasprefix(s, prefix) (memcmp(s, prefix, strlen(prefix)) == 0)
> > +#define startswith(s, prefix) (memcmp(s, prefix, strlen(prefix)) == 0)
> 
> What if 's' is shorter than 'prefix'? We should not access the
> possibly uninitialized memory?

This is a good point.

I figure memcmp() quits its loop early in such a case, so that it will
only access one vector of potentially uninitialized memory at most. That
is of course only if memcmp() compares things linearly from the
beginning. If it compares from the end, then we might end up accessing
not even uninitialized but even unmapped memory, which could trigger a
segfault... I don't think memcmp() makes any guarantees on the
direction, so maybe we should not use it after all?

strncmp() should work however, I guess...

Lennart

-- 
Lennart Poettering - Red Hat, Inc.


More information about the systemd-devel mailing list