From nobody Fri Oct 03 17:48:32 2025 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4cdbk90pgzz6BHSj; Fri, 03 Oct 2025 17:48:33 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R12" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4cdbk81Z3Lz3TSQ; Fri, 03 Oct 2025 17:48:32 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1759513712; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ORzN5T1OZemTnUICq7Rj7X/Bo8HeofGyciVzji0mbFo=; b=AKqLGyw9hQjVtbAGPKJILy+LaZ7HL5M1xbecb1YPpQD0M5YicOxwkKxkpOMHKxzSXYziRL RWGkFO/V/Kb+jiLibyLACflKMmQtvqDOpbur9qY472mZbZ9JTkAro+2V0gWfoOEGWz1GcH s2aIpfwbvqrHIAoPcEyNkkRq1xKP3Tqlv42rAR9Qu9qqAhI7ZupDQblgEtfdtEss8dBUOB z1cRuJQekbp5Jm6MMMobamZM/M6YmYt+mvd2q7gplxItxbuVJrzbR5UdlrCt5qLtuWGpPF R57NMlN5speV1p87M2W6SC3f+zTdrOjecUOLdRoFw+0MF6WOAeN5CJCD0MLsgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1759513712; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ORzN5T1OZemTnUICq7Rj7X/Bo8HeofGyciVzji0mbFo=; b=e+ZSQCYqjJj9nZeyOo7nu4+BzCWw1RH/ysRLtABM1YKKiDTpFL0cqH8vE/guHTmqiID/+X NIQSPSSoKmmVFjdozS5A57cRtChTnPpzidcbvAuBrbpK1GhIDAcENC9LUq07bWuORZYRpf SNqer0b1KrFrfpDlQTHHVoeFSCtyRE6tkYl47HXS2qEIfFOCX6s8skPtkZYUHczdKxqkXe GMiLGvec05cGh/9N0nU5QkZeS8ahVVfYtQl5Q+qdc0sFi4p4i3Okp9RQzUhnrwRiUH2K2l FljiabircjWnKk9wJnFUlBVrri2EfPIKOhXJ30+Dc5Wqrl2X4skkxq7yxvGWdw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1759513712; a=rsa-sha256; cv=none; b=XPw1zYpJSGJnnYbGKs98n2fUiBMRRlmMbchnaF0U2UPdvGym6UWH7VxVHPGthJiCkg6pKD y/pEevxLCATCxlezd/S5aVLVdLdvFaoWDNkcJ3CW4Zje4UyFsrUQuSc+G9R6SShjNA3O8j U8pTsIDFf82PXVTtawdLiOsJf+OnyrNQ1ql2Z7hY7ATnydnnW5p8RILiHOPmuhWGDR5bwF A+DXDOttpx35x3v3wuhKS5Iecrs13bTdrX8e09FwZTPAGlc5auQaJxutv73+Z2vJ6MRmPr EaGo96QWPCXtBWKAGcX8gH5kJWbAgwnRcPq/UMgk9qpLD6hLOPOdeNbABtDtBg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4cdbk817ZZz14Bn; Fri, 03 Oct 2025 17:48:32 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 593HmWbr060847; Fri, 3 Oct 2025 17:48:32 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 593HmWeI060844; Fri, 3 Oct 2025 17:48:32 GMT (envelope-from git) Date: Fri, 3 Oct 2025 17:48:32 GMT Message-Id: <202510031748.593HmWeI060844@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Robert Clausecker Subject: git: 42664610795b - main - lib/libc: add recallocarray() List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: fuz X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 42664610795bc0a728851ba6223fcf9b93801167 Auto-Submitted: auto-generated The branch main has been updated by fuz: URL: https://cgit.FreeBSD.org/src/commit/?id=42664610795bc0a728851ba6223fcf9b93801167 commit 42664610795bc0a728851ba6223fcf9b93801167 Author: Robert Clausecker AuthorDate: 2025-10-02 14:33:55 +0000 Commit: Robert Clausecker CommitDate: 2025-10-03 17:46:55 +0000 lib/libc: add recallocarray() This function from OpenBSD is a hybrid of reallocarray() and calloc(). It reallocates an array, clearing any newly allocated items. reallocarray() ultimately originates from OpenBSD. The source is taken from lib/libopenbsd, which now no longer has the function unless when bootstrapping (needed for mandoc). Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D52863 --- include/stdlib.h | 2 ++ lib/libc/stdlib/Makefile.inc | 3 +- lib/libc/stdlib/Symbol.map | 4 +++ lib/libc/stdlib/reallocarray.3 | 37 ++++++++++++++++++++++++- lib/{libopenbsd => libc/stdlib}/recallocarray.c | 0 lib/libopenbsd/Makefile | 8 ++++-- 6 files changed, 50 insertions(+), 4 deletions(-) diff --git a/include/stdlib.h b/include/stdlib.h index ba0cf4b5e88e..784cb63bfc5b 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -314,6 +314,8 @@ int radixsort(const unsigned char **, int, const unsigned char *, unsigned); void *reallocarray(void *, size_t, size_t) __result_use_check __alloc_size2(2, 3); +void *recallocarray(void *, size_t, size_t, size_t) __result_use_check + __alloc_size2(3, 4); void *reallocf(void *, size_t) __result_use_check __alloc_size(2); int rpmatch(const char *); char *secure_getenv(const char *); diff --git a/lib/libc/stdlib/Makefile.inc b/lib/libc/stdlib/Makefile.inc index ca199a669be1..e7b9955b9646 100644 --- a/lib/libc/stdlib/Makefile.inc +++ b/lib/libc/stdlib/Makefile.inc @@ -10,7 +10,7 @@ MISRCS+=C99_Exit.c a64l.c abort.c abs.c atexit.c atof.c atoi.c atol.c atoll.c \ insque.c l64a.c labs.c ldiv.c llabs.c lldiv.c lsearch.c \ merge.c mergesort_b.c ptsname.c qsort.c qsort_r.c qsort_r_compat.c \ qsort_s.c quick_exit.c radixsort.c rand.c \ - random.c reallocarray.c reallocf.c realpath.c remque.c \ + random.c reallocarray.c reallocf.c realpath.c recallocarray.c remque.c \ set_constraint_handler_s.c strfmon.c strtoimax.c \ strtol.c strtold.c strtoll.c strtoq.c strtoul.c strtonum.c strtoull.c \ strtoumax.c strtouq.c system.c tdelete.c tfind.c tsearch.c twalk.c @@ -76,6 +76,7 @@ MLINKS+=random.3 initstate.3 \ random.3 srandom.3 \ random.3 srandomdev.3 MLINKS+=radixsort.3 sradixsort.3 +MLINKS+=reallocarray.3 recallocarray.3 MLINKS+=set_constraint_handler_s.3 abort_handler_s.3 MLINKS+=set_constraint_handler_s.3 ignore_handler_s.3 MLINKS+=strfmon.3 strfmon_l.3 diff --git a/lib/libc/stdlib/Symbol.map b/lib/libc/stdlib/Symbol.map index 2b79ca2ece8b..53d71bcafb7d 100644 --- a/lib/libc/stdlib/Symbol.map +++ b/lib/libc/stdlib/Symbol.map @@ -131,6 +131,10 @@ FBSD_1.8 { getenv_r; }; +FBSD_1.9 { + recallocarray; +}; + FBSDprivate_1.0 { __system; _system; diff --git a/lib/libc/stdlib/reallocarray.3 b/lib/libc/stdlib/reallocarray.3 index 80035c67a497..9a2ab5c7a840 100644 --- a/lib/libc/stdlib/reallocarray.3 +++ b/lib/libc/stdlib/reallocarray.3 @@ -26,7 +26,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd May 1, 2015 +.Dd October 2, 2025 .Dt REALLOCARRAY 3 .Os .Sh NAME @@ -38,6 +38,8 @@ .In stdlib.h .Ft void * .Fn reallocarray "void *ptr" "size_t nmemb" "size_t size" +.Ft void * +.Fn recallocarray "void *ptr" "size_t oldnmeb" "size_t nmemb" size_t size" .Sh DESCRIPTION The .Fn reallocarray @@ -52,6 +54,33 @@ and checks for integer overflow in the calculation .Fa nmemb * .Fa size . +.Pp +The +.Fn recallocarray +function is similar to the +.Fn reallocarray +function +except it ensures newly allocated memory is cleared similar to +.Fn calloc . +If +.Fa ptr +is +.Dv NULL , +.Fa oldnmemb +is ignored and the call is equivalent to +.Fn calloc . +If +.Fa ptr +is not +.Dv NULL , +.Fa oldnmemb +must be a value such that +.Fa oldnmemb +* +.Fa size +is the size of the earlier allocation that returned +.Fa ptr , +otherwise the behaviour is undefined. .Sh RETURN VALUES The .Fn reallocarray @@ -142,3 +171,9 @@ function first appeared in .Ox 5.6 and .Fx 11.0 . +The +.Fn recallocarray +function first appeared in +.Ox 6.1 +and +.Fx 16.0 . diff --git a/lib/libopenbsd/recallocarray.c b/lib/libc/stdlib/recallocarray.c similarity index 100% rename from lib/libopenbsd/recallocarray.c rename to lib/libc/stdlib/recallocarray.c diff --git a/lib/libopenbsd/Makefile b/lib/libopenbsd/Makefile index 53bd0200934f..80ae0f90621a 100644 --- a/lib/libopenbsd/Makefile +++ b/lib/libopenbsd/Makefile @@ -1,8 +1,12 @@ LIB= openbsd SRCS= imsg-buffer.c \ imsg.c \ - ohash.c \ - recallocarray.c + ohash.c +.if defined(BOOTSTRAPPING) +.PATH: ${SRCTOP}/lib/libc/stdlib +SRCS+= recallocarray.c +.endif + .if !defined(BOOTSTRAPPING) # Skip getdtablecount.c when bootstrapping since it doesn't compile for Linux # and is not used by any of the bootstrap tools