Bug 475498

Summary: Add reallocarray wrapper
Product: [Developer tools] valgrind Reporter: Paul Floyd <pjfloyd>
Component: generalAssignee: Julian Seward <jseward>
Status: RESOLVED FIXED    
Severity: normal    
Priority: NOR    
Version First Reported In: 3.22 GIT   
Target Milestone: ---   
Platform: Other   
OS: FreeBSD   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:
Attachments: initial patch

Description Paul Floyd 2023-10-12 05:15:01 UTC
Currently there is no wrapper for this function.

As long as implementations just check for overflow and call realloc we are OK since we will intercept that. It's not safe to assume that and implementations could directly call their internal functions to handle the call.

This was mentioned in https://bugs.kde.org/show_bug.cgi?id=407589 but I only fixed the aligned_alloc part.
Comment 1 Paul Floyd 2023-10-12 05:26:57 UTC
Also adding a wrapper will slightly improve error messages.

The FreeBSD implementation is

void *
reallocarray(void *optr, size_t nmemb, size_t size)
{

        if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
            nmemb > 0 && SIZE_MAX / nmemb < size) {
                errno = ENOMEM;
                return (NULL);
        }
        return (realloc(optr, size * nmemb));
}

Illumos uses the same source (from OpenBSD)

musl and GNU libc do the same thing but without the square root of MAX_SIZE checks to avoid the division.
Comment 2 Paul Floyd 2023-11-08 06:55:29 UTC
Created attachment 162942 [details]
initial patch

Linux expected not yet generated
Comment 3 Paul Floyd 2023-11-09 07:25:22 UTC
commit 993cdf80a15ecf2cdaaa10a120cb29949f22605f (HEAD -> master, origin/master, origin/HEAD, bug475498)
Author: Paul Floyd <pjfloyd@wanadoo.fr>
Date:   Thu Nov 9 07:58:02 2023 +0100

    Bug 475498 - Add reallocarray wrapper