]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
Sync from gnulib.
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 17 Nov 2004 23:09:12 +0000 (23:09 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 17 Nov 2004 23:09:12 +0000 (23:09 +0000)
lib/ChangeLog
lib/calloc.c
lib/malloc.c
lib/realloc.c
m4/ChangeLog
m4/calloc.m4

index 8fc22f03269d75a22d0ce3725807fb2bf8f874cd..64bc023f15a84d5b50b2566c31b3f9fee317621d 100644 (file)
@@ -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.
index 2a9e6fa20e681f09616e48fd9143f0f86f90d403..1ff0f1c0b8d581d775b5e04fcba21bc109ab2428 100644 (file)
@@ -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.
 
 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);
 }
index a43d169271b733412ba2f8326fd7f7643de93869..ca04c57cd87cf8a661980fe658eb0f94cd922f5c 100644 (file)
@@ -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
index ccbf991388e5010bb8399d016cdb57e5ac34ed2c..14fec075f7006a8165dfd0bacad96ee39a2fe682 100644 (file)
@@ -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);
 }
index 6b030b2045f995869ac19af35e5bfa4d547cea0f..6cd8f6a155e12324999327ce30bf4d6b226b9983 100644 (file)
@@ -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.
index 5d03ed27111eeabd26ee6df0a36864a49cb8aa5f..f244df7b22a62319c9c72d6751c6e497cad02eea 100644 (file)
@@ -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.
 
 # 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])])