From nobody Sun Jul 6 23:18:43 2025 X-Original-To: dev-commits-src-all@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 4bb3GF5Z1xz60Zw4; Sun, 06 Jul 2025 23:18:45 +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 "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4bb3GD1ggCz3tR5; Sun, 06 Jul 2025 23:18:43 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1751843924; 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=i9wYzVcNDC9EX+ZuTEVA4SuNrxC2Pm3kzVjGEZKQams=; b=Fm/iq/oonCkwoHGNHSRAsmevZqCSb6JiAJ02rNyUYlCwDduiSDwu2fYzrSDOHo1GnhAuB2 txOHZHJtKLpEPZ9Lw92ITi7K6jvrnxXeHoU4vxqLeCukODK/qtUsDGIjDsJq1kB3sv6C/g k8lheq2cvA9sXI+5aJR9flROxGnTm3s5PB6i9ut4U4juZTQFw5zDz18Vwabo/v3Wpm1TVw uB4UwmnbtszFpJPWUIbk7unfCR8h9EtiO15L0q69EGlJ1c8+eWN7VUnNXq5Qg4m0iNSq+o eWhDZj9nPsGgGcJtjR1eOF8YsHxQiyysd5ZZeRc4rsEwzvRpryjDsV2TAg8qeQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1751843924; 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=i9wYzVcNDC9EX+ZuTEVA4SuNrxC2Pm3kzVjGEZKQams=; b=PIWvKnalU41H68qdUr6ZvqCH3b1/dov9gYmc9gqiIU9eP/zS4HrggWD87pNqbBLXnSxQ+N 3bDh+4hYa3tXcmjpp899LOzGUfLMaeLNymw+U2tbPVtIAQAjMGUe4+jijcVE8yEtcnaxYz f5uoZr8/oRBglkWvW/xQZy1X+gEu84O42HcUwZd4LMOJxxMLwIP6JRxZsh7DfVqZ+jxsbH xKuds7YxkFXa6CoPTlBeEnPEL+/PewU8KMrOqijR7zQdF6IqVBnmrKxrzPD+rw/Mc8MnrZ yYdM2N3NlSzdaFDwhe1ciQ/p/9SD2aG3WeO5OcEQGtTURGdDVWdvYVY7Kmi++w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1751843924; a=rsa-sha256; cv=none; b=q3O8scGXZwxkwohzmiffAPMqLSlj1q1Z80OmnJw0lAR6UXD9Dy3lCdY10msnXYyKMpPpmA DlYC3e2qnmGUV4G/iDD/rxVaP7Y2mw2BI0bdKR1b37ROyaLicdwLNzRzIrzIlf80zLqtkI esTJMv7ug0M8tRkR1TNLR51cbcPfpVlA7BAscPxaEZWASWvF1PAW7Rca6s2+XVajhLGN0r 6XgTzbXieE2jp8eCMCHKUk80YZOKf9NoRBFroJJTQz2S95+u74JATyj8isPXDMQ9bgszGo eI1KZD5fYVWCbCi50/a0q2fG3RMGdo/h1fE/RKSckd/0p2mPX8CJ0iKRE/VkXQ== 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 4bb3GC11yPz996; Sun, 06 Jul 2025 23:18:43 +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 566NIhgn031308; Sun, 6 Jul 2025 23:18:43 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 566NIhSM031303; Sun, 6 Jul 2025 23:18:43 GMT (envelope-from git) Date: Sun, 6 Jul 2025 23:18:43 GMT Message-Id: <202507062318.566NIhSM031303@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 71a0af25a535 - main - libopenbsd: Add recallocarray() List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 71a0af25a5353f929521cb04c73a7266ac5b7b61 Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=71a0af25a5353f929521cb04c73a7266ac5b7b61 commit 71a0af25a5353f929521cb04c73a7266ac5b7b61 Author: Ricardo Branco AuthorDate: 2025-06-16 21:41:48 +0000 Commit: Mark Johnston CommitDate: 2025-07-06 23:09:00 +0000 libopenbsd: Add recallocarray() Reviewed by: kib MFC after: 1 month Pull Request: https://github.com/freebsd/freebsd-src/pull/1698 --- lib/libopenbsd/Makefile | 3 +- lib/libopenbsd/recallocarray.c | 82 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/lib/libopenbsd/Makefile b/lib/libopenbsd/Makefile index 675ed476c51d..dca1c08b0aed 100644 --- a/lib/libopenbsd/Makefile +++ b/lib/libopenbsd/Makefile @@ -2,7 +2,8 @@ PACKAGE=lib${LIB} LIB= openbsd SRCS= imsg-buffer.c \ imsg.c \ - ohash.c + ohash.c \ + recallocarray.c .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 diff --git a/lib/libopenbsd/recallocarray.c b/lib/libopenbsd/recallocarray.c new file mode 100644 index 000000000000..11e1fda744c7 --- /dev/null +++ b/lib/libopenbsd/recallocarray.c @@ -0,0 +1,82 @@ +/* $OpenBSD: recallocarray.c,v 1.1 2017/03/06 18:44:21 otto Exp $ */ +/* + * Copyright (c) 2008, 2017 Otto Moerbeek + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include + +/* + * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX + * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW + */ +#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4)) + +void *recallocarray(void *, size_t, size_t, size_t); + +void * +recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size) +{ + size_t oldsize, newsize; + void *newptr; + + if (ptr == NULL) + return calloc(newnmemb, size); + + if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && + newnmemb > 0 && SIZE_MAX / newnmemb < size) { + errno = ENOMEM; + return NULL; + } + newsize = newnmemb * size; + + if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && + oldnmemb > 0 && SIZE_MAX / oldnmemb < size) { + errno = EINVAL; + return NULL; + } + oldsize = oldnmemb * size; + + /* + * Don't bother too much if we're shrinking just a bit, + * we do not shrink for series of small steps, oh well. + */ + if (newsize <= oldsize) { + size_t d = oldsize - newsize; + + if (d < oldsize / 2 && d < (size_t)getpagesize()) { + memset((char *)ptr + newsize, 0, d); + return ptr; + } + } + + newptr = malloc(newsize); + if (newptr == NULL) + return NULL; + + if (newsize > oldsize) { + memcpy(newptr, ptr, oldsize); + memset((char *)newptr + oldsize, 0, newsize - oldsize); + } else + memcpy(newptr, ptr, newsize); + + explicit_bzero(ptr, oldsize); + free(ptr); + + return newptr; +}