openat: don’t close (-1)
[gnulib.git] / tests / test-c-stack.c
blobd21b8dad7beb42f5803a5a07058bf0640a255e4d
1 /* Test of c-stack module.
2 Copyright (C) 2002, 2004, 2006, 2008-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #include <config.h>
19 #include "c-stack.h"
21 #include "exitfail.h"
22 #include <stdio.h>
23 #if HAVE_SETRLIMIT
24 /* At least FreeBSD 5.0 needs extra headers before <sys/resource.h>
25 will compile. */
26 # include <sys/types.h>
27 # include <sys/time.h>
28 # include <sys/resource.h>
29 #endif
31 #include "macros.h"
33 /* Skip this test when an address sanitizer is in use. */
34 #ifndef __has_feature
35 # define __has_feature(a) 0
36 #endif
37 #if defined __SANITIZE_ADDRESS__ || __has_feature (address_sanitizer)
39 int
40 main (int argc, char **argv)
42 fputs ("skipping test: address sanitizer in use\n", stderr);
43 return 77;
46 #else
48 static volatile int *
49 recurse_1 (volatile int n, volatile int *p)
51 if (n >= 0)
52 *recurse_1 (n + 1, p) += n;
53 return p;
56 static int
57 recurse (volatile int n)
59 int sum = 0;
60 return *recurse_1 (n, &sum);
63 int
64 main (int argc, char **argv)
66 # if HAVE_SETRLIMIT && defined RLIMIT_STACK
67 /* Before starting the endless recursion, try to be friendly to the
68 user's machine. On some Linux 2.2.x systems, there is no stack
69 limit for user processes at all. We don't want to kill such
70 systems. */
71 struct rlimit rl;
72 rl.rlim_cur = rl.rlim_max = 0x100000; /* 1 MB */
73 setrlimit (RLIMIT_STACK, &rl);
74 # endif
76 if (c_stack_action (NULL) == 0)
78 if (1 < argc)
80 exit_failure = 77;
81 ++*argv[argc]; /* Intentionally dereference NULL. */
83 return recurse (0);
85 fputs ("skipping test: ", stderr);
86 perror ("c_stack_action");
87 return 77;
90 #endif