This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [RFC][PATCH] Add reallocarray function.
- From: Florian Weimer <fweimer at redhat dot com>
- To: RÃdiger Sonderfeld <ruediger at c-plusplus dot de>, libc-alpha at sourceware dot org
- Date: Mon, 01 Sep 2014 17:48:38 +0200
- Subject: Re: [RFC][PATCH] Add reallocarray function.
- Authentication-results: sourceware.org; auth=none
- References: <5040375 dot QgetdG6juS at descartes>
On 05/18/2014 10:32 PM, RÃdiger Sonderfeld wrote:
+/* 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__;
I'm not sure if this is still on the table, but experience shows that
the realloc interface is error-prone for another reason: The straight
way to write an a reallocation,
ptr = realloc(ptr, new_size);
leads to a memory leak on error. It would be less error-prone to have
reallocarray to update the pointer directly on success, e.g.:
if (reallocarray(&ptr, new_count, sizeof(T)) < 0) {
// handle error
}
However, this cannot be implemented as a C function, only as a macro.
--
Florian Weimer / Red Hat Product Security