Hi Bruno,

Thanks for the guidelines. I've written a very rudimentary version of the module. This is a barely working version, but I do not know what else I require to complete the module. I know that this is missing:


1. An autoconf check
2. A way for reallocarray() to be defined within <stdlib.h>
3. Unit tests

Of these, I think I can write the unit tests without much trouble, but for the other two, I don't know how to write them

The glibc version is implemented behind the feature macro, _GNU_SOURCE. I am not sure how that should be implemented into the module in gnulib.

* Bruno Haible <br...@clisp.org> [170808 10:19]:
Hi Darshit,

Glibc 2.26 introduced the reallocarray() function which acts as a safe
realloc counterpart to calloc(). ... If others here agree, I'd like to see this
function within in gnulib.

Yes, a portable replacement of this function belongs in gnulib.
It falls both in the categories "Enhancements of ISO C or POSIX functions" and
"Portable general use facilities" of the gnulib scope [1].

If someone will willing to help me out / give me pointers on what would
be required to implement this within gnulib, I will happily port the
code from glibc to gnulib and submit a completed patch.

Nice! Thanks. You can start out by looking at the implementation of module 
'strsep'
or by reading the chapter "Writing modules" [2].

Bruno

[1] 
https://www.gnu.org/software/gnulib/manual/html_node/Various-Kinds-of-Modules.html
[2] https://www.gnu.org/software/gnulib/manual/html_node/Writing-modules.html



--
Thanking You,
Darshit Shah
PGP Fingerprint: 7845 120B 07CB D8D6 ECE5 FF2B 2A17 43ED A91A 35B6
From c47f7e2a300d9cf4a4163e29bc3329bd9508c9e5 Mon Sep 17 00:00:00 2001
From: Darshit Shah <dar...@gmail.com>
Date: Tue, 8 Aug 2017 11:22:40 +0200
Subject: [PATCH] Add new module reallocarray-gnu

---
 lib/reallocarray.c       | 35 +++++++++++++++++++++++++++++++++++
 modules/reallocarray-gnu | 24 ++++++++++++++++++++++++
 2 files changed, 59 insertions(+)
 create mode 100644 lib/reallocarray.c
 create mode 100644 modules/reallocarray-gnu

diff --git a/lib/reallocarray.c b/lib/reallocarray.c
new file mode 100644
index 000000000..cbdc66626
--- /dev/null
+++ b/lib/reallocarray.c
@@ -0,0 +1,35 @@
+/* reallocarray() function that is glibc compatible.
+
+   Copyright (C) 2017 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program 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 General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* written by Darshit Shah */
+
+#include <config.h>
+
+#include <errno.h>
+
+#include "xalloc-oversized.h"
+#include "realloc.h"
+
+void *
+reallocarray(void *ptr, size_t nmemb, size_t size) {
+    if (xalloc_oversized (nmemb, size)) {
+        errno = ENOMEM;
+        return NULL;
+    }
+    /* We rely on using the semantics of the GNU realloc() function here. */
+    return realloc(ptr, nmemb * size);
+}
diff --git a/modules/reallocarray-gnu b/modules/reallocarray-gnu
new file mode 100644
index 000000000..898e1ff0f
--- /dev/null
+++ b/modules/reallocarray-gnu
@@ -0,0 +1,24 @@
+Description:
+reallocarray() function that is glibc compatible.
+
+Files:
+lib/reallocarray.c
+
+Depends-on:
+xalloc-oversized
+realloc-gnu
+stdlib
+
+configure.ac:
+gl_MODULE_INDICATOR([reallocarray-gnu])
+
+Makefile.am:
+
+Include:
+<stdlib.h>
+
+License:
+GPL
+
+Maintainer:
+Darshit Shah
-- 
2.14.0

Attachment: signature.asc
Description: PGP signature

Reply via email to