summaryrefslogtreecommitdiff
path: root/lib/calloc.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2004-11-17 23:05:47 +0000
committerPaul Eggert <eggert@cs.ucla.edu>2004-11-17 23:05:47 +0000
commit31d4ee218ba9dabe654a9fbf53a72e5bf203b0dc (patch)
treebbf403b1ee699dc4099754adb5ebf1ce79bd4e97 /lib/calloc.c
parent7b2dc3362ca654843fae1d6c14233ae5701a72df (diff)
downloadgnulib-31d4ee218ba9dabe654a9fbf53a72e5bf203b0dc.tar.gz
(rpl_calloc): Defend against buggy calloc implementations
that mishandle size_t overflow.
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 2a9e6fa20e..1ff0f1c0b8 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);
}