X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=tests%2Ftest-fprintf-posix3.c;h=375d0c6d14331cbffd53a5f692280df904e70979;hb=54c3ba61dfdf260865f2a6529ab414e24da17cf5;hp=90a2f7ce803429c3841f4120c916c10546af6076;hpb=d60f3b0c6b0f93a601acd1cfd3923f94ca05abb0;p=gnulib.git diff --git a/tests/test-fprintf-posix3.c b/tests/test-fprintf-posix3.c index 90a2f7ce8..375d0c6d1 100644 --- a/tests/test-fprintf-posix3.c +++ b/tests/test-fprintf-posix3.c @@ -1,5 +1,5 @@ /* Test of POSIX compatible fprintf() function. - Copyright (C) 2009-2011 Free Software Foundation, Inc. + Copyright (C) 2009-2013 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 @@ -20,15 +20,18 @@ #include -#if HAVE_GETRLIMIT && HAVE_SETRLIMIT - #include -#include -#include -#include #include #include +#if HAVE_GETRLIMIT && HAVE_SETRLIMIT +# include +# include +# include +#endif + +#include "resource-ext.h" + /* Test against a memory leak in the fprintf replacement. */ /* Number of iterations across the loop. */ @@ -45,30 +48,32 @@ int main (int argc, char *argv[]) { - struct rlimit limit; + uintptr_t initial_rusage_as; int arg; - int repeat; + int result; /* Limit the amount of malloc()ed memory to MAX_ALLOC_TOTAL or less. */ - /* On BSD systems, malloc() is limited by RLIMIT_DATA. */ -#ifdef RLIMIT_DATA - if (getrlimit (RLIMIT_DATA, &limit) < 0) - return 77; - if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > MAX_ALLOC_TOTAL) - limit.rlim_max = MAX_ALLOC_TOTAL; - limit.rlim_cur = limit.rlim_max; - if (setrlimit (RLIMIT_DATA, &limit) < 0) - return 77; + /* On AIX systems, malloc() is limited by RLIMIT_DATA. */ +#if HAVE_GETRLIMIT && HAVE_SETRLIMIT && defined RLIMIT_DATA + { + struct rlimit limit; + + if (getrlimit (RLIMIT_DATA, &limit) >= 0) + { + if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > MAX_ALLOC_TOTAL) + limit.rlim_max = MAX_ALLOC_TOTAL; + limit.rlim_cur = limit.rlim_max; + (void) setrlimit (RLIMIT_DATA, &limit); + } + } #endif - /* On Linux systems, malloc() is limited by RLIMIT_AS. */ -#ifdef RLIMIT_AS - if (getrlimit (RLIMIT_AS, &limit) < 0) - return 77; - if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > MAX_ALLOC_TOTAL) - limit.rlim_max = MAX_ALLOC_TOTAL; - limit.rlim_cur = limit.rlim_max; - if (setrlimit (RLIMIT_AS, &limit) < 0) + /* On all systems except AIX and OpenBSD, malloc() is limited by RLIMIT_AS. + On some systems, setrlimit of RLIMIT_AS doesn't work but get_rusage_as () + does. Allow the address space size to grow by at most MAX_ALLOC_TOTAL. */ + initial_rusage_as = get_rusage_as (); +#if !defined _AIX + if (initial_rusage_as == 0) return 77; #endif @@ -79,30 +84,28 @@ main (int argc, char *argv[]) if (memory == NULL) return 1; memset (memory, 17, MAX_ALLOC_TOTAL); - return 78; + result = 78; } - - /* Perform the test and test whether it triggers a permanent memory - allocation of more than MAX_ALLOC_TOTAL bytes. */ - - for (repeat = 0; repeat < NUM_ROUNDS; repeat++) + else { - /* This may produce a temporary memory allocation of 11000 bytes. - but should not result in a permanent memory allocation. */ - if (fprintf (stdout, "%011000d\n", 17) == -1 - && errno == ENOMEM) - return 1; + /* Perform the test and test whether it triggers a permanent memory + allocation of more than MAX_ALLOC_TOTAL bytes. */ + int repeat; + + for (repeat = 0; repeat < NUM_ROUNDS; repeat++) + { + /* This may produce a temporary memory allocation of 11000 bytes. + but should not result in a permanent memory allocation. */ + if (fprintf (stdout, "%011000d\n", 17) == -1 + && errno == ENOMEM) + return 1; + } + + result = 0; } - return 0; -} + if (get_rusage_as () > initial_rusage_as + MAX_ALLOC_TOTAL) + return 1; -#else - -int -main (int argc, char *argv[]) -{ - return 77; + return result; } - -#endif