summaryrefslogtreecommitdiff
path: root/lib/calloc.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2004-11-17 23:09:12 +0000
committerPaul Eggert <eggert@cs.ucla.edu>2004-11-17 23:09:12 +0000
commit0884570c4e81073738a5bdceb2653f447685a296 (patch)
treee67af1d8445f34b494a125e6d6c87cb5d61669c3 /lib/calloc.c
parenta18a5093f84d0ef20711af85b7ce0b9392144f27 (diff)
downloadcoreutils-0884570c4e81073738a5bdceb2653f447685a296.tar.xz
Sync from gnulib.
Diffstat (limited to 'lib/calloc.c')
-rw-r--r--lib/calloc.c10
1 files changed, 9 insertions, 1 deletions
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);
}