This is the mail archive of the
mailing list for the glibc project.
Re: [RFC][PATCH v2] Add reallocarray function.
- From: Rich Felker <dalias at libc dot org>
- To: "Joseph S. Myers" <joseph at codesourcery dot com>
- Cc: RÃdiger Sonderfeld <ruediger at c-plusplus dot de>, Paul Eggert <eggert at cs dot ucla dot edu>, libc-alpha at sourceware dot org
- Date: Mon, 19 May 2014 22:01:08 -0400
- Subject: Re: [RFC][PATCH v2] Add reallocarray function.
- Authentication-results: sourceware.org; auth=none
- References: <5379208F dot 8030000 at cs dot ucla dot edu> <2080621 dot 6fAB4UMNoY at descartes> <Pine dot LNX dot 4 dot 64 dot 1405191501120 dot 25418 at digraph dot polyomino dot org dot uk>
On Mon, May 19, 2014 at 03:02:52PM +0000, Joseph S. Myers wrote:
> On Sun, 18 May 2014, R?diger Sonderfeld wrote:
> > diff --git a/stdlib/stdlib.h b/stdlib/stdlib.h
> > index 00329a2..b75c28f 100644
> > --- a/stdlib/stdlib.h
> > +++ b/stdlib/stdlib.h
> > @@ -479,6 +479,13 @@ extern void *calloc (size_t __nmemb, size_t __size)
> > between objects pointed by the old and new pointers. */
> > extern void *realloc (void *__ptr, size_t __size)
> > __THROW __attribute_warn_unused_result__;
> > +/* Re-allocate the previously allocated block in PTR, making the new
> > + block large enough for NMEMB elements of SIZE bytes each. */
> > +/* __attribute_malloc__ is not used, because if realloc returns
> > + the same pointer that was passed to it, aliasing needs to be allowed
> > + between objects pointed by the old and new pointers. */
> > +extern void *reallocarray (void *__ptr, size_t __nmemb, size_t __size)
> > + __THROW __attribute_warn_unused_result__;
> How has this patch been tested? This function declaration needs
> conditioning on __USE_GNU, as no standard includes this function in
> <stdlib.h>; I'd have expected you to get failures of the conform/ tests
> for stdlib.h when you ran the testsuite.
Not really related to this patch, but I think the comment about
__attribute_malloc__ that was copied from realloc is wrong. The new
and old pointers cannot alias because, if realloc succeeds (and I
would expect reallocarray to behave the same) the _value_ of the old
pointer is indeterminate and accessing it (e.g. comparing it with new)
invokes undefined behavior.