[lxc-devel] [PATCH 2/6] Delete string from array Add function to delete a string from a non-null terminated buffer

Christian Brauner christianvanbrauner at gmail.com
Mon Sep 7 18:35:32 UTC 2015


On Mon, Sep 07, 2015 at 05:03:37PM +0000, Serge Hallyn wrote:
> Quoting Serge Hallyn (serge.hallyn at ubuntu.com):
> > Quoting Christian Brauner (christianvanbrauner at gmail.com):
> > 
> > I'm probably wrong, but
> > 
> > 1. if the buffer is non-null-terminated, then can't the memmove of
> > 	strlen(del) - needlelen + 1 end up in a segv?
> > 2. the comment should probably mention newlines.  If needle doesn't
> > 	include newline, and the string is a policy, we'll end up
> > 	with extra newlines.  Which may not matter, unless we do
> > 	thousands of operations on a container...
> 
> In fact, if it is not null-terminated, you can't do the strlen(del)
> safely, can you?

Right, I would change
        memmove(del, del + needlelen, strlen(del) - needlelen + 1);
to
        memmove(del, del + needlelen, strnlen(del, haystacklen) - needlelen);

The Current master relies on the same idea in mod_rdep(), I'll send a patch for
the current master in a few minutes, unless you're on it right now.

Christian
> 
> > > Signed-off-by: Christian Brauner <christianvanbrauner at gmail.com>
> > > 
> > >  100.0% src/lxc/
> > > diff --git a/src/lxc/utils.c b/src/lxc/utils.c
> > > index 7ced314..5940542 100644
> > > --- a/src/lxc/utils.c
> > > +++ b/src/lxc/utils.c
> > > @@ -1466,3 +1466,17 @@ err:
> > >  	close(fd);
> > >  	return ret;
> > >  }
> > > +
> > > +bool lxc_delete_string_in_array(char *haystack, size_t haystacklen,
> > > +				const char *needle, size_t needlelen)
> > > +{
> > > +	char *del = NULL;
> > > +	bool bret = false;
> > > +
> > > +	if ((del = memmem(haystack, haystacklen, needle, needlelen))) {
> > > +		memmove(del, del + needlelen, strlen(del) - needlelen + 1);
> > > +		bret = true;
> > > +	}
> > > +	return bret;
> > > +}
> > > +
> > > diff --git a/src/lxc/utils.h b/src/lxc/utils.h
> > > index ee12dde..715d125 100644
> > > --- a/src/lxc/utils.h
> > > +++ b/src/lxc/utils.h
> > > @@ -243,6 +243,9 @@ extern char *lxc_append_paths(const char *first, const char *second);
> > >  extern bool lxc_string_in_list(const char *needle, const char *haystack, char sep);
> > >  extern char **lxc_string_split(const char *string, char sep);
> > >  extern char **lxc_string_split_and_trim(const char *string, char sep);
> > > +/* Delete a string from a non-null terminated buffer. */
> > > +bool lxc_delete_string_in_array(char *haystack, size_t haystacklen,
> > > +				const char *needle, size_t needlelen);
> > >  
> > >  /* some simple array manipulation utilities */
> > >  typedef void (*lxc_free_fn)(void *);
> > > -- 
> > > 2.5.1
> > > 
> > _______________________________________________
> > lxc-devel mailing list
> > lxc-devel at lists.linuxcontainers.org
> > http://lists.linuxcontainers.org/listinfo/lxc-devel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20150907/ce274e50/attachment.sig>


More information about the lxc-devel mailing list