New function xalloc_oversized.
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 27 Oct 2003 08:00:26 +0000 (08:00 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 27 Oct 2003 08:00:26 +0000 (08:00 +0000)
lib/ChangeLog
lib/xalloc.h
lib/xmalloc.c

index 90936e109d94c8984c73bf513305e64b7181d328..d88f08fa9bde10a1884b0fe37ea02566f1423574 100644 (file)
@@ -1,3 +1,26 @@
+2003-10-26  Paul Eggert  <eggert@twinsun.com>
+
+       * xalloc.h (xalloc_oversized): New static inline function, for
+       callers that want to do their own size-overflow checking.  Include
+       <stdbool.h>, since xalloc_oversized returns bool.
+
+       Add two functions x2realloc, x2nrealloc, for programs that grow
+       arrays dynamically by doubling their sizes.
+       * xalloc.h (x2realloc, x2nrealloc): New decls.
+       * xmalloc.c (x2nrealloc_inline, x2nrealloc, x2realloc):
+       New functions.
+
+       Port to C99 semantics for 'inline' of external functions.
+       Bug reported by Bruno Haible.
+       * xmalloc.c (xnmalloc_inline): New static inline function,
+       with the old contents of xnmalloc.
+       (xnmalloc, xmalloc): Use it.
+       (xnrealloc_inline): New static inline function,
+       with the old contents of xnrealloc.
+       (xnrealloc, xrealloc): Use it.
+
+       * alloc.c (alloca): xmalloc cannot return NULL, so don't test for that.
+
 2003-10-25  Paul Eggert  <eggert@twinsun.com>
 
        Fix several address-calculation bugs in the hash modules,
index b07e10c79a0a7be265038c6d1faaeddf913b150b..5e81b00e39955ebc724e24e1feb9cc469e07cf4c 100644 (file)
@@ -20,6 +20,7 @@
 #ifndef XALLOC_H_
 # define XALLOC_H_
 
+# include <stdbool.h>
 # include <stddef.h>
 
 # ifndef __attribute__
@@ -59,6 +60,16 @@ void *x2nrealloc (void *p, size_t *pn, size_t s);
 void *xclone (void const *p, size_t s);
 char *xstrdup (const char *str);
 
+/* Return true if an array of N objects, each of size S, cannot exist
+   due to size arithmetic overflow.  S must be nonzero.  */
+
+static inline bool
+xalloc_oversized (size_t n, size_t s)
+{
+  size_t size_max = -1;
+  return size_max / s < n;
+}
+
 /* These macros are deprecated; they will go away soon, and are retained
    temporarily only to ease conversion to the functions described above.  */
 # define CCLONE(p, n) xclone (p, (n) * sizeof *(p))
index caa5ed895248e3f58098326cf9d117de79a8b894..20d85d83aa5750d15b520dbbc534edaf11023eba 100644 (file)
@@ -23,7 +23,6 @@
 
 #include "xalloc.h"
 
-#include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
 
 /* If non NULL, call this function when memory is exhausted. */
 void (*xalloc_fail_func) (void) = 0;
 
-/* Return true if array of N objects, each of size S, cannot exist due
-   to arithmetic overflow.  S must be nonzero.  */
-
-static inline bool
-array_size_overflow (size_t n, size_t s)
-{
-  return SIZE_MAX / s < n;
-}
-
 /* If XALLOC_FAIL_FUNC is NULL, or does return, display this message
    before exiting when memory is exhausted.  Goes through gettext. */
 char const xalloc_msg_memory_exhausted[] = N_("memory exhausted");
@@ -81,7 +71,7 @@ static inline void *
 xnmalloc_inline (size_t n, size_t s)
 {
   void *p;
-  if (array_size_overflow (n, s) || ! (p = malloc (n * s)))
+  if (xalloc_oversized (n, s) || ! (p = malloc (n * s)))
     xalloc_die ();
   return p;
 }
@@ -106,7 +96,7 @@ xmalloc (size_t n)
 static inline void *
 xnrealloc_inline (void *p, size_t n, size_t s)
 {
-  if (array_size_overflow (n, s) || ! (p = realloc (p, n * s)))
+  if (xalloc_oversized (n, s) || ! (p = realloc (p, n * s)))
     xalloc_die ();
   return p;
 }
@@ -249,7 +239,7 @@ xcalloc (size_t n, size_t s)
   void *p;
   /* Test for overflow, since some calloc implementations don't have
      proper overflow checks.  */
-  if (array_size_overflow (n, s) || ! (p = calloc (n, s)))
+  if (xalloc_oversized (n, s) || ! (p = calloc (n, s)))
     xalloc_die ();
   return p;
 }