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

Serge Hallyn serge.hallyn at ubuntu.com
Mon Sep 7 17:03:37 UTC 2015


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?

> > 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


More information about the lxc-devel mailing list