summaryrefslogtreecommitdiff
blob: dbfae4dbe9f3a86243ad46c2183e7f3c52ef9828 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
From 7d0638e792ebe528af3f6d8e353eff68157d7105 Mon Sep 17 00:00:00 2001
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date: Wed, 1 Mar 2023 14:41:23 -0300
Subject: [PATCH 2/3] support: Add xreallocarray

As a wrapper over reallocarray.
---
 support/Makefile        |  1 +
 support/support.h       |  2 ++
 support/xreallocarray.c | 29 +++++++++++++++++++++++++++++
 3 files changed, 32 insertions(+)
 create mode 100644 support/xreallocarray.c

diff --git a/support/Makefile b/support/Makefile
index 362a51f882..d056563c15 100644
--- a/support/Makefile
+++ b/support/Makefile
@@ -201,6 +201,7 @@ libsupport-routines = \
   xread \
   xreadlink \
   xrealloc \
+  xreallocarray \
   xrecvfrom \
   xsendto \
   xsetlocale \
diff --git a/support/support.h b/support/support.h
index ba21ec9b5a..b4e31e4483 100644
--- a/support/support.h
+++ b/support/support.h
@@ -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
index 0000000000..74fdaa421b
--- /dev/null
+++ b/support/xreallocarray.c
@@ -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;
+}
-- 
2.41.0