summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/ChangeLog7
-rw-r--r--lib/calloc.c10
-rw-r--r--lib/malloc.c2
-rw-r--r--lib/realloc.c15
4 files changed, 28 insertions, 6 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);
}