]>
Commit | Line | Data |
---|---|---|
688903eb | 1 | /* Copyright (C) 1997-2018 Free Software Foundation, Inc. |
b5efde2f UD |
2 | This file is part of the GNU C Library. |
3 | ||
4 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
b5efde2f UD |
8 | |
9 | The GNU C Library 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 GNU | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
b5efde2f | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
b5efde2f | 17 | |
8d9618b7 UD |
18 | #include <sys/stat.h> |
19 | #include <sys/types.h> | |
895c30cb | 20 | #include <sys/mman.h> |
8d9618b7 UD |
21 | #include <fcntl.h> |
22 | #include <unistd.h> | |
ac16e905 | 23 | #include <pwd.h> |
b5efde2f | 24 | #include <stdio.h> |
9090848d | 25 | #include <stdint.h> |
b5efde2f | 26 | #include <stdlib.h> |
3e1f480e | 27 | #include <string.h> |
ac16e905 | 28 | #include <wordexp.h> |
9090848d | 29 | #include <libc-pointer-arith.h> |
825adeee | 30 | #include <dso_handle.h> |
b5efde2f | 31 | |
6ca96fe2 | 32 | #define IFS " \n\t" |
3db52d94 | 33 | |
a39208bd CD |
34 | extern int __register_atfork (void (*) (void), void (*) (void), void (*) (void), void *); |
35 | ||
36 | static int __app_register_atfork (void (*prepare) (void), void (*parent) (void), void (*child) (void)) | |
37 | { | |
825adeee | 38 | return __register_atfork (prepare, parent, child, __dso_handle); |
a39208bd CD |
39 | } |
40 | ||
41 | /* Number of forks seen. */ | |
42 | static int registered_forks; | |
43 | ||
44 | /* For each fork increment the fork count. */ | |
45 | static void | |
46 | register_fork (void) | |
47 | { | |
48 | registered_forks++; | |
49 | } | |
50 | ||
b5efde2f UD |
51 | struct test_case_struct |
52 | { | |
53 | int retval; | |
54 | const char *env; | |
55 | const char *words; | |
56 | int flags; | |
00b16c4a | 57 | size_t wordc; |
b5efde2f | 58 | const char *wordv[10]; |
6ca96fe2 | 59 | const char *ifs; |
b5efde2f UD |
60 | } test_case[] = |
61 | { | |
c06cc21c | 62 | /* Simple word- and field-splitting */ |
6ca96fe2 UD |
63 | { 0, NULL, "one", 0, 1, { "one", }, IFS }, |
64 | { 0, NULL, "one two", 0, 2, { "one", "two", }, IFS }, | |
65 | { 0, NULL, "one two three", 0, 3, { "one", "two", "three", }, IFS }, | |
66 | { 0, NULL, " \tfoo\t\tbar ", 0, 2, { "foo", "bar", }, IFS }, | |
a8125d85 | 67 | { 0, NULL, "red , white blue", 0, 4, { "red", ",", "white", "blue", }, " ," }, |
c06cc21c UD |
68 | { 0, NULL, "one two three", 0, 3, { "one", "two", "three", }, "" }, |
69 | { 0, NULL, "one \"two three\"", 0, 2, { "one", "two three", }, IFS }, | |
70 | { 0, NULL, "one \"two three\"", 0, 2, { "one", "two three", }, "" }, | |
71 | { 0, "two three", "one \"$var\"", 0, 2, { "one", "two three", }, IFS }, | |
72 | { 0, "two three", "one $var", 0, 3, { "one", "two", "three", }, IFS }, | |
73 | { 0, "two three", "one \"$var\"", 0, 2, { "one", "two three", }, "" }, | |
74 | { 0, "two three", "one $var", 0, 2, { "one", "two three", }, "" }, | |
1ffaaca2 UD |
75 | |
76 | /* The non-whitespace IFS char at the end delimits the second field | |
77 | * but does NOT start a new field. */ | |
78 | { 0, ":abc:", "$var", 0, 2, { "", "abc", }, ":" }, | |
79 | ||
4708015f UD |
80 | { 0, NULL, "$(echo :abc:)", 0, 2, { "", "abc", }, ":" }, |
81 | { 0, NULL, "$(echo :abc:\\ )", 0, 2, { "", "abc", }, ": " }, | |
82 | { 0, NULL, "$(echo :abc\\ )", 0, 2, { "", "abc", }, ": " }, | |
83 | { 0, ":abc:", "$(echo $var)", 0, 2, { "", "abc", }, ":" }, | |
a8125d85 | 84 | { 0, NULL, ":abc:", 0, 1, { ":abc:", }, ":" }, |
4708015f | 85 | { 0, NULL, "$(echo :abc:)def", 0, 3, { "", "abc", "def", }, |
b2900a13 | 86 | ":" }, |
4708015f UD |
87 | { 0, NULL, "$(echo abc:de)f", 0, 2, { "abc", "def", }, ":" }, |
88 | { 0, NULL, "$(echo abc:de)f:ghi", 0, 2, { "abc", "def:ghi", }, | |
b2900a13 | 89 | ":" }, |
4708015f | 90 | { 0, NULL, "abc:d$(echo ef:ghi)", 0, 2, { "abc:def", "ghi", }, |
b2900a13 | 91 | ":" }, |
4708015f | 92 | { 0, "abc:", "$var$(echo def:ghi)", 0, 3, { "abc", "def", |
b2900a13 | 93 | "ghi", }, ":" }, |
4708015f | 94 | { 0, "abc:d", "$var$(echo ef:ghi)", 0, 3, { "abc", "def", |
b2900a13 | 95 | "ghi", }, ":" }, |
4708015f | 96 | { 0, "def:ghi", "$(echo abc:)$var", 0, 3, { "abc", "def", |
b2900a13 | 97 | "ghi", }, ":" }, |
4708015f | 98 | { 0, "ef:ghi", "$(echo abc:d)$var", 0, 3, { "abc", "def", |
b2900a13 | 99 | "ghi", }, ":" }, |
3db52d94 UD |
100 | |
101 | /* Simple parameter expansion */ | |
6ca96fe2 UD |
102 | { 0, "foo", "${var}", 0, 1, { "foo", }, IFS }, |
103 | { 0, "foo", "$var", 0, 1, { "foo", }, IFS }, | |
104 | { 0, "foo", "\\\"$var\\\"", 0, 1, { "\"foo\"", }, IFS }, | |
105 | { 0, "foo", "%$var%", 0, 1, { "%foo%", }, IFS }, | |
106 | { 0, "foo", "-$var-", 0, 1, { "-foo-", }, IFS }, | |
3db52d94 UD |
107 | |
108 | /* Simple quote removal */ | |
6ca96fe2 UD |
109 | { 0, NULL, "\"quoted\"", 0, 1, { "quoted", }, IFS }, |
110 | { 0, "foo", "\"$var\"\"$var\"", 0, 1, { "foofoo", }, IFS }, | |
111 | { 0, NULL, "'singly-quoted'", 0, 1, { "singly-quoted", }, IFS }, | |
bac660f8 | 112 | { 0, NULL, "contin\\\nuation", 0, 1, { "continuation", }, IFS }, |
a9c27b3e UD |
113 | { 0, NULL, "explicit ''", 0, 2, { "explicit", "", }, IFS }, |
114 | { 0, NULL, "explicit \"\"", 0, 2, { "explicit", "", }, IFS }, | |
115 | { 0, NULL, "explicit ``", 0, 1, { "explicit", }, IFS }, | |
3db52d94 UD |
116 | |
117 | /* Simple command substitution */ | |
6ca96fe2 UD |
118 | { 0, NULL, "$(echo hello)", 0, 1, { "hello", }, IFS }, |
119 | { 0, NULL, "$( (echo hello) )", 0, 1, { "hello", }, IFS }, | |
120 | { 0, NULL, "$((echo hello);(echo there))", 0, 2, { "hello", "there", }, IFS }, | |
121 | { 0, NULL, "`echo one two`", 0, 2, { "one", "two", }, IFS }, | |
122 | { 0, NULL, "$(echo ')')", 0, 1, { ")" }, IFS }, | |
123 | { 0, NULL, "$(echo hello; echo)", 0, 1, { "hello", }, IFS }, | |
52aec7d1 | 124 | { 0, NULL, "a$(echo b)c", 0, 1, { "abc", }, IFS }, |
3db52d94 UD |
125 | |
126 | /* Simple arithmetic expansion */ | |
6ca96fe2 UD |
127 | { 0, NULL, "$((1 + 1))", 0, 1, { "2", }, IFS }, |
128 | { 0, NULL, "$((2-3))", 0, 1, { "-1", }, IFS }, | |
129 | { 0, NULL, "$((-1))", 0, 1, { "-1", }, IFS }, | |
130 | { 0, NULL, "$[50+20]", 0, 1, { "70", }, IFS }, | |
131 | { 0, NULL, "$(((2+3)*(4+5)))", 0, 1, { "45", }, IFS }, | |
bcc86889 UD |
132 | { 0, NULL, "$((010))", 0, 1, { "8" }, IFS }, |
133 | { 0, NULL, "$((0x10))", 0, 1, { "16" }, IFS }, | |
134 | { 0, NULL, "$((010+0x10))", 0, 1, { "24" }, IFS }, | |
135 | { 0, NULL, "$((-010+0x10))", 0, 1, { "8" }, IFS }, | |
136 | { 0, NULL, "$((-0x10+010))", 0, 1, { "-8" }, IFS }, | |
3db52d94 UD |
137 | |
138 | /* Advanced parameter expansion */ | |
6ca96fe2 UD |
139 | { 0, NULL, "${var:-bar}", 0, 1, { "bar", }, IFS }, |
140 | { 0, NULL, "${var-bar}", 0, 1, { "bar", }, IFS }, | |
141 | { 0, "", "${var:-bar}", 0, 1, { "bar", }, IFS }, | |
142 | { 0, "foo", "${var:-bar}", 0, 1, { "foo", }, IFS }, | |
143 | { 0, "", "${var-bar}", 0, 0, { NULL, }, IFS }, | |
144 | { 0, NULL, "${var:=bar}", 0, 1, { "bar", }, IFS }, | |
145 | { 0, NULL, "${var=bar}", 0, 1, { "bar", }, IFS }, | |
146 | { 0, "", "${var:=bar}", 0, 1, { "bar", }, IFS }, | |
147 | { 0, "foo", "${var:=bar}", 0, 1, { "foo", }, IFS }, | |
148 | { 0, "", "${var=bar}", 0, 0, { NULL, }, IFS }, | |
149 | { 0, "foo", "${var:?bar}", 0, 1, { "foo", }, IFS }, | |
150 | { 0, NULL, "${var:+bar}", 0, 0, { NULL, }, IFS }, | |
151 | { 0, NULL, "${var+bar}", 0, 0, { NULL, }, IFS }, | |
152 | { 0, "", "${var:+bar}", 0, 0, { NULL, }, IFS }, | |
153 | { 0, "foo", "${var:+bar}", 0, 1, { "bar", }, IFS }, | |
154 | { 0, "", "${var+bar}", 0, 1, { "bar", }, IFS }, | |
155 | { 0, "12345", "${#var}", 0, 1, { "5", }, IFS }, | |
156 | { 0, NULL, "${var:-'}'}", 0, 1, { "}", }, IFS }, | |
157 | { 0, NULL, "${var-}", 0, 0, { NULL }, IFS }, | |
158 | ||
bac660f8 UD |
159 | { 0, "pizza", "${var#${var}}", 0, 0, { NULL }, IFS }, |
160 | { 0, "pepperoni", "${var%$(echo oni)}", 0, 1, { "pepper" }, IFS }, | |
161 | { 0, "6pack", "${var#$((6))}", 0, 1, { "pack" }, IFS }, | |
162 | { 0, "b*witched", "${var##b*}", 0, 0, { NULL }, IFS }, | |
163 | { 0, "b*witched", "${var##\"b*\"}", 0, 1, { "witched" }, IFS }, | |
6ca96fe2 UD |
164 | { 0, "banana", "${var%na*}", 0, 1, { "bana", }, IFS }, |
165 | { 0, "banana", "${var%%na*}", 0, 1, { "ba", }, IFS }, | |
166 | { 0, "borabora-island", "${var#*bora}", 0, 1, { "bora-island", }, IFS }, | |
bac660f8 UD |
167 | { 0, "borabora-island", "${var##*bora}", 0, 1, { "-island", }, IFS }, |
168 | { 0, "coconut", "${var##\\*co}", 0, 1, { "coconut", }, IFS }, | |
6ca96fe2 | 169 | { 0, "100%", "${var%0%}", 0, 1, { "10" }, IFS }, |
3db52d94 | 170 | |
8d9618b7 | 171 | /* Pathname expansion */ |
6ca96fe2 UD |
172 | { 0, NULL, "???", 0, 2, { "one", "two", }, IFS }, |
173 | { 0, NULL, "[ot]??", 0, 2, { "one", "two", }, IFS }, | |
174 | { 0, NULL, "t*", 0, 2, { "three", "two", }, IFS }, | |
175 | { 0, NULL, "\"t\"*", 0, 2, { "three", "two", }, IFS }, | |
8d9618b7 UD |
176 | |
177 | /* Nested constructs */ | |
6ca96fe2 UD |
178 | { 0, "one two", "$var", 0, 2, { "one", "two", }, IFS }, |
179 | { 0, "one two three", "$var", 0, 3, { "one", "two", "three", }, IFS }, | |
180 | { 0, " \tfoo\t\tbar ", "$var", 0, 2, { "foo", "bar", }, IFS }, | |
181 | { 0, " red , white blue", "$var", 0, 3, { "red", "white", "blue", }, ", \n\t" }, | |
182 | { 0, " red , white blue", "\"$var\"", 0, 1, { " red , white blue", }, ", \n\t" }, | |
183 | { 0, NULL, "\"$(echo hello there)\"", 0, 1, { "hello there", }, IFS }, | |
184 | { 0, NULL, "\"$(echo \"hello there\")\"", 0, 1, { "hello there", }, IFS }, | |
185 | { 0, NULL, "${var=one two} \"$var\"", 0, 3, { "one", "two", "one two", }, IFS }, | |
186 | { 0, "1", "$(( $(echo 3)+$var ))", 0, 1, { "4", }, IFS }, | |
187 | { 0, NULL, "\"$(echo \"*\")\"", 0, 1, { "*", }, IFS }, | |
52aec7d1 | 188 | { 0, NULL, "\"a\n\n$(echo)b\"", 0, 1, { "a\n\nb", }, IFS }, |
6ca96fe2 UD |
189 | { 0, "foo", "*$var*", 0, 1, { "*foo*", }, IFS }, |
190 | { 0, "o thr", "*$var*", 0, 2, { "two", "three" }, IFS }, | |
8d9618b7 | 191 | |
6796bc80 | 192 | /* Different IFS values */ |
c06cc21c UD |
193 | { 0, "a b\tc\nd ", "$var", 0, 4, { "a", "b", "c", "d" }, NULL /* unset */ }, |
194 | { 0, "a b\tc d ", "$var", 0, 1, { "a b\tc d " }, "" /* `null' */ }, | |
195 | { 0, "a,b c\n, d", "$var", 0, 3, { "a", "b c", " d" }, "\t\n," }, | |
6796bc80 | 196 | |
8d9618b7 | 197 | /* Other things that should succeed */ |
6ca96fe2 UD |
198 | { 0, NULL, "\\*\"|&;<>\"\\(\\)\\{\\}", 0, 1, { "*|&;<>(){}", }, IFS }, |
199 | { 0, "???", "$var", 0, 1, { "???", }, IFS }, | |
200 | { 0, NULL, "$var", 0, 0, { NULL, }, IFS }, | |
201 | { 0, NULL, "\"\\n\"", 0, 1, { "\\n", }, IFS }, | |
202 | { 0, NULL, "", 0, 0, { NULL, }, IFS }, | |
8d9618b7 | 203 | |
52aec7d1 UD |
204 | /* Flags not already covered (testit() has special handling for these) */ |
205 | { 0, NULL, "one two", WRDE_DOOFFS, 2, { "one", "two", }, IFS }, | |
206 | { 0, NULL, "appended", WRDE_APPEND, 3, { "pre1", "pre2", "appended", }, IFS }, | |
207 | { 0, NULL, "appended", WRDE_DOOFFS|WRDE_APPEND, 3, { "pre1", "pre2", "appended", }, IFS }, | |
208 | ||
8d9618b7 | 209 | /* Things that should fail */ |
6796bc80 | 210 | { WRDE_BADCHAR, NULL, "new\nline", 0, 0, { NULL, }, "" /* \n not IFS */ }, |
6ca96fe2 UD |
211 | { WRDE_BADCHAR, NULL, "pipe|symbol", 0, 0, { NULL, }, IFS }, |
212 | { WRDE_BADCHAR, NULL, "&ersand", 0, 0, { NULL, }, IFS }, | |
213 | { WRDE_BADCHAR, NULL, "semi;colon", 0, 0, { NULL, }, IFS }, | |
214 | { WRDE_BADCHAR, NULL, "<greater", 0, 0, { NULL, }, IFS }, | |
215 | { WRDE_BADCHAR, NULL, "less>", 0, 0, { NULL, }, IFS }, | |
216 | { WRDE_BADCHAR, NULL, "(open-paren", 0, 0, { NULL, }, IFS }, | |
217 | { WRDE_BADCHAR, NULL, "close-paren)", 0, 0, { NULL, }, IFS }, | |
218 | { WRDE_BADCHAR, NULL, "{open-brace", 0, 0, { NULL, }, IFS }, | |
219 | { WRDE_BADCHAR, NULL, "close-brace}", 0, 0, { NULL, }, IFS }, | |
220 | { WRDE_CMDSUB, NULL, "$(ls)", WRDE_NOCMD, 0, { NULL, }, IFS }, | |
221 | { WRDE_BADVAL, NULL, "$var", WRDE_UNDEF, 0, { NULL, }, IFS }, | |
222 | { WRDE_BADVAL, NULL, "$9", WRDE_UNDEF, 0, { NULL, }, IFS }, | |
223 | { WRDE_SYNTAX, NULL, "$[50+20))", 0, 0, { NULL, }, IFS }, | |
224 | { WRDE_SYNTAX, NULL, "${%%noparam}", 0, 0, { NULL, }, IFS }, | |
225 | { WRDE_SYNTAX, NULL, "${missing-brace", 0, 0, { NULL, }, IFS }, | |
52aec7d1 | 226 | { WRDE_SYNTAX, NULL, "$(for i in)", 0, 0, { NULL, }, IFS }, |
6ca96fe2 UD |
227 | { WRDE_SYNTAX, NULL, "$((2+))", 0, 0, { NULL, }, IFS }, |
228 | { WRDE_SYNTAX, NULL, "`", 0, 0, { NULL, }, IFS }, | |
bcc86889 | 229 | { WRDE_SYNTAX, NULL, "$((010+4+))", 0, 0, { NULL }, IFS }, |
a39208bd CD |
230 | /* Test for CVE-2014-7817. We test 3 combinations of command |
231 | substitution inside an arithmetic expression to make sure that | |
232 | no commands are executed and error is returned. */ | |
233 | { WRDE_CMDSUB, NULL, "$((`echo 1`))", WRDE_NOCMD, 0, { NULL, }, IFS }, | |
234 | { WRDE_CMDSUB, NULL, "$((1+`echo 1`))", WRDE_NOCMD, 0, { NULL, }, IFS }, | |
235 | { WRDE_CMDSUB, NULL, "$((1+$((`echo 1`))))", WRDE_NOCMD, 0, { NULL, }, IFS }, | |
6ca96fe2 | 236 | |
5f85a4bf PP |
237 | { WRDE_SYNTAX, NULL, "`\\", 0, 0, { NULL, }, IFS }, /* BZ 18042 */ |
238 | { WRDE_SYNTAX, NULL, "${", 0, 0, { NULL, }, IFS }, /* BZ 18043 */ | |
239 | { WRDE_SYNTAX, NULL, "L${a:", 0, 0, { NULL, }, IFS }, /* BZ 18043#c4 */ | |
2b028564 | 240 | { WRDE_SYNTAX, NULL, "$[1/0]", WRDE_NOCMD, 0, {NULL, }, IFS }, /* BZ 18100 */ |
36103ba2 | 241 | |
6ca96fe2 | 242 | { -1, NULL, NULL, 0, 0, { NULL, }, IFS }, |
b5efde2f UD |
243 | }; |
244 | ||
ac16e905 | 245 | static int testit (struct test_case_struct *tc); |
8619129f | 246 | static int tests; |
ac16e905 | 247 | |
de149cdb | 248 | static void |
8193034b UD |
249 | command_line_test (const char *words) |
250 | { | |
251 | wordexp_t we; | |
252 | int i; | |
253 | int retval = wordexp (words, &we, 0); | |
254 | printf ("wordexp returned %d\n", retval); | |
255 | for (i = 0; i < we.we_wordc; i++) | |
256 | printf ("we_wordv[%d] = \"%s\"\n", i, we.we_wordv[i]); | |
257 | } | |
258 | ||
b5efde2f | 259 | int |
8193034b | 260 | main (int argc, char *argv[]) |
b5efde2f | 261 | { |
67600288 | 262 | const char *globfile[] = { "one", "two", "three", NULL }; |
8d9618b7 | 263 | char tmpdir[32]; |
ac16e905 | 264 | struct passwd *pw; |
31161268 | 265 | const char *cwd; |
b5efde2f UD |
266 | int test; |
267 | int fail = 0; | |
67600288 | 268 | int i; |
52aec7d1 | 269 | struct test_case_struct ts; |
b5efde2f | 270 | |
8193034b UD |
271 | if (argc > 1) |
272 | { | |
273 | command_line_test (argv[1]); | |
274 | return 0; | |
275 | } | |
276 | ||
67600288 | 277 | cwd = getcwd (NULL, 0); |
8d9618b7 UD |
278 | |
279 | /* Set up arena for pathname expansion */ | |
280 | tmpnam (tmpdir); | |
67600288 | 281 | if (mkdir (tmpdir, S_IRWXU) || chdir (tmpdir)) |
8d9618b7 | 282 | return -1; |
67600288 UD |
283 | else |
284 | { | |
285 | int fd; | |
286 | ||
287 | for (i = 0; globfile[i]; ++i) | |
288 | if ((fd = creat (globfile[i], S_IRUSR | S_IWUSR)) == -1 | |
289 | || close (fd)) | |
290 | return -1; | |
291 | } | |
8d9618b7 | 292 | |
a39208bd CD |
293 | /* If we are not allowed to do command substitution, we install |
294 | fork handlers to verify that no forks happened. No forks should | |
295 | happen at all if command substitution is disabled. */ | |
296 | if (__app_register_atfork (register_fork, NULL, NULL) != 0) | |
297 | { | |
298 | printf ("Failed to register fork handler.\n"); | |
299 | return -1; | |
300 | } | |
301 | ||
2bcf29ba UD |
302 | for (test = 0; test_case[test].retval != -1; test++) |
303 | if (testit (&test_case[test])) | |
304 | ++fail; | |
305 | ||
a9c27b3e | 306 | /* Tilde-expansion tests. */ |
ac16e905 UD |
307 | pw = getpwnam ("root"); |
308 | if (pw != NULL) | |
b5efde2f | 309 | { |
52aec7d1 UD |
310 | ts.retval = 0; |
311 | ts.env = NULL; | |
312 | ts.words = "~root "; | |
313 | ts.flags = 0; | |
314 | ts.wordc = 1; | |
315 | ts.wordv[0] = pw->pw_dir; | |
316 | ts.ifs = IFS; | |
317 | ||
91eecefd UD |
318 | if (testit (&ts)) |
319 | ++fail; | |
320 | ||
321 | ts.retval = 0; | |
322 | ts.env = pw->pw_dir; | |
323 | ts.words = "${var#~root}x"; | |
324 | ts.flags = 0; | |
325 | ts.wordc = 1; | |
326 | ts.wordv[0] = "x"; | |
327 | ts.ifs = IFS; | |
328 | ||
52aec7d1 UD |
329 | if (testit (&ts)) |
330 | ++fail; | |
331 | } | |
332 | ||
333 | /* "~" expands to value of $HOME when HOME is set */ | |
334 | ||
335 | setenv ("HOME", "/dummy/home", 1); | |
336 | ts.retval = 0; | |
337 | ts.env = NULL; | |
338 | ts.words = "~ ~/foo"; | |
339 | ts.flags = 0; | |
340 | ts.wordc = 2; | |
341 | ts.wordv[0] = "/dummy/home"; | |
342 | ts.wordv[1] = "/dummy/home/foo"; | |
343 | ts.ifs = IFS; | |
344 | ||
345 | if (testit (&ts)) | |
346 | ++fail; | |
ac16e905 | 347 | |
52aec7d1 UD |
348 | /* "~" expands to home dir from passwd file if HOME is not set */ |
349 | ||
350 | pw = getpwuid (getuid ()); | |
351 | if (pw != NULL) | |
352 | { | |
353 | unsetenv ("HOME"); | |
ac16e905 UD |
354 | ts.retval = 0; |
355 | ts.env = NULL; | |
52aec7d1 | 356 | ts.words = "~"; |
ac16e905 UD |
357 | ts.flags = 0; |
358 | ts.wordc = 1; | |
359 | ts.wordv[0] = pw->pw_dir; | |
8619129f | 360 | ts.ifs = IFS; |
ac16e905 UD |
361 | |
362 | if (testit (&ts)) | |
b17277cf | 363 | ++fail; |
ac16e905 UD |
364 | } |
365 | ||
2b028564 FW |
366 | /* Integer overflow in division. */ |
367 | { | |
368 | static const char *const numbers[] = { | |
369 | "0", | |
370 | "1", | |
371 | "65536", | |
372 | "2147483648", | |
373 | "4294967296" | |
374 | "9223372036854775808", | |
375 | "18446744073709551616", | |
376 | "170141183460469231731687303715884105728", | |
377 | "340282366920938463463374607431768211456", | |
378 | NULL | |
379 | }; | |
380 | ||
381 | for (const char *const *num = numbers; *num; ++num) | |
382 | { | |
383 | wordexp_t p; | |
384 | char pattern[256]; | |
385 | snprintf (pattern, sizeof (pattern), "$[(-%s)/(-1)]", *num); | |
386 | int ret = wordexp (pattern, &p, WRDE_NOCMD); | |
387 | if (ret == 0) | |
388 | { | |
389 | if (p.we_wordc != 1 || strcmp (p.we_wordv[0], *num) != 0) | |
390 | { | |
391 | printf ("Integer overflow for \"%s\" failed", pattern); | |
392 | ++fail; | |
393 | } | |
394 | wordfree (&p); | |
395 | } | |
396 | else if (ret != WRDE_SYNTAX) | |
397 | { | |
398 | printf ("Integer overflow for \"%s\" failed with %d", | |
399 | pattern, ret); | |
400 | ++fail; | |
401 | } | |
402 | } | |
403 | } | |
404 | ||
8619129f UD |
405 | puts ("tests completed, now cleaning up"); |
406 | ||
67600288 UD |
407 | /* Clean up */ |
408 | for (i = 0; globfile[i]; ++i) | |
409 | remove (globfile[i]); | |
410 | ||
22bc7978 UD |
411 | if (cwd == NULL) |
412 | cwd = ".."; | |
67600288 UD |
413 | |
414 | chdir (cwd); | |
415 | rmdir (tmpdir); | |
416 | ||
8619129f UD |
417 | printf ("tests failed: %d\n", fail); |
418 | ||
ac16e905 UD |
419 | return fail != 0; |
420 | } | |
421 | ||
36103ba2 PP |
422 | static const char * |
423 | at_page_end (const char *words) | |
424 | { | |
425 | const int pagesize = getpagesize (); | |
426 | char *start = mmap (0, 2 * pagesize, PROT_READ|PROT_WRITE, | |
427 | MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); | |
428 | ||
429 | if (start == MAP_FAILED) | |
430 | return start; | |
431 | ||
432 | if (mprotect (start + pagesize, pagesize, PROT_NONE)) | |
433 | { | |
434 | munmap (start, 2 * pagesize); | |
435 | return MAP_FAILED; | |
436 | } | |
437 | ||
438 | /* Includes terminating NUL. */ | |
439 | const size_t words_size = strlen (words) + 1; | |
440 | char *words_start = start + pagesize - words_size; | |
441 | memcpy (words_start, words, words_size); | |
442 | ||
443 | return words_start; | |
444 | } | |
ac16e905 UD |
445 | |
446 | static int | |
447 | testit (struct test_case_struct *tc) | |
448 | { | |
ac16e905 | 449 | int retval; |
52aec7d1 UD |
450 | wordexp_t we, sav_we; |
451 | char *dummy; | |
ac16e905 | 452 | int bzzzt = 0; |
52aec7d1 | 453 | int start_offs = 0; |
ac16e905 UD |
454 | int i; |
455 | ||
456 | if (tc->env) | |
457 | setenv ("var", tc->env, 1); | |
458 | else | |
459 | unsetenv ("var"); | |
460 | ||
6ca96fe2 UD |
461 | if (tc->ifs) |
462 | setenv ("IFS", tc->ifs, 1); | |
463 | else | |
464 | unsetenv ("IFS"); | |
465 | ||
52aec7d1 UD |
466 | sav_we.we_wordc = 99; |
467 | sav_we.we_wordv = &dummy; | |
468 | sav_we.we_offs = 3; | |
469 | we = sav_we; | |
470 | ||
8619129f | 471 | printf ("Test %d (%s): ", ++tests, tc->words); |
36103ba2 PP |
472 | fflush (NULL); |
473 | const char *words = at_page_end (tc->words); | |
52aec7d1 | 474 | |
a39208bd CD |
475 | if (tc->flags & WRDE_NOCMD) |
476 | registered_forks = 0; | |
477 | ||
52aec7d1 UD |
478 | if (tc->flags & WRDE_APPEND) |
479 | { | |
480 | /* initial wordexp() call, to be appended to */ | |
481 | if (wordexp ("pre1 pre2", &we, tc->flags & ~WRDE_APPEND) != 0) | |
482 | { | |
483 | printf ("FAILED setup\n"); | |
484 | return 1; | |
485 | } | |
486 | } | |
36103ba2 | 487 | retval = wordexp (words, &we, tc->flags); |
ac16e905 | 488 | |
a39208bd CD |
489 | if ((tc->flags & WRDE_NOCMD) |
490 | && (registered_forks > 0)) | |
491 | { | |
492 | printf ("FAILED fork called for WRDE_NOCMD\n"); | |
493 | return 1; | |
494 | } | |
495 | ||
52aec7d1 UD |
496 | if (tc->flags & WRDE_DOOFFS) |
497 | start_offs = sav_we.we_offs; | |
498 | ||
22bc7978 | 499 | if (retval != tc->retval || (retval == 0 && we.we_wordc != tc->wordc)) |
ac16e905 | 500 | bzzzt = 1; |
52aec7d1 UD |
501 | else if (retval == 0) |
502 | { | |
503 | for (i = 0; i < start_offs; ++i) | |
504 | if (we.we_wordv[i] != NULL) | |
505 | { | |
506 | bzzzt = 1; | |
507 | break; | |
508 | } | |
509 | ||
510 | for (i = 0; i < we.we_wordc; ++i) | |
511 | if (we.we_wordv[i+start_offs] == NULL || | |
512 | strcmp (tc->wordv[i], we.we_wordv[i+start_offs]) != 0) | |
513 | { | |
514 | bzzzt = 1; | |
515 | break; | |
516 | } | |
517 | } | |
b5efde2f | 518 | |
ac16e905 UD |
519 | if (bzzzt) |
520 | { | |
521 | printf ("FAILED\n"); | |
00b16c4a | 522 | printf ("Test words: <%s>, need retval %d, wordc %Zd\n", |
ac16e905 | 523 | tc->words, tc->retval, tc->wordc); |
52aec7d1 UD |
524 | if (start_offs != 0) |
525 | printf ("(preceded by %d NULLs)\n", start_offs); | |
00b16c4a | 526 | printf ("Got retval %d, wordc %Zd: ", retval, we.we_wordc); |
52aec7d1 UD |
527 | if (retval == 0 || retval == WRDE_NOSPACE) |
528 | { | |
529 | for (i = 0; i < we.we_wordc + start_offs; ++i) | |
530 | if (we.we_wordv[i] == NULL) | |
531 | printf ("NULL "); | |
532 | else | |
533 | printf ("<%s> ", we.we_wordv[i]); | |
534 | } | |
ac16e905 | 535 | printf ("\n"); |
b5efde2f | 536 | } |
52aec7d1 UD |
537 | else if (retval != 0 && retval != WRDE_NOSPACE && |
538 | (we.we_wordc != sav_we.we_wordc || | |
539 | we.we_wordv != sav_we.we_wordv || | |
540 | we.we_offs != sav_we.we_offs)) | |
541 | { | |
542 | bzzzt = 1; | |
543 | printf ("FAILED to restore wordexp_t members\n"); | |
544 | } | |
ac16e905 UD |
545 | else |
546 | printf ("OK\n"); | |
547 | ||
a8125d85 UD |
548 | if (retval == 0 || retval == WRDE_NOSPACE) |
549 | wordfree (&we); | |
b5efde2f | 550 | |
36103ba2 PP |
551 | const int page_size = getpagesize (); |
552 | char *start = (char *) PTR_ALIGN_DOWN (words, page_size); | |
553 | ||
554 | if (munmap (start, 2 * page_size) != 0) | |
555 | return 1; | |
556 | ||
557 | fflush (NULL); | |
ac16e905 | 558 | return bzzzt; |
b5efde2f | 559 | } |