1 /* Copyright (C) 1997-2022 Free Software Foundation, Inc.
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
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.
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
12    Lesser General Public License for more details.
13 
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    <https://www.gnu.org/licenses/>.  */
17 
18 #include <wordexp.h>
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <pwd.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/mman.h>
25 
26 #include <libc-pointer-arith.h>
27 #include <array_length.h>
28 #include <support/xunistd.h>
29 #include <support/check.h>
30 #include <support/next_to_fault.h>
31 
32 #define IFS " \n\t"
33 
34 struct test_case_struct
35 {
36   int retval;
37   const char *env;
38   const char *words;
39   int flags;
40   size_t wordc;
41   const char *wordv[10];
42   const char *ifs;
43 } static test_case[] =
44   {
45     /* Simple word- and field-splitting */
46     { 0, NULL, "one", 0, 1, { "one", }, IFS },
47     { 0, NULL, "one two", 0, 2, { "one", "two", }, IFS },
48     { 0, NULL, "one two three", 0, 3, { "one", "two", "three", }, IFS },
49     { 0, NULL, " \tfoo\t\tbar ", 0, 2, { "foo", "bar", }, IFS },
50     { 0, NULL, "red , white blue", 0, 4, { "red", ",", "white", "blue", }, " ," },
51     { 0, NULL, "one two three", 0, 3, { "one", "two", "three", }, "" },
52     { 0, NULL, "one \"two three\"", 0, 2, { "one", "two three", }, IFS },
53     { 0, NULL, "one \"two three\"", 0, 2, { "one", "two three", }, "" },
54     { 0, "two three", "one \"$var\"", 0, 2, { "one", "two three", }, IFS },
55     { 0, "two three", "one $var", 0, 3, { "one", "two", "three", }, IFS },
56     { 0, "two three", "one \"$var\"", 0, 2, { "one", "two three", }, "" },
57     { 0, "two three", "one $var", 0, 2, { "one", "two three", }, "" },
58 
59     /* The non-whitespace IFS char at the end delimits the second field
60      * but does NOT start a new field. */
61     { 0, ":abc:", "$var", 0, 2, { "", "abc", }, ":" },
62 
63     { 0, NULL, "$(echo :abc:)", 0, 2, { "", "abc", }, ":" },
64     { 0, NULL, "$(echo :abc:\\ )", 0, 2, { "", "abc", }, ": " },
65     { 0, NULL, "$(echo :abc\\ )", 0, 2, { "", "abc", }, ": " },
66     { 0, ":abc:", "$(echo $var)", 0, 2, { "", "abc", }, ":" },
67     { 0, NULL, ":abc:", 0, 1, { ":abc:", }, ":" },
68     { 0, NULL, "$(echo :abc:)def", 0, 3, { "", "abc", "def", },
69       ":" },
70     { 0, NULL, "$(echo abc:de)f", 0, 2, { "abc", "def", }, ":" },
71     { 0, NULL, "$(echo abc:de)f:ghi", 0, 2, { "abc", "def:ghi", },
72       ":" },
73     { 0, NULL, "abc:d$(echo ef:ghi)", 0, 2, { "abc:def", "ghi", },
74       ":" },
75     { 0, "abc:", "$var$(echo def:ghi)", 0, 3, { "abc", "def",
76 							  "ghi", }, ":" },
77     { 0, "abc:d", "$var$(echo ef:ghi)", 0, 3, { "abc", "def",
78 							  "ghi", }, ":" },
79     { 0, "def:ghi", "$(echo abc:)$var", 0, 3, { "abc", "def",
80 							  "ghi", }, ":" },
81     { 0, "ef:ghi", "$(echo abc:d)$var", 0, 3, { "abc", "def",
82 							  "ghi", }, ":" },
83 
84     /* Simple parameter expansion */
85     { 0, "foo", "${var}", 0, 1, { "foo", }, IFS },
86     { 0, "foo", "$var", 0, 1, { "foo", }, IFS },
87     { 0, "foo", "\\\"$var\\\"", 0, 1, { "\"foo\"", }, IFS },
88     { 0, "foo", "%$var%", 0, 1, { "%foo%", }, IFS },
89     { 0, "foo", "-$var-", 0, 1, { "-foo-", }, IFS },
90 
91     /* Simple quote removal */
92     { 0, NULL, "\"quoted\"", 0, 1, { "quoted", }, IFS },
93     { 0, "foo", "\"$var\"\"$var\"", 0, 1, { "foofoo", }, IFS },
94     { 0, NULL, "'singly-quoted'", 0, 1, { "singly-quoted", }, IFS },
95     { 0, NULL, "contin\\\nuation", 0, 1, { "continuation", }, IFS },
96     { 0, NULL, "explicit ''", 0, 2, { "explicit", "", }, IFS },
97     { 0, NULL, "explicit \"\"", 0, 2, { "explicit", "", }, IFS },
98     { 0, NULL, "explicit ``", 0, 1, { "explicit", }, IFS },
99 
100     /* Simple command substitution */
101     { 0, NULL, "$(echo hello)", 0, 1, { "hello", }, IFS },
102     { 0, NULL, "$( (echo hello) )", 0, 1, { "hello", }, IFS },
103     { 0, NULL, "$((echo hello);(echo there))", 0, 2, { "hello", "there", }, IFS },
104     { 0, NULL, "`echo one two`", 0, 2, { "one", "two", }, IFS },
105     { 0, NULL, "$(echo ')')", 0, 1, { ")" }, IFS },
106     { 0, NULL, "$(echo hello; echo)", 0, 1, { "hello", }, IFS },
107     { 0, NULL, "a$(echo b)c", 0, 1, { "abc", }, IFS },
108 
109     /* Simple arithmetic expansion */
110     { 0, NULL, "$((1 + 1))", 0, 1, { "2", }, IFS },
111     { 0, NULL, "$((2-3))", 0, 1, { "-1", }, IFS },
112     { 0, NULL, "$((-1))", 0, 1, { "-1", }, IFS },
113     { 0, NULL, "$[50+20]", 0, 1, { "70", }, IFS },
114     { 0, NULL, "$(((2+3)*(4+5)))", 0, 1, { "45", }, IFS },
115     { 0, NULL, "$((010))", 0, 1, { "8" }, IFS },
116     { 0, NULL, "$((0x10))", 0, 1, { "16" }, IFS },
117     { 0, NULL, "$((010+0x10))", 0, 1, { "24" }, IFS },
118     { 0, NULL, "$((-010+0x10))", 0, 1, { "8" }, IFS },
119     { 0, NULL, "$((-0x10+010))", 0, 1, { "-8" }, IFS },
120 
121     /* Advanced parameter expansion */
122     { 0, NULL, "${var:-bar}", 0, 1, { "bar", }, IFS },
123     { 0, NULL, "${var-bar}", 0, 1, { "bar", }, IFS },
124     { 0, "", "${var:-bar}", 0, 1, { "bar", }, IFS },
125     { 0, "foo", "${var:-bar}", 0, 1, { "foo", }, IFS },
126     { 0, "", "${var-bar}", 0, 0, { NULL, }, IFS },
127     { 0, NULL, "${var:=bar}", 0, 1, { "bar", }, IFS },
128     { 0, NULL, "${var=bar}", 0, 1, { "bar", }, IFS },
129     { 0, "", "${var:=bar}", 0, 1, { "bar", }, IFS },
130     { 0, "foo", "${var:=bar}", 0, 1, { "foo", }, IFS },
131     { 0, "", "${var=bar}", 0, 0, { NULL, }, IFS },
132     { 0, "foo", "${var:?bar}", 0, 1, { "foo", }, IFS },
133     { 0, NULL, "${var:+bar}", 0, 0, { NULL, }, IFS },
134     { 0, NULL, "${var+bar}", 0, 0, { NULL, }, IFS },
135     { 0, "", "${var:+bar}", 0, 0, { NULL, }, IFS },
136     { 0, "foo", "${var:+bar}", 0, 1, { "bar", }, IFS },
137     { 0, "", "${var+bar}", 0, 1, { "bar", }, IFS },
138     { 0, "12345", "${#var}", 0, 1, { "5", }, IFS },
139     { 0, NULL, "${var:-'}'}", 0, 1, { "}", }, IFS },
140     { 0, NULL, "${var-}", 0, 0, { NULL }, IFS },
141 
142     { 0, "pizza", "${var#${var}}", 0, 0, { NULL }, IFS },
143     { 0, "pepperoni", "${var%$(echo oni)}", 0, 1, { "pepper" }, IFS },
144     { 0, "6pack", "${var#$((6))}", 0, 1, { "pack" }, IFS },
145     { 0, "b*witched", "${var##b*}", 0, 0, { NULL }, IFS },
146     { 0, "b*witched", "${var##\"b*\"}", 0, 1, { "witched" }, IFS },
147     { 0, "banana", "${var%na*}", 0, 1, { "bana", }, IFS },
148     { 0, "banana", "${var%%na*}", 0, 1, { "ba", }, IFS },
149     { 0, "borabora-island", "${var#*bora}", 0, 1, { "bora-island", }, IFS },
150     { 0, "borabora-island", "${var##*bora}", 0, 1, { "-island", }, IFS },
151     { 0, "coconut", "${var##\\*co}", 0, 1, { "coconut", }, IFS },
152     { 0, "100%", "${var%0%}", 0, 1, { "10" }, IFS },
153 
154     /* Pathname expansion */
155     { 0, NULL, "???", 0, 2, { "one", "two", }, IFS },
156     { 0, NULL, "[ot]??", 0, 2, { "one", "two", }, IFS },
157     { 0, NULL, "t*", 0, 2, { "three", "two", }, IFS },
158     { 0, NULL, "\"t\"*", 0, 2, { "three", "two", }, IFS },
159 
160     /* Nested constructs */
161     { 0, "one two", "$var", 0, 2, { "one", "two", }, IFS },
162     { 0, "one two three", "$var", 0, 3, { "one", "two", "three", }, IFS },
163     { 0, " \tfoo\t\tbar ", "$var", 0, 2, { "foo", "bar", }, IFS },
164     { 0, "  red  , white blue", "$var", 0, 3, { "red", "white", "blue", }, ", \n\t" },
165     { 0, "  red  , white blue", "\"$var\"", 0, 1, { "  red  , white blue", }, ", \n\t" },
166     { 0, NULL, "\"$(echo hello there)\"", 0, 1, { "hello there", }, IFS },
167     { 0, NULL, "\"$(echo \"hello there\")\"", 0, 1, { "hello there", }, IFS },
168     { 0, NULL, "${var=one two} \"$var\"", 0, 3, { "one", "two", "one two", }, IFS },
169     { 0, "1", "$(( $(echo 3)+$var ))", 0, 1, { "4", }, IFS },
170     { 0, NULL, "\"$(echo \"*\")\"", 0, 1, { "*", }, IFS },
171     { 0, NULL, "\"a\n\n$(echo)b\"", 0, 1, { "a\n\nb", }, IFS },
172     { 0, "foo", "*$var*", 0, 1, { "*foo*", }, IFS },
173     { 0, "o thr", "*$var*", 0, 2, { "two", "three" }, IFS },
174 
175     /* Different IFS values */
176     { 0, "a b\tc\nd  ", "$var", 0, 4, { "a", "b", "c", "d" }, NULL /* unset */ },
177     { 0, "a b\tc d  ", "$var", 0, 1, { "a b\tc d  " }, "" /* `null' */ },
178     { 0, "a,b c\n, d", "$var", 0, 3, { "a", "b c", " d" }, "\t\n," },
179 
180     /* Other things that should succeed */
181     { 0, NULL, "\\*\"|&;<>\"\\(\\)\\{\\}", 0, 1, { "*|&;<>(){}", }, IFS },
182     { 0, "???", "$var", 0, 1, { "???", }, IFS },
183     { 0, NULL, "$var", 0, 0, { NULL, }, IFS },
184     { 0, NULL, "\"\\n\"", 0, 1, { "\\n", }, IFS },
185     { 0, NULL, "", 0, 0, { NULL, }, IFS },
186     { 0, NULL, "${1234567890123456789012}", 0, 0, { NULL, }, IFS },
187 
188     /* Flags not already covered (testit() has special handling for these) */
189     { 0, NULL, "one two", WRDE_DOOFFS, 2, { "one", "two", }, IFS },
190     { 0, NULL, "appended", WRDE_APPEND, 3, { "pre1", "pre2", "appended", }, IFS },
191     { 0, NULL, "appended", WRDE_DOOFFS|WRDE_APPEND, 3, { "pre1", "pre2", "appended", }, IFS },
192 
193     /* Things that should fail */
194     { WRDE_BADCHAR, NULL, "new\nline", 0, 0, { NULL, }, "" /* \n not IFS */ },
195     { WRDE_BADCHAR, NULL, "pipe|symbol", 0, 0, { NULL, }, IFS },
196     { WRDE_BADCHAR, NULL, "&ampersand", 0, 0, { NULL, }, IFS },
197     { WRDE_BADCHAR, NULL, "semi;colon", 0, 0, { NULL, }, IFS },
198     { WRDE_BADCHAR, NULL, "<greater", 0, 0, { NULL, }, IFS },
199     { WRDE_BADCHAR, NULL, "less>", 0, 0, { NULL, }, IFS },
200     { WRDE_BADCHAR, NULL, "(open-paren", 0, 0, { NULL, }, IFS },
201     { WRDE_BADCHAR, NULL, "close-paren)", 0, 0, { NULL, }, IFS },
202     { WRDE_BADCHAR, NULL, "{open-brace", 0, 0, { NULL, }, IFS },
203     { WRDE_BADCHAR, NULL, "close-brace}", 0, 0, { NULL, }, IFS },
204     { WRDE_BADVAL, NULL, "$var", WRDE_UNDEF, 0, { NULL, }, IFS },
205     { WRDE_BADVAL, NULL, "$9", WRDE_UNDEF, 0, { NULL, }, IFS },
206     { WRDE_SYNTAX, NULL, "$[50+20))", 0, 0, { NULL, }, IFS },
207     { WRDE_SYNTAX, NULL, "${%%noparam}", 0, 0, { NULL, }, IFS },
208     { WRDE_SYNTAX, NULL, "${missing-brace", 0, 0, { NULL, }, IFS },
209     { WRDE_SYNTAX, NULL, "$(for i in)", 0, 0, { NULL, }, IFS },
210     { WRDE_SYNTAX, NULL, "$((2+))", 0, 0, { NULL, }, IFS },
211     { WRDE_SYNTAX, NULL, "`", 0, 0, { NULL, }, IFS },
212     { WRDE_SYNTAX, NULL, "$((010+4+))", 0, 0, { NULL }, IFS },
213 
214     { WRDE_SYNTAX, NULL, "`\\", 0, 0, { NULL, }, IFS },     /* BZ 18042  */
215     { WRDE_SYNTAX, NULL, "${", 0, 0, { NULL, }, IFS },      /* BZ 18043  */
216     { WRDE_SYNTAX, NULL, "L${a:", 0, 0, { NULL, }, IFS },   /* BZ 18043#c4  */
217   };
218 
219 static int testit (struct test_case_struct *tc);
220 static int tests;
221 
222 static void
command_line_test(const char * words)223 command_line_test (const char *words)
224 {
225   wordexp_t we;
226   int i;
227   int retval = wordexp (words, &we, 0);
228   printf ("info: wordexp returned %d\n", retval);
229   for (i = 0; i < we.we_wordc; i++)
230     printf ("info: we_wordv[%d] = \"%s\"\n", i, we.we_wordv[i]);
231 }
232 
233 static int
do_test(int argc,char * argv[])234 do_test (int argc, char *argv[])
235 {
236   const char *globfile[] = { "one", "two", "three" };
237   char tmpdir[32];
238   struct passwd *pw;
239   const char *cwd;
240   int test;
241   struct test_case_struct ts;
242 
243   if (argc > 1)
244     {
245       command_line_test (argv[1]);
246       return 0;
247     }
248 
249   cwd = getcwd (NULL, 0);
250 
251   /* Set up arena for pathname expansion */
252   tmpnam (tmpdir);
253   xmkdir (tmpdir, S_IRWXU);
254   TEST_VERIFY_EXIT (chdir (tmpdir) == 0);
255 
256   for (int i = 0; i < array_length (globfile); ++i)
257     {
258       int fd = xopen (globfile[i], O_WRONLY|O_CREAT|O_TRUNC,
259 		      S_IRUSR | S_IWUSR);
260       xclose (fd);
261     }
262 
263   for (test = 0; test < array_length (test_case); test++)
264     TEST_COMPARE (testit (&test_case[test]), 0);
265 
266   /* Tilde-expansion tests. */
267   pw = getpwnam ("root");
268   if (pw != NULL)
269     {
270       ts.retval = 0;
271       ts.env = NULL;
272       ts.words = "~root ";
273       ts.flags = 0;
274       ts.wordc = 1;
275       ts.wordv[0] = pw->pw_dir;
276       ts.ifs = IFS;
277 
278       TEST_COMPARE (testit (&ts), 0);
279 
280       ts.retval = 0;
281       ts.env = pw->pw_dir;
282       ts.words = "${var#~root}x";
283       ts.flags = 0;
284       ts.wordc = 1;
285       ts.wordv[0] = "x";
286       ts.ifs = IFS;
287 
288       TEST_COMPARE (testit (&ts), 0);
289     }
290 
291   /* "~" expands to value of $HOME when HOME is set */
292 
293   setenv ("HOME", "/dummy/home", 1);
294   ts.retval = 0;
295   ts.env = NULL;
296   ts.words = "~ ~/foo";
297   ts.flags = 0;
298   ts.wordc = 2;
299   ts.wordv[0] = "/dummy/home";
300   ts.wordv[1] = "/dummy/home/foo";
301   ts.ifs = IFS;
302 
303   TEST_COMPARE (testit (&ts), 0);
304 
305   /* "~" expands to home dir from passwd file if HOME is not set */
306 
307   pw = getpwuid (getuid ());
308   if (pw != NULL)
309     {
310       unsetenv ("HOME");
311       ts.retval = 0;
312       ts.env = NULL;
313       ts.words = "~";
314       ts.flags = 0;
315       ts.wordc = 1;
316       ts.wordv[0] = pw->pw_dir;
317       ts.ifs = IFS;
318 
319       TEST_COMPARE (testit (&ts), 0);
320     }
321 
322   puts ("tests completed, now cleaning up");
323 
324   /* Clean up */
325   for (int i = 0; i < array_length (globfile); ++i)
326     remove (globfile[i]);
327 
328   if (cwd == NULL)
329     cwd = "..";
330 
331   chdir (cwd);
332   rmdir (tmpdir);
333 
334   return 0;
335 }
336 
337 struct support_next_to_fault
at_page_end(const char * words)338 at_page_end (const char *words)
339 {
340   const size_t words_size = strlen (words) + 1;
341   struct support_next_to_fault ntf
342     = support_next_to_fault_allocate (words_size);
343 
344   /* Includes terminating NUL.  */
345   memcpy (ntf.buffer, words, words_size);
346 
347   return ntf;
348 }
349 
350 static int
testit(struct test_case_struct * tc)351 testit (struct test_case_struct *tc)
352 {
353   int retval;
354   wordexp_t we, sav_we;
355   char *dummy;
356   int bzzzt = 0;
357   int start_offs = 0;
358   int i;
359 
360   if (tc->env)
361     setenv ("var", tc->env, 1);
362   else
363     unsetenv ("var");
364 
365   if (tc->ifs)
366     setenv ("IFS", tc->ifs, 1);
367   else
368     unsetenv ("IFS");
369 
370   sav_we.we_wordc = 99;
371   sav_we.we_wordv = &dummy;
372   sav_we.we_offs = 3;
373   we = sav_we;
374 
375   printf ("info: test %d (%s): ", ++tests, tc->words);
376   fflush (NULL);
377   struct support_next_to_fault words = at_page_end (tc->words);
378 
379   if (tc->flags & WRDE_APPEND)
380     {
381       /* initial wordexp() call, to be appended to */
382       if (wordexp ("pre1 pre2", &we, tc->flags & ~WRDE_APPEND) != 0)
383         {
384 	  printf ("info: FAILED setup\n");
385 	  return 1;
386 	}
387     }
388   retval = wordexp (words.buffer, &we, tc->flags);
389 
390   if (tc->flags & WRDE_DOOFFS)
391       start_offs = sav_we.we_offs;
392 
393   if (retval != tc->retval || (retval == 0 && we.we_wordc != tc->wordc))
394     bzzzt = 1;
395   else if (retval == 0)
396     {
397       for (i = 0; i < start_offs; ++i)
398 	if (we.we_wordv[i] != NULL)
399 	  {
400 	    bzzzt = 1;
401 	    break;
402 	  }
403 
404       for (i = 0; i < we.we_wordc; ++i)
405 	if (we.we_wordv[i+start_offs] == NULL
406 	    || strcmp (tc->wordv[i], we.we_wordv[i+start_offs]) != 0)
407 	  {
408 	    bzzzt = 1;
409 	    break;
410 	  }
411     }
412 
413   if (bzzzt)
414     {
415       printf ("FAILED\n");
416       printf ("info: Test words: <%s>, need retval %d, wordc %Zd\n",
417 	      tc->words, tc->retval, tc->wordc);
418       if (start_offs != 0)
419 	printf ("(preceded by %d NULLs)\n", start_offs);
420       printf ("Got retval %d, wordc %Zd: ", retval, we.we_wordc);
421       if (retval == 0 || retval == WRDE_NOSPACE)
422 	{
423 	  for (i = 0; i < we.we_wordc + start_offs; ++i)
424 	    if (we.we_wordv[i] == NULL)
425 	      printf ("NULL ");
426 	    else
427 	      printf ("<%s> ", we.we_wordv[i]);
428 	}
429       printf ("\n");
430     }
431   else if (retval != 0 && retval != WRDE_NOSPACE
432 	   && (we.we_wordc != sav_we.we_wordc
433 	       || we.we_wordv != sav_we.we_wordv
434 	       || we.we_offs != sav_we.we_offs))
435     {
436       bzzzt = 1;
437       printf ("FAILED to restore wordexp_t members\n");
438     }
439   else
440     printf ("OK\n");
441 
442   if (retval == 0 || retval == WRDE_NOSPACE)
443     wordfree (&we);
444 
445   support_next_to_fault_free (&words);
446 
447   fflush (NULL);
448   return bzzzt;
449 }
450 
451 #define TEST_FUNCTION_ARGV do_test
452 #include <support/test-driver.c>
453