[ccan] [PATCH] darray: Fix bug in the darray_remove() macro

Damien Grassart damien at grassart.com
Tue Aug 29 20:08:35 AEST 2017


Sorry about the confusion, I'll resend the whole set.

On Tue, Aug 29, 2017 at 6:54 AM, David Gibson <david at gibson.dropbear.id.au>
wrote:

> On Mon, Aug 28, 2017 at 07:09:35AM +0200, Damien Grassart wrote:
> > The memmove() call should be using the index argument to determine the
> > number of bytes to copy. To be consistent with the rest of the code,
> > we should also not evaluate the index parameter multiple
> > times. Calling this with rand() % arr.size would otherwise generally
> > segfault.
> >
> > Finally, we want to avoid using "index" as an identifier so as to not
> > shadow index(3) in the C library.
>
> Uh.. sorry, I think we're in a state of confusion because applied some
> of the patches then removed them again due to problems discovered
> later.
>
> Can you please rebase on the latest ccan tree and send me the whole
> set of patches as a batch.
>
> >
> > Signed-off-by: Damien Grassart <damien at grassart.com>
> > ---
> >  ccan/darray/darray.h | 11 ++++++-----
> >  1 file changed, 6 insertions(+), 5 deletions(-)
> >
> > diff --git a/ccan/darray/darray.h b/ccan/darray/darray.h
> > index 82726c05..58470fde 100644
> > --- a/ccan/darray/darray.h
> > +++ b/ccan/darray/darray.h
> > @@ -170,8 +170,8 @@ typedef darray(unsigned long)  darray_ulong;
> >               memmove((arr).item+1, (arr).item,
> ((arr).size-1)*sizeof(*(arr).item)); \
> >               (arr).item[0] = (__VA_ARGS__); \
> >       } while(0)
> > -#define darray_insert(arr, index, ...) do { \
> > -             size_t index_ = index; \
> > +#define darray_insert(arr, i, ...) do { \
> > +             size_t index_ = (i); \
> >               darray_resize(arr, (arr).size+1); \
> >               memmove((arr).item+index_+1, (arr).item+index_,
> ((arr).size-index_-1)*sizeof(*(arr).item)); \
> >               (arr).item[index_] = (__VA_ARGS__); \
> > @@ -230,9 +230,10 @@ typedef darray(unsigned long)  darray_ulong;
> >  #define darray_pop(arr) ((arr).item[--(arr).size])
> >  #define darray_pop_check(arr) ((arr).size ? darray_pop(arr) : NULL)
> >  /* Warning, slow: Requires copying all elements after removed item. */
> > -#define darray_remove(arr, index) do { \
> > -     if (index < arr.size-1)    \
> > -             memmove(&(arr).item[index], &(arr).item[index+1],
> ((arr).size-1-i)*sizeof(*(arr).item)); \
> > +#define darray_remove(arr, i) do { \
> > +     size_t index_ = (i); \
> > +     if (index_ < arr.size-1)    \
> > +             memmove(&(arr).item[index_], &(arr).item[index_+1],
> ((arr).size-1-index_)*sizeof(*(arr).item)); \
> >       (arr).size--;  \
> >       } while(0)
> >
>
> --
> David Gibson                    | I'll have my music baroque, and my code
> david AT gibson.dropbear.id.au  | minimalist, thank you.  NOT _the_
> _other_
>                                 | _way_ _around_!
> http://www.ozlabs.org/~dgibson
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ozlabs.org/pipermail/ccan/attachments/20170829/7b8cab0e/attachment.html>


More information about the ccan mailing list