[v6,2/3] support: Add xreallocarray

Message ID 20230302145732.2293756-3-adhemerval.zanella@linaro.org
State Superseded
Headers
Series Fix opendir regression on some FS |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent

Commit Message

Adhemerval Zanella March 2, 2023, 2:57 p.m. UTC
  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
  

Comments

Florian Weimer March 10, 2023, 4:49 p.m. UTC | #1
* Adhemerval Zanella:

> +void *
> +xreallocarray (void *p, size_t n, size_t s)
> +{
> +  void *r = reallocarray (p, n, s);
> +  if (r == NULL)
> +    oom_error ("reallocarray", n);
> +  return r;
> +}

Isn't the failure condition more complicated?  See xrealloc.

Thanks,
Florian
  
Adhemerval Zanella March 10, 2023, 6:44 p.m. UTC | #2
On 10/03/23 13:49, Florian Weimer wrote:
> * Adhemerval Zanella:
> 
>> +void *
>> +xreallocarray (void *p, size_t n, size_t s)
>> +{
>> +  void *r = reallocarray (p, n, s);
>> +  if (r == NULL)
>> +    oom_error ("reallocarray", n);
>> +  return r;
>> +}
> 
> Isn't the failure condition more complicated?  See xrealloc.

Indeed, I will update the patch.
  
Paul Eggert March 10, 2023, 9:21 p.m. UTC | #3
On 2023-03-10 10:44, Adhemerval Zanella Netto wrote:
> 
> 
> On 10/03/23 13:49, Florian Weimer wrote:
>> * Adhemerval Zanella:
>>
>>> +void *
>>> +xreallocarray (void *p, size_t n, size_t s)
>>> +{
>>> +  void *r = reallocarray (p, n, s);
>>> +  if (r == NULL)
>>> +    oom_error ("reallocarray", n);
>>> +  return r;
>>> +}
>>
>> Isn't the failure condition more complicated?  See xrealloc.
> 
> Indeed, I will update the patch.

You can steal the source code from Gnulib, which already has 
xreallocarray in lib/xmalloc.c.

(Gnulib also has an xireallocarray which is better if you're worried 
about integer overflow, but one step at a time.)
  

Patch

diff --git a/support/Makefile b/support/Makefile
index a304c5cdc0..1ca4a9567e 100644
--- a/support/Makefile
+++ b/support/Makefile
@@ -191,6 +191,7 @@  libsupport-routines = \
   xraise \
   xreadlink \
   xrealloc \
+  xreallocarray \
   xrecvfrom \
   xsendto \
   xsetlocale \
diff --git a/support/support.h b/support/support.h
index 525ff1ebce..741e4c5c2a 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..6478725c9c
--- /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)
+    oom_error ("reallocarray", n);
+  return r;
+}