[RFC][PATCH] Add reallocarray function.
Florian Weimer
fweimer@redhat.com
Mon Sep 1 15:48:00 GMT 2014
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
More information about the Libc-alpha
mailing list