The branch main has been updated by markj:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=71a0af25a5353f929521cb04c73a7266ac5b7b61

commit 71a0af25a5353f929521cb04c73a7266ac5b7b61
Author:     Ricardo Branco <rbra...@suse.de>
AuthorDate: 2025-06-16 21:41:48 +0000
Commit:     Mark Johnston <ma...@freebsd.org>
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 <o...@drijf.net>
+ *
+ * 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 <errno.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+
+/*
+ * 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;
+}

Reply via email to