]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
support: Add xreallocarray
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 1 Mar 2023 17:41:23 +0000 (14:41 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Fri, 29 Dec 2023 13:12:17 +0000 (10:12 -0300)
As a wrapper over reallocarray.

support/Makefile
support/support.h
support/xreallocarray.c [new file with mode: 0644]

index 9aa7f23a6e4b89d495f611dd532eb5ae1b7a5053..25233bcf8f6bd9cccc00f00b6e78ba5d869e27e4 100644 (file)
@@ -201,6 +201,7 @@ libsupport-routines = \
   xread \
   xreadlink \
   xrealloc \
+  xreallocarray \
   xrecvfrom \
   xsendto \
   xsetlocale \
index 4a068d3aeea8cb6fe0ce801f43e2f20f34237602..31cea1df17c09244b6624070b7218af3fddbd7f6 100644 (file)
@@ -107,6 +107,8 @@ extern void *xcalloc (size_t n, size_t s)
   __returns_nonnull;
 extern void *xrealloc (void *o, size_t n)
   __attribute_malloc__ __attribute_alloc_size__ ((2)) __attr_dealloc_free;
+extern void *xreallocarray (void *p, size_t n, size_t s)
+  __attribute_alloc_size__ ((2, 3)) __attr_dealloc_free;
 extern char *xstrdup (const char *) __attribute_malloc__ __attr_dealloc_free
   __returns_nonnull;
 void *xposix_memalign (size_t alignment, size_t n)
diff --git a/support/xreallocarray.c b/support/xreallocarray.c
new file mode 100644 (file)
index 0000000..74fdaa4
--- /dev/null
@@ -0,0 +1,29 @@
+/* Error-checking wrapper for reallocarray
+   Copyright (C) 2016-2023 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <stdlib.h>
+#include <support/support.h>
+
+void *
+xreallocarray (void *p, size_t n, size_t s)
+{
+  void *r = reallocarray (p, n, s);
+  if (r == NULL && (p == NULL || (n != 0 && s != 0)))
+    oom_error ("reallocarray", n);
+  return r;
+}