bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-gnulib] new xalloc_oversized macro in xalloc.h


From: Paul Eggert
Subject: [Bug-gnulib] new xalloc_oversized macro in xalloc.h
Date: 27 Oct 2003 00:15:23 -0800
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3

Bruno Haible <address@hidden> writes:

> Can you put the array_size_overflow macro or inline function in a
> public header file? xalloc.h or a different one, I don't mind.

OK, but I think the name should change if it's public, since
"array_size_overflow" is too generic.  I installed the following
change to have xalloc.h define a new inline function "xalloc_oversized".

2003-10-26  Paul Eggert  <address@hidden>

        * m4/xalloc.m4 (gl_XALLOC): Require AC_C_INLINE, since xalloc.h
        now uses inline.
        * lib/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.
        * lib/xalloc.c (array_size_overflow): Remove.  All callers changed
        to use xalloc_oversized.

Index: m4/xalloc.m4
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/xalloc.m4,v
retrieving revision 1.4
diff -p -u -r1.4 xalloc.m4
--- m4/xalloc.m4        13 Oct 2003 06:07:11 -0000      1.4
+++ m4/xalloc.m4        27 Oct 2003 08:05:55 -0000
@@ -1,4 +1,4 @@
-# xalloc.m4 serial 4
+# xalloc.m4 serial 5
 dnl Copyright (C) 2002-2003 Free Software Foundation, Inc.
 dnl This file is free software, distributed under the terms of the GNU
 dnl General Public License.  As a special exception to the GNU General
@@ -8,6 +8,7 @@ dnl the same distribution terms as the r
 
 AC_DEFUN([gl_XALLOC],
 [
+  AC_REQUIRE([AC_C_INLINE])
   gl_PREREQ_XMALLOC
   gl_PREREQ_XSTRDUP
 ])
Index: lib/xalloc.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/xalloc.h,v
retrieving revision 1.18
diff -p -u -r1.18 xalloc.h
--- lib/xalloc.h        27 Oct 2003 07:28:24 -0000      1.18
+++ lib/xalloc.h        27 Oct 2003 07:57:54 -0000
@@ -20,6 +20,7 @@
 #ifndef XALLOC_H_
 # define XALLOC_H_
 
+# include <stdbool.h>
 # include <stddef.h>
 
 # ifndef __attribute__
@@ -58,6 +59,16 @@ void *x2realloc (void *p, size_t *pn);
 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.  */
Index: lib/xmalloc.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/xmalloc.c,v
retrieving revision 1.30
diff -p -u -r1.30 xmalloc.c
--- lib/xmalloc.c       27 Oct 2003 07:28:24 -0000      1.30
+++ lib/xmalloc.c       27 Oct 2003 07:57:54 -0000
@@ -23,7 +23,6 @@
 
 #include "xalloc.h"
 
-#include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -49,15 +48,6 @@
 /* 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;
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]