ug_CN locale: Fix noexpr and yesexpr
[glibc.git] / posix / tst-glob-tilde.c
blob9518b4a6f85d16a7d7eab4b383b8b2b7e6877f55
1 /* Check for GLOB_TIDLE heap allocation issues (bug 22320, bug 22325).
2 Copyright (C) 2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <glob.h>
20 #include <mcheck.h>
21 #include <nss.h>
22 #include <pwd.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <support/check.h>
26 #include <support/support.h>
28 /* Flag which indicates whether to pass the GLOB_ONLYDIR flag. */
29 static int do_onlydir;
31 /* Flag which indicates whether to pass the GLOB_NOCHECK flag. */
32 static int do_nocheck;
34 /* Flag which indicates whether to pass the GLOB_MARK flag. */
35 static int do_mark;
37 static void
38 one_test (const char *prefix, const char *middle, const char *suffix)
40 char *pattern = xasprintf ("%s%s%s", prefix, middle, suffix);
41 int flags = GLOB_TILDE;
42 if (do_onlydir)
43 flags |= GLOB_ONLYDIR;
44 if (do_nocheck)
45 flags |= GLOB_NOCHECK;
46 if (do_mark)
47 flags |= GLOB_MARK;
48 glob_t gl;
49 /* This glob call might result in crashes or memory leaks. */
50 if (glob (pattern, flags, NULL, &gl) == 0)
51 globfree (&gl);
52 free (pattern);
55 enum
57 /* The largest base being tested. */
58 largest_base_size = 500000,
60 /* The actual size is the base size plus a variable whose absolute
61 value is not greater than this. This helps malloc to trigger
62 overflows. */
63 max_size_skew = 16,
65 /* The maximum string length supported by repeating_string
66 below. */
67 repeat_size = largest_base_size + max_size_skew,
70 /* Used to construct strings which repeat a single character 'x'. */
71 static char *repeat;
73 /* Return a string of SIZE characters. */
74 const char *
75 repeating_string (int size)
77 TEST_VERIFY (size >= 0);
78 TEST_VERIFY (size <= repeat_size);
79 const char *repeated_shifted = repeat + repeat_size - size;
80 TEST_VERIFY (strlen (repeated_shifted) == size);
81 return repeated_shifted;
84 static int
85 do_test (void)
87 /* Avoid network-based NSS modules and initialize nss_files with a
88 dummy lookup. This has to come before mtrace because NSS does
89 not free all memory. */
90 __nss_configure_lookup ("passwd", "files");
91 (void) getpwnam ("root");
93 mtrace ();
95 repeat = xmalloc (repeat_size + 1);
96 memset (repeat, 'x', repeat_size);
97 repeat[repeat_size] = '\0';
99 /* These numbers control the size of the user name. The values
100 cover the minimum (0), a typical size (8), a large
101 stack-allocated size (100000), and a somewhat large
102 heap-allocated size (largest_base_size). */
103 static const int base_sizes[] = { 0, 8, 100, 100000, largest_base_size, -1 };
105 for (do_onlydir = 0; do_onlydir < 2; ++do_onlydir)
106 for (do_nocheck = 0; do_nocheck < 2; ++do_nocheck)
107 for (do_mark = 0; do_mark < 2; ++do_mark)
108 for (int base_idx = 0; base_sizes[base_idx] >= 0; ++base_idx)
110 for (int size_skew = -max_size_skew; size_skew <= max_size_skew;
111 ++size_skew)
113 int size = base_sizes[base_idx] + size_skew;
114 if (size < 0)
115 continue;
117 const char *user_name = repeating_string (size);
118 one_test ("~", user_name, "/a/b");
121 const char *user_name = repeating_string (base_sizes[base_idx]);
122 one_test ("~", user_name, "");
123 one_test ("~", user_name, "/");
124 one_test ("~", user_name, "/a");
125 one_test ("~", user_name, "/*/*");
126 one_test ("~", user_name, "\\/");
127 one_test ("/~", user_name, "");
128 one_test ("*/~", user_name, "/a/b");
131 free (repeat);
133 return 0;
136 #include <support/test-driver.c>