[PATCH 06/17] Add no-fail equivalents of allocarray & reallocarray

Alan Coopersmith alan.coopersmith at oracle.com
Tue Mar 31 17:50:19 PDT 2015


Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
---
 include/os.h |    9 +++++++++
 os/utils.c   |   10 ++++++++++
 2 files changed, 19 insertions(+)

diff --git a/include/os.h b/include/os.h
index 2633741..bc624a2 100644
--- a/include/os.h
+++ b/include/os.h
@@ -76,6 +76,8 @@ typedef struct _NewClientRec *NewClientPtr;
 #define xnfstrdup(s) XNFstrdup(s)
 
 #define xallocarray(num, size) reallocarray(NULL, (num), (size))
+#define xnfallocarray(num, size) XNFreallocarray(NULL, (num), (size))
+#define xnfreallocarray(ptr, num, size) XNFreallocarray((ptr), (num), (size))
 #endif
 
 #include <stdio.h>
@@ -241,6 +243,13 @@ extern _X_EXPORT void *
 XNFrealloc(void * /*ptr */ , unsigned long /*amount */ );
 
 /*
+ * This function reallocarrays(3)s passed buffer, terminating the server if
+ * there is not enough memory or the arguments overflow when multiplied.
+ */
+extern _X_EXPORT void *
+XNFreallocarray(void *ptr, size_t nmemb, size_t size);
+
+/*
  * This function strdup(3)s passed string. The only difference from the library
  * function that it is safe to pass NULL, as NULL will be returned.
  */
diff --git a/os/utils.c b/os/utils.c
index 24a8751..23f4117 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -1158,6 +1158,16 @@ XNFrealloc(void *ptr, unsigned long amount)
     return ret;
 }
 
+void *
+XNFreallocarray(void *ptr, size_t nmemb, size_t size)
+{
+    void *ret = reallocarray(ptr, nmemb, size);
+
+    if (!ret)
+        FatalError("XNFreallocarray: Out of memory");
+    return ret;
+}
+
 char *
 Xstrdup(const char *s)
 {
-- 
1.7.9.2



More information about the xorg-devel mailing list