changeset 5464:f7677662318c

(rpl_calloc): Defend against buggy calloc implementations that mishandle size_t overflow.
author Paul Eggert <eggert@cs.ucla.edu>
date Wed, 17 Nov 2004 23:05:47 +0000
parents 8044da4f6750
children f0ee86da74a7
files lib/calloc.c
diffstat 1 files changed, 9 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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);
 }