support: Also return fd when it is 0
[glibc.git] / io / tst-ftw-bz26353.c
blob6b89fbcaaa8d8405dc7b5f8412ba8d6f62266656
1 /* test ftw bz26353: Check whether stack overflow occurs when the value
2 of the nopenfd parameter is too large.
4 Copyright (C) 2020-2021 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <https://www.gnu.org/licenses/>. */
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <ftw.h>
24 #include <errno.h>
25 #include <sys/resource.h>
27 #include <support/temp_file.h>
28 #include <support/capture_subprocess.h>
29 #include <support/check.h>
31 static int
32 my_func (const char *file, const struct stat *sb, int flag)
34 return 0;
37 static int
38 get_large_nopenfd (void)
40 struct rlimit r;
41 TEST_COMPARE (getrlimit (RLIMIT_STACK, &r), 0);
42 if (r.rlim_cur == RLIM_INFINITY)
44 r.rlim_cur = 8 * 1024 * 1024;
45 TEST_COMPARE (setrlimit (RLIMIT_STACK, &r), 0);
47 return (int) r.rlim_cur;
50 static void
51 do_ftw (void *unused)
53 char *tempdir = support_create_temp_directory ("tst-bz26353");
54 int large_nopenfd = get_large_nopenfd ();
55 TEST_COMPARE (ftw (tempdir, my_func, large_nopenfd), 0);
56 free (tempdir);
59 /* Check whether stack overflow occurs. */
60 static int
61 do_test (void)
63 struct support_capture_subprocess result;
64 result = support_capture_subprocess (do_ftw, NULL);
65 support_capture_subprocess_check (&result, "bz26353", 0, sc_allow_none);
66 support_capture_subprocess_free (&result);
67 return 0;
70 #include <support/test-driver.c>