1# Copyright (C) 2002-2004, 2006-2018 Free Software Foundation, Inc. 2# This file is free software; the Free Software Foundation 3# gives unlimited permission to copy and/or distribute it, 4# with or without modifications, as long as this notice is preserved. 5 6# Test whether the *printf family of functions recovers gracefully in case 7# of an out-of-memory condition, or whether it crashes the entire program. 8# Result is gl_cv_func_printf_enomem. 9 10printf_enomem_test = ''' 11#include <stdio.h> 12#include <sys/types.h> 13#include <sys/time.h> 14#include <sys/resource.h> 15#include <errno.h> 16int main() 17{ 18 struct rlimit limit; 19 int ret; 20 nocrash_init (); 21 /* Some printf implementations allocate temporary space with malloc. */ 22 /* On BSD systems, malloc() is limited by RLIMIT_DATA. */ 23#ifdef RLIMIT_DATA 24 if (getrlimit (RLIMIT_DATA, &limit) < 0) 25 return 77; 26 if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > 5000000) 27 limit.rlim_max = 5000000; 28 limit.rlim_cur = limit.rlim_max; 29 if (setrlimit (RLIMIT_DATA, &limit) < 0) 30 return 77; 31#endif 32 /* On Linux systems, malloc() is limited by RLIMIT_AS. */ 33#ifdef RLIMIT_AS 34 if (getrlimit (RLIMIT_AS, &limit) < 0) 35 return 77; 36 if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > 5000000) 37 limit.rlim_max = 5000000; 38 limit.rlim_cur = limit.rlim_max; 39 if (setrlimit (RLIMIT_AS, &limit) < 0) 40 return 77; 41#endif 42 /* Some printf implementations allocate temporary space on the stack. */ 43#ifdef RLIMIT_STACK 44 if (getrlimit (RLIMIT_STACK, &limit) < 0) 45 return 77; 46 if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > 5000000) 47 limit.rlim_max = 5000000; 48 limit.rlim_cur = limit.rlim_max; 49 if (setrlimit (RLIMIT_STACK, &limit) < 0) 50 return 77; 51#endif 52 ret = printf ("%.5000000f", 1.0); 53 return !(ret == 5000002 || (ret < 0 && errno == ENOMEM)); 54} 55''' 56 57if not meson.is_cross_build() or meson.has_exe_wrapper() 58 run_result = cc.run(printf_enomem_test, 59 name : 'printf survives out-of-memory conditions') 60 gl_cv_func_printf_enomem = run_result.compiled() and run_result.returncode() == 0 61else 62 # If we don't know, assume the worst. 63 gl_cv_func_printf_enomem = false 64 if (host_system == 'linux' or 65 host_system == 'solaris' or 66 host_system == 'sunos' or 67 host_system == 'aix' or 68 host_system == 'irix' or 69 host_system == 'beos' or 70 host_system == 'haiku') 71 gl_cv_func_printf_enomem = true 72 endif 73endif 74