[Mesa-dev] [PATCH 2/3] u_dynarray: return 0 on realloc failure

Caio Marcelo de Oliveira Filho caio.oliveira at intel.com
Sat May 4 23:24:48 UTC 2019


Hi,

> > diff --git a/src/util/u_dynarray.h b/src/util/u_dynarray.h
> > index b30fd7b1154..f6a81609dbe 100644
> > --- a/src/util/u_dynarray.h
> > +++ b/src/util/u_dynarray.h
> > @@ -85,20 +85,22 @@ util_dynarray_ensure_cap(struct util_dynarray *buf, unsigned newcap)
> >           buf->capacity = DYN_ARRAY_INITIAL_SIZE;
> >
> >        while (newcap > buf->capacity)
> >           buf->capacity *= 2;
> >
> >        if (buf->mem_ctx) {
> >           buf->data = reralloc_size(buf->mem_ctx, buf->data, buf->capacity);
> >        } else {
> >           buf->data = realloc(buf->data, buf->capacity);
> >        }
> > +      if (!buf->data)
> > +         return 0;
> 
> To keep buf->data valid, put the new value in a temporary variable and
> copy it into buf->data on success. If realloc and reralloc_size fail,
> the original pointer is still valid, while if we overwrite buf->data
> we are guaranteed to leak the data on failure.

You also want to use a temporary variable for capacity.  If realloc
fails and we keep the old data, we also want to keep the old capacity.


	Caio


More information about the mesa-dev mailing list