summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/ChangeLog7
-rw-r--r--lib/calloc.c10
-rw-r--r--lib/malloc.c2
-rw-r--r--lib/realloc.c15
-rw-r--r--m4/ChangeLog5
-rw-r--r--m4/calloc.m435
6 files changed, 57 insertions, 17 deletions
diff --git a/lib/ChangeLog b/lib/ChangeLog
index 8fc22f032..64bc023f1 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,10 @@
+2004-11-17 Paul Eggert <eggert@cs.ucla.edu>
+
+ * realloc.c (rpl_realloc): Call 'free' if n==0, since realloc
+ might fail. Problem reported by Yoann Vandoorselaere.
+ * calloc.c (rpl_calloc): Defend against buggy calloc implementations
+ that mishandle size_t overflow.
+
2004-11-16 Paul Eggert <eggert@cs.ucla.edu>
* xgetcwd.c: Include <limits.h>, for PATH_MAX.
diff --git a/lib/calloc.c b/lib/calloc.c
index 2a9e6fa20..1ff0f1c0b 100644
--- a/lib/calloc.c
+++ b/lib/calloc.c
@@ -1,4 +1,4 @@
-/* Work around the condition whereby calloc (n, s) fails when n*s is 0.
+/* calloc() function that is glibc compatible.
This wrapper function is required at least on Tru64 UNIX 5.1.
Copyright (C) 2004 Free Software Foundation, Inc.
@@ -31,9 +31,17 @@
void *
rpl_calloc (size_t n, size_t s)
{
+ size_t bytes;
if (n == 0)
n = 1;
if (s == 0)
s = 1;
+
+ /* Defend against buggy calloc implementations that mishandle
+ size_t overflow. */
+ bytes = n * s;
+ if (bytes / s != n)
+ return NULL;
+
return calloc (n, s);
}
diff --git a/lib/malloc.c b/lib/malloc.c
index a43d16927..ca04c57cd 100644
--- a/lib/malloc.c
+++ b/lib/malloc.c
@@ -1,4 +1,4 @@
-/* Work around bug on some systems where malloc (0) fails.
+/* malloc() function that is glibc compatible.
Copyright (C) 1997, 1998 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
diff --git a/lib/realloc.c b/lib/realloc.c
index ccbf99138..14fec075f 100644
--- a/lib/realloc.c
+++ b/lib/realloc.c
@@ -1,5 +1,5 @@
-/* Work around bug on some systems where realloc (NULL, 0) fails.
- Copyright (C) 1997, 2003 Free Software Foundation, Inc.
+/* realloc() function that is glibc compatible.
+ Copyright (C) 1997, 2003, 2004 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
@@ -32,8 +32,15 @@ void *
rpl_realloc (void *p, size_t n)
{
if (n == 0)
- n = 1;
- if (p == 0)
+ {
+ n = 1;
+
+ /* In theory realloc might fail, so don't rely on it to free. */
+ free (p);
+ p = NULL;
+ }
+
+ if (p == NULL)
return malloc (n);
return realloc (p, n);
}
diff --git a/m4/ChangeLog b/m4/ChangeLog
index 6b030b204..6cd8f6a15 100644
--- a/m4/ChangeLog
+++ b/m4/ChangeLog
@@ -1,3 +1,8 @@
+2004-11-17 Paul Eggert <eggert@cs.ucla.edu>
+
+ * calloc.m4 (_AC_FUNC_CALLOC_IF): Check for buggy calloc implementations
+ that mishandle size_t overflow.
+
2004-11-16 Paul Eggert <eggert@cs.ucla.edu>
* canon-host.m4 (gl_CANON_HOST): Check for getaddrinfo.
diff --git a/m4/calloc.m4 b/m4/calloc.m4
index 5d03ed271..f244df7b2 100644
--- a/m4/calloc.m4
+++ b/m4/calloc.m4
@@ -1,6 +1,25 @@
-#serial 2
+# calloc.m4 serial 3
-# Determine whether calloc (N, S) returns non-NULL when N*S is zero.
+# Copyright (C) 2004 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 2, 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, write to the Free Software Foundation,
+# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+# Written by Jim Meyering.
+
+# Determine whether calloc (N, S) returns non-NULL when N*S is zero,
+# and returns NULL when N*S overflows.
# If so, define HAVE_CALLOC. Otherwise, define calloc to rpl_calloc
# and arrange to use a calloc wrapper function that does work in that case.
@@ -9,17 +28,11 @@
# If `calloc (0, 0)' is properly handled, run IF-WORKS, otherwise, IF-NOT.
AC_DEFUN([_AC_FUNC_CALLOC_IF],
[AC_REQUIRE([AC_HEADER_STDC])dnl
+AC_REQUIRE([AC_TYPE_SIZE_T])dnl
AC_CHECK_HEADERS(stdlib.h)
AC_CACHE_CHECK([for GNU libc compatible calloc], ac_cv_func_calloc_0_nonnull,
-[AC_RUN_IFELSE(
-[AC_LANG_PROGRAM(
-[[#if STDC_HEADERS || HAVE_STDLIB_H
-# include <stdlib.h>
-#else
-char *calloc ();
-#endif
-]],
- [exit (calloc (0, 0) ? 0 : 1);])],
+[AC_RUN_IFELSE([AC_LANG_PROGRAM([AC_INCLUDES_DEFAULT],
+ [exit (!calloc (0, 0) || calloc ((size_t) -1 / 8 + 1, 8));])],
[ac_cv_func_calloc_0_nonnull=yes],
[ac_cv_func_calloc_0_nonnull=no],
[ac_cv_func_calloc_0_nonnull=no])])