Bug 475498 - Add reallocarray wrapper
Summary: Add reallocarray wrapper
Status: RESOLVED FIXED
Alias: None
Product: valgrind
Classification: Developer tools
Component: general (other bugs)
Version First Reported In: 3.22 GIT
Platform: Other FreeBSD
: NOR normal
Target Milestone: ---
Assignee: Julian Seward
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-10-12 05:15 UTC by Paul Floyd
Modified: 2023-11-09 07:25 UTC (History)
0 users

See Also:
Latest Commit:
Version Fixed/Implemented In:
Sentry Crash Report:


Attachments
initial patch (15.57 KB, patch)
2023-11-08 06:55 UTC, Paul Floyd
Details

Note You need to log in before you can comment on or make changes to this bug.
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