1 /* test ftw bz26353: Check whether stack overflow occurs when the value
2    of the nopenfd parameter is too large.
3 
4    Copyright (C) 2020-2022 Free Software Foundation, Inc.
5    This file is part of the GNU C Library.
6 
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.
11 
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.
16 
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/>.  */
20 
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <ftw.h>
24 #include <errno.h>
25 #include <sys/resource.h>
26 
27 #include <support/temp_file.h>
28 #include <support/capture_subprocess.h>
29 #include <support/check.h>
30 
31 static int
my_func(const char * file,const struct stat * sb,int flag)32 my_func (const char *file, const struct stat *sb, int flag)
33 {
34   return 0;
35 }
36 
37 static int
get_large_nopenfd(void)38 get_large_nopenfd (void)
39 {
40   struct rlimit r;
41   TEST_COMPARE (getrlimit (RLIMIT_STACK, &r), 0);
42   if (r.rlim_cur == RLIM_INFINITY)
43     {
44       r.rlim_cur = 8 * 1024 * 1024;
45       TEST_COMPARE (setrlimit (RLIMIT_STACK, &r), 0);
46     }
47   return (int) r.rlim_cur;
48 }
49 
50 static void
do_ftw(void * unused)51 do_ftw (void *unused)
52 {
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);
57 }
58 
59 /* Check whether stack overflow occurs.  */
60 static int
do_test(void)61 do_test (void)
62 {
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;
68 }
69 
70 #include <support/test-driver.c>
71