1 /* Copyright (C) 1997-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
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.
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
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
19 #include <sys/types.h>
30 extern void *__dso_handle
__attribute__ ((__weak__
, __visibility__ ("hidden")));
31 extern int __register_atfork (void (*) (void), void (*) (void), void (*) (void), void *);
33 static int __app_register_atfork (void (*prepare
) (void), void (*parent
) (void), void (*child
) (void))
35 return __register_atfork (prepare
, parent
, child
,
36 &__dso_handle
== NULL
? NULL
: __dso_handle
);
39 /* Number of forks seen. */
40 static int registered_forks
;
42 /* For each fork increment the fork count. */
49 struct test_case_struct
56 const char *wordv
[10];
60 /* Simple word- and field-splitting */
61 { 0, NULL
, "one", 0, 1, { "one", }, IFS
},
62 { 0, NULL
, "one two", 0, 2, { "one", "two", }, IFS
},
63 { 0, NULL
, "one two three", 0, 3, { "one", "two", "three", }, IFS
},
64 { 0, NULL
, " \tfoo\t\tbar ", 0, 2, { "foo", "bar", }, IFS
},
65 { 0, NULL
, "red , white blue", 0, 4, { "red", ",", "white", "blue", }, " ," },
66 { 0, NULL
, "one two three", 0, 3, { "one", "two", "three", }, "" },
67 { 0, NULL
, "one \"two three\"", 0, 2, { "one", "two three", }, IFS
},
68 { 0, NULL
, "one \"two three\"", 0, 2, { "one", "two three", }, "" },
69 { 0, "two three", "one \"$var\"", 0, 2, { "one", "two three", }, IFS
},
70 { 0, "two three", "one $var", 0, 3, { "one", "two", "three", }, IFS
},
71 { 0, "two three", "one \"$var\"", 0, 2, { "one", "two three", }, "" },
72 { 0, "two three", "one $var", 0, 2, { "one", "two three", }, "" },
74 /* The non-whitespace IFS char at the end delimits the second field
75 * but does NOT start a new field. */
76 { 0, ":abc:", "$var", 0, 2, { "", "abc", }, ":" },
78 { 0, NULL
, "$(echo :abc:)", 0, 2, { "", "abc", }, ":" },
79 { 0, NULL
, "$(echo :abc:\\ )", 0, 2, { "", "abc", }, ": " },
80 { 0, NULL
, "$(echo :abc\\ )", 0, 2, { "", "abc", }, ": " },
81 { 0, ":abc:", "$(echo $var)", 0, 2, { "", "abc", }, ":" },
82 { 0, NULL
, ":abc:", 0, 1, { ":abc:", }, ":" },
83 { 0, NULL
, "$(echo :abc:)def", 0, 3, { "", "abc", "def", },
85 { 0, NULL
, "$(echo abc:de)f", 0, 2, { "abc", "def", }, ":" },
86 { 0, NULL
, "$(echo abc:de)f:ghi", 0, 2, { "abc", "def:ghi", },
88 { 0, NULL
, "abc:d$(echo ef:ghi)", 0, 2, { "abc:def", "ghi", },
90 { 0, "abc:", "$var$(echo def:ghi)", 0, 3, { "abc", "def",
92 { 0, "abc:d", "$var$(echo ef:ghi)", 0, 3, { "abc", "def",
94 { 0, "def:ghi", "$(echo abc:)$var", 0, 3, { "abc", "def",
96 { 0, "ef:ghi", "$(echo abc:d)$var", 0, 3, { "abc", "def",
99 /* Simple parameter expansion */
100 { 0, "foo", "${var}", 0, 1, { "foo", }, IFS
},
101 { 0, "foo", "$var", 0, 1, { "foo", }, IFS
},
102 { 0, "foo", "\\\"$var\\\"", 0, 1, { "\"foo\"", }, IFS
},
103 { 0, "foo", "%$var%", 0, 1, { "%foo%", }, IFS
},
104 { 0, "foo", "-$var-", 0, 1, { "-foo-", }, IFS
},
106 /* Simple quote removal */
107 { 0, NULL
, "\"quoted\"", 0, 1, { "quoted", }, IFS
},
108 { 0, "foo", "\"$var\"\"$var\"", 0, 1, { "foofoo", }, IFS
},
109 { 0, NULL
, "'singly-quoted'", 0, 1, { "singly-quoted", }, IFS
},
110 { 0, NULL
, "contin\\\nuation", 0, 1, { "continuation", }, IFS
},
111 { 0, NULL
, "explicit ''", 0, 2, { "explicit", "", }, IFS
},
112 { 0, NULL
, "explicit \"\"", 0, 2, { "explicit", "", }, IFS
},
113 { 0, NULL
, "explicit ``", 0, 1, { "explicit", }, IFS
},
115 /* Simple command substitution */
116 { 0, NULL
, "$(echo hello)", 0, 1, { "hello", }, IFS
},
117 { 0, NULL
, "$( (echo hello) )", 0, 1, { "hello", }, IFS
},
118 { 0, NULL
, "$((echo hello);(echo there))", 0, 2, { "hello", "there", }, IFS
},
119 { 0, NULL
, "`echo one two`", 0, 2, { "one", "two", }, IFS
},
120 { 0, NULL
, "$(echo ')')", 0, 1, { ")" }, IFS
},
121 { 0, NULL
, "$(echo hello; echo)", 0, 1, { "hello", }, IFS
},
122 { 0, NULL
, "a$(echo b)c", 0, 1, { "abc", }, IFS
},
124 /* Simple arithmetic expansion */
125 { 0, NULL
, "$((1 + 1))", 0, 1, { "2", }, IFS
},
126 { 0, NULL
, "$((2-3))", 0, 1, { "-1", }, IFS
},
127 { 0, NULL
, "$((-1))", 0, 1, { "-1", }, IFS
},
128 { 0, NULL
, "$[50+20]", 0, 1, { "70", }, IFS
},
129 { 0, NULL
, "$(((2+3)*(4+5)))", 0, 1, { "45", }, IFS
},
130 { 0, NULL
, "$((010))", 0, 1, { "8" }, IFS
},
131 { 0, NULL
, "$((0x10))", 0, 1, { "16" }, IFS
},
132 { 0, NULL
, "$((010+0x10))", 0, 1, { "24" }, IFS
},
133 { 0, NULL
, "$((-010+0x10))", 0, 1, { "8" }, IFS
},
134 { 0, NULL
, "$((-0x10+010))", 0, 1, { "-8" }, IFS
},
136 /* Advanced parameter expansion */
137 { 0, NULL
, "${var:-bar}", 0, 1, { "bar", }, IFS
},
138 { 0, NULL
, "${var-bar}", 0, 1, { "bar", }, IFS
},
139 { 0, "", "${var:-bar}", 0, 1, { "bar", }, IFS
},
140 { 0, "foo", "${var:-bar}", 0, 1, { "foo", }, IFS
},
141 { 0, "", "${var-bar}", 0, 0, { NULL
, }, IFS
},
142 { 0, NULL
, "${var:=bar}", 0, 1, { "bar", }, IFS
},
143 { 0, NULL
, "${var=bar}", 0, 1, { "bar", }, IFS
},
144 { 0, "", "${var:=bar}", 0, 1, { "bar", }, IFS
},
145 { 0, "foo", "${var:=bar}", 0, 1, { "foo", }, IFS
},
146 { 0, "", "${var=bar}", 0, 0, { NULL
, }, IFS
},
147 { 0, "foo", "${var:?bar}", 0, 1, { "foo", }, IFS
},
148 { 0, NULL
, "${var:+bar}", 0, 0, { NULL
, }, IFS
},
149 { 0, NULL
, "${var+bar}", 0, 0, { NULL
, }, IFS
},
150 { 0, "", "${var:+bar}", 0, 0, { NULL
, }, IFS
},
151 { 0, "foo", "${var:+bar}", 0, 1, { "bar", }, IFS
},
152 { 0, "", "${var+bar}", 0, 1, { "bar", }, IFS
},
153 { 0, "12345", "${#var}", 0, 1, { "5", }, IFS
},
154 { 0, NULL
, "${var:-'}'}", 0, 1, { "}", }, IFS
},
155 { 0, NULL
, "${var-}", 0, 0, { NULL
}, IFS
},
157 { 0, "pizza", "${var#${var}}", 0, 0, { NULL
}, IFS
},
158 { 0, "pepperoni", "${var%$(echo oni)}", 0, 1, { "pepper" }, IFS
},
159 { 0, "6pack", "${var#$((6))}", 0, 1, { "pack" }, IFS
},
160 { 0, "b*witched", "${var##b*}", 0, 0, { NULL
}, IFS
},
161 { 0, "b*witched", "${var##\"b*\"}", 0, 1, { "witched" }, IFS
},
162 { 0, "banana", "${var%na*}", 0, 1, { "bana", }, IFS
},
163 { 0, "banana", "${var%%na*}", 0, 1, { "ba", }, IFS
},
164 { 0, "borabora-island", "${var#*bora}", 0, 1, { "bora-island", }, IFS
},
165 { 0, "borabora-island", "${var##*bora}", 0, 1, { "-island", }, IFS
},
166 { 0, "coconut", "${var##\\*co}", 0, 1, { "coconut", }, IFS
},
167 { 0, "100%", "${var%0%}", 0, 1, { "10" }, IFS
},
169 /* Pathname expansion */
170 { 0, NULL
, "???", 0, 2, { "one", "two", }, IFS
},
171 { 0, NULL
, "[ot]??", 0, 2, { "one", "two", }, IFS
},
172 { 0, NULL
, "t*", 0, 2, { "three", "two", }, IFS
},
173 { 0, NULL
, "\"t\"*", 0, 2, { "three", "two", }, IFS
},
175 /* Nested constructs */
176 { 0, "one two", "$var", 0, 2, { "one", "two", }, IFS
},
177 { 0, "one two three", "$var", 0, 3, { "one", "two", "three", }, IFS
},
178 { 0, " \tfoo\t\tbar ", "$var", 0, 2, { "foo", "bar", }, IFS
},
179 { 0, " red , white blue", "$var", 0, 3, { "red", "white", "blue", }, ", \n\t" },
180 { 0, " red , white blue", "\"$var\"", 0, 1, { " red , white blue", }, ", \n\t" },
181 { 0, NULL
, "\"$(echo hello there)\"", 0, 1, { "hello there", }, IFS
},
182 { 0, NULL
, "\"$(echo \"hello there\")\"", 0, 1, { "hello there", }, IFS
},
183 { 0, NULL
, "${var=one two} \"$var\"", 0, 3, { "one", "two", "one two", }, IFS
},
184 { 0, "1", "$(( $(echo 3)+$var ))", 0, 1, { "4", }, IFS
},
185 { 0, NULL
, "\"$(echo \"*\")\"", 0, 1, { "*", }, IFS
},
186 { 0, NULL
, "\"a\n\n$(echo)b\"", 0, 1, { "a\n\nb", }, IFS
},
187 { 0, "foo", "*$var*", 0, 1, { "*foo*", }, IFS
},
188 { 0, "o thr", "*$var*", 0, 2, { "two", "three" }, IFS
},
190 /* Different IFS values */
191 { 0, "a b\tc\nd ", "$var", 0, 4, { "a", "b", "c", "d" }, NULL
/* unset */ },
192 { 0, "a b\tc d ", "$var", 0, 1, { "a b\tc d " }, "" /* `null' */ },
193 { 0, "a,b c\n, d", "$var", 0, 3, { "a", "b c", " d" }, "\t\n," },
195 /* Other things that should succeed */
196 { 0, NULL
, "\\*\"|&;<>\"\\(\\)\\{\\}", 0, 1, { "*|&;<>(){}", }, IFS
},
197 { 0, "???", "$var", 0, 1, { "???", }, IFS
},
198 { 0, NULL
, "$var", 0, 0, { NULL
, }, IFS
},
199 { 0, NULL
, "\"\\n\"", 0, 1, { "\\n", }, IFS
},
200 { 0, NULL
, "", 0, 0, { NULL
, }, IFS
},
202 /* Flags not already covered (testit() has special handling for these) */
203 { 0, NULL
, "one two", WRDE_DOOFFS
, 2, { "one", "two", }, IFS
},
204 { 0, NULL
, "appended", WRDE_APPEND
, 3, { "pre1", "pre2", "appended", }, IFS
},
205 { 0, NULL
, "appended", WRDE_DOOFFS
|WRDE_APPEND
, 3, { "pre1", "pre2", "appended", }, IFS
},
207 /* Things that should fail */
208 { WRDE_BADCHAR
, NULL
, "new\nline", 0, 0, { NULL
, }, "" /* \n not IFS */ },
209 { WRDE_BADCHAR
, NULL
, "pipe|symbol", 0, 0, { NULL
, }, IFS
},
210 { WRDE_BADCHAR
, NULL
, "&ersand", 0, 0, { NULL
, }, IFS
},
211 { WRDE_BADCHAR
, NULL
, "semi;colon", 0, 0, { NULL
, }, IFS
},
212 { WRDE_BADCHAR
, NULL
, "<greater", 0, 0, { NULL
, }, IFS
},
213 { WRDE_BADCHAR
, NULL
, "less>", 0, 0, { NULL
, }, IFS
},
214 { WRDE_BADCHAR
, NULL
, "(open-paren", 0, 0, { NULL
, }, IFS
},
215 { WRDE_BADCHAR
, NULL
, "close-paren)", 0, 0, { NULL
, }, IFS
},
216 { WRDE_BADCHAR
, NULL
, "{open-brace", 0, 0, { NULL
, }, IFS
},
217 { WRDE_BADCHAR
, NULL
, "close-brace}", 0, 0, { NULL
, }, IFS
},
218 { WRDE_CMDSUB
, NULL
, "$(ls)", WRDE_NOCMD
, 0, { NULL
, }, IFS
},
219 { WRDE_BADVAL
, NULL
, "$var", WRDE_UNDEF
, 0, { NULL
, }, IFS
},
220 { WRDE_BADVAL
, NULL
, "$9", WRDE_UNDEF
, 0, { NULL
, }, IFS
},
221 { WRDE_SYNTAX
, NULL
, "$[50+20))", 0, 0, { NULL
, }, IFS
},
222 { WRDE_SYNTAX
, NULL
, "${%%noparam}", 0, 0, { NULL
, }, IFS
},
223 { WRDE_SYNTAX
, NULL
, "${missing-brace", 0, 0, { NULL
, }, IFS
},
224 { WRDE_SYNTAX
, NULL
, "$(for i in)", 0, 0, { NULL
, }, IFS
},
225 { WRDE_SYNTAX
, NULL
, "$((2+))", 0, 0, { NULL
, }, IFS
},
226 { WRDE_SYNTAX
, NULL
, "`", 0, 0, { NULL
, }, IFS
},
227 { WRDE_SYNTAX
, NULL
, "$((010+4+))", 0, 0, { NULL
}, IFS
},
228 /* Test for CVE-2014-7817. We test 3 combinations of command
229 substitution inside an arithmetic expression to make sure that
230 no commands are executed and error is returned. */
231 { WRDE_CMDSUB
, NULL
, "$((`echo 1`))", WRDE_NOCMD
, 0, { NULL
, }, IFS
},
232 { WRDE_CMDSUB
, NULL
, "$((1+`echo 1`))", WRDE_NOCMD
, 0, { NULL
, }, IFS
},
233 { WRDE_CMDSUB
, NULL
, "$((1+$((`echo 1`))))", WRDE_NOCMD
, 0, { NULL
, }, IFS
},
235 { -1, NULL
, NULL
, 0, 0, { NULL
, }, IFS
},
238 static int testit (struct test_case_struct
*tc
);
242 command_line_test (const char *words
)
246 int retval
= wordexp (words
, &we
, 0);
247 printf ("wordexp returned %d\n", retval
);
248 for (i
= 0; i
< we
.we_wordc
; i
++)
249 printf ("we_wordv[%d] = \"%s\"\n", i
, we
.we_wordv
[i
]);
253 main (int argc
, char *argv
[])
255 const char *globfile
[] = { "one", "two", "three", NULL
};
262 struct test_case_struct ts
;
266 command_line_test (argv
[1]);
270 cwd
= getcwd (NULL
, 0);
272 /* Set up arena for pathname expansion */
274 if (mkdir (tmpdir
, S_IRWXU
) || chdir (tmpdir
))
280 for (i
= 0; globfile
[i
]; ++i
)
281 if ((fd
= creat (globfile
[i
], S_IRUSR
| S_IWUSR
)) == -1
286 /* If we are not allowed to do command substitution, we install
287 fork handlers to verify that no forks happened. No forks should
288 happen at all if command substitution is disabled. */
289 if (__app_register_atfork (register_fork
, NULL
, NULL
) != 0)
291 printf ("Failed to register fork handler.\n");
295 for (test
= 0; test_case
[test
].retval
!= -1; test
++)
296 if (testit (&test_case
[test
]))
299 /* Tilde-expansion tests. */
300 pw
= getpwnam ("root");
308 ts
.wordv
[0] = pw
->pw_dir
;
316 ts
.words
= "${var#~root}x";
326 /* "~" expands to value of $HOME when HOME is set */
328 setenv ("HOME", "/dummy/home", 1);
331 ts
.words
= "~ ~/foo";
334 ts
.wordv
[0] = "/dummy/home";
335 ts
.wordv
[1] = "/dummy/home/foo";
341 /* "~" expands to home dir from passwd file if HOME is not set */
343 pw
= getpwuid (getuid ());
352 ts
.wordv
[0] = pw
->pw_dir
;
359 puts ("tests completed, now cleaning up");
362 for (i
= 0; globfile
[i
]; ++i
)
363 remove (globfile
[i
]);
371 printf ("tests failed: %d\n", fail
);
378 testit (struct test_case_struct
*tc
)
381 wordexp_t we
, sav_we
;
388 setenv ("var", tc
->env
, 1);
393 setenv ("IFS", tc
->ifs
, 1);
397 sav_we
.we_wordc
= 99;
398 sav_we
.we_wordv
= &dummy
;
402 printf ("Test %d (%s): ", ++tests
, tc
->words
);
404 if (tc
->flags
& WRDE_NOCMD
)
405 registered_forks
= 0;
407 if (tc
->flags
& WRDE_APPEND
)
409 /* initial wordexp() call, to be appended to */
410 if (wordexp ("pre1 pre2", &we
, tc
->flags
& ~WRDE_APPEND
) != 0)
412 printf ("FAILED setup\n");
416 retval
= wordexp (tc
->words
, &we
, tc
->flags
);
418 if ((tc
->flags
& WRDE_NOCMD
)
419 && (registered_forks
> 0))
421 printf ("FAILED fork called for WRDE_NOCMD\n");
425 if (tc
->flags
& WRDE_DOOFFS
)
426 start_offs
= sav_we
.we_offs
;
428 if (retval
!= tc
->retval
|| (retval
== 0 && we
.we_wordc
!= tc
->wordc
))
430 else if (retval
== 0)
432 for (i
= 0; i
< start_offs
; ++i
)
433 if (we
.we_wordv
[i
] != NULL
)
439 for (i
= 0; i
< we
.we_wordc
; ++i
)
440 if (we
.we_wordv
[i
+start_offs
] == NULL
||
441 strcmp (tc
->wordv
[i
], we
.we_wordv
[i
+start_offs
]) != 0)
451 printf ("Test words: <%s>, need retval %d, wordc %Zd\n",
452 tc
->words
, tc
->retval
, tc
->wordc
);
454 printf ("(preceded by %d NULLs)\n", start_offs
);
455 printf ("Got retval %d, wordc %Zd: ", retval
, we
.we_wordc
);
456 if (retval
== 0 || retval
== WRDE_NOSPACE
)
458 for (i
= 0; i
< we
.we_wordc
+ start_offs
; ++i
)
459 if (we
.we_wordv
[i
] == NULL
)
462 printf ("<%s> ", we
.we_wordv
[i
]);
466 else if (retval
!= 0 && retval
!= WRDE_NOSPACE
&&
467 (we
.we_wordc
!= sav_we
.we_wordc
||
468 we
.we_wordv
!= sav_we
.we_wordv
||
469 we
.we_offs
!= sav_we
.we_offs
))
472 printf ("FAILED to restore wordexp_t members\n");
477 if (retval
== 0 || retval
== WRDE_NOSPACE
)