>From bad7a098679f6bfd8573d14e1722765977436643 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 9 May 2021 18:36:41 +0200 Subject: [PATCH 2/2] malloc-gnu, realloc-gnu, calloc-gnu: Ensure errno gets set on IRIX. * m4/malloc.m4 (gl_CHECK_MALLOC_POSIX): Require AC_CANONICAL_HOST. Set gl_cv_func_malloc_posix to 'no' also on IRIX. --- ChangeLog | 4 ++++ m4/malloc.m4 | 56 +++++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index e69f447..b24976a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2021-05-09 Bruno Haible + malloc-gnu, realloc-gnu, calloc-gnu: Ensure errno gets set on IRIX. + * m4/malloc.m4 (gl_CHECK_MALLOC_POSIX): Require AC_CANONICAL_HOST. Set + gl_cv_func_malloc_posix to 'no' also on IRIX. + malloc-gnu, realloc-gnu, calloc-gnu tests: Verify errno is set. * tests/test-malloc-gnu.c: Include . (main): Verify that, when an allocation larger than PTRDIFF_MAX failed, diff --git a/m4/malloc.m4 b/m4/malloc.m4 index c09006d..de1b2c6 100644 --- a/m4/malloc.m4 +++ b/m4/malloc.m4 @@ -1,4 +1,4 @@ -# malloc.m4 serial 24 +# malloc.m4 serial 25 dnl Copyright (C) 2007, 2009-2021 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -115,20 +115,54 @@ AC_DEFUN([gl_FUNC_MALLOC_POSIX], # Set gl_cv_func_malloc_posix to yes or no accordingly. AC_DEFUN([gl_CHECK_MALLOC_POSIX], [ + AC_REQUIRE([AC_CANONICAL_HOST]) AC_CACHE_CHECK([whether malloc, realloc, calloc set errno on failure], [gl_cv_func_malloc_posix], [ dnl It is too dangerous to try to allocate a large amount of memory: dnl some systems go to their knees when you do that. So assume that - dnl all Unix implementations of the function set errno on failure. - AC_COMPILE_IFELSE( - [AC_LANG_PROGRAM( - [[]], - [[#if defined _WIN32 && ! defined __CYGWIN__ - choke me - #endif - ]])], - [gl_cv_func_malloc_posix=yes], - [gl_cv_func_malloc_posix=no]) + dnl all Unix implementations of the function set errno on failure, + dnl except on those platforms where we have seen 'test-malloc-gnu', + dnl 'test-realloc-gnu', 'test-calloc-gnu' fail. + case "$host_os" in + mingw*) + gl_cv_func_malloc_posix=no ;; + irix*) + dnl The three functions return NULL with errno unset when the + dnl argument is larger than PTRDIFF_MAX. Here is a test program: +m4_divert_push([KILL]) +#include +#include +#include +#define ptrdiff_t long +#ifndef PTRDIFF_MAX +# define PTRDIFF_MAX ((ptrdiff_t) ((1UL << (8 * sizeof (ptrdiff_t) - 1)) - 1)) +#endif + +int main () +{ + void *p; + + fprintf (stderr, "PTRDIFF_MAX = %lu\n", (unsigned long) PTRDIFF_MAX); + + errno = 0; + p = malloc ((unsigned long) PTRDIFF_MAX + 1); + fprintf (stderr, "p=%p errno=%d\n", p, errno); + + errno = 0; + p = calloc (PTRDIFF_MAX / 2 + 1, 2); + fprintf (stderr, "p=%p errno=%d\n", p, errno); + + errno = 0; + p = realloc (NULL, (unsigned long) PTRDIFF_MAX + 1); + fprintf (stderr, "p=%p errno=%d\n", p, errno); + + return 0; +} +m4_divert_pop([KILL]) + gl_cv_func_malloc_posix=no ;; + *) + gl_cv_func_malloc_posix=yes ;; + esac ]) ]) -- 2.7.4