PR bin/43469 - correctly handle quoting of the pattern part of ${var%pat} type expansions. (from kre)diff -r1.93 -r1.94 src/bin/sh/expand.c
(christos)
--- src/bin/sh/expand.c 2015/08/27 07:46:47 1.93
+++ src/bin/sh/expand.c 2016/02/22 20:02:00 1.94
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: expand.c,v 1.93 2015/08/27 07:46:47 christos Exp $ */ | 1 | /* $NetBSD: expand.c,v 1.94 2016/02/22 20:02:00 christos Exp $ */ | |
2 | 2 | |||
3 | /*- | 3 | /*- | |
4 | * Copyright (c) 1991, 1993 | 4 | * Copyright (c) 1991, 1993 | |
5 | * The Regents of the University of California. All rights reserved. | 5 | * The Regents of the University of California. All rights reserved. | |
6 | * | 6 | * | |
7 | * This code is derived from software contributed to Berkeley by | 7 | * This code is derived from software contributed to Berkeley by | |
8 | * Kenneth Almquist. | 8 | * Kenneth Almquist. | |
9 | * | 9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | 10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | 11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | 12 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | 13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | 14 | * notice, this list of conditions and the following disclaimer. | |
@@ -27,27 +27,27 @@ | @@ -27,27 +27,27 @@ | |||
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
32 | * SUCH DAMAGE. | 32 | * SUCH DAMAGE. | |
33 | */ | 33 | */ | |
34 | 34 | |||
35 | #include <sys/cdefs.h> | 35 | #include <sys/cdefs.h> | |
36 | #ifndef lint | 36 | #ifndef lint | |
37 | #if 0 | 37 | #if 0 | |
38 | static char sccsid[] = "@(#)expand.c 8.5 (Berkeley) 5/15/95"; | 38 | static char sccsid[] = "@(#)expand.c 8.5 (Berkeley) 5/15/95"; | |
39 | #else | 39 | #else | |
40 | __RCSID("$NetBSD: expand.c,v 1.93 2015/08/27 07:46:47 christos Exp $"); | 40 | __RCSID("$NetBSD: expand.c,v 1.94 2016/02/22 20:02:00 christos Exp $"); | |
41 | #endif | 41 | #endif | |
42 | #endif /* not lint */ | 42 | #endif /* not lint */ | |
43 | 43 | |||
44 | #include <sys/types.h> | 44 | #include <sys/types.h> | |
45 | #include <sys/time.h> | 45 | #include <sys/time.h> | |
46 | #include <sys/stat.h> | 46 | #include <sys/stat.h> | |
47 | #include <errno.h> | 47 | #include <errno.h> | |
48 | #include <dirent.h> | 48 | #include <dirent.h> | |
49 | #include <unistd.h> | 49 | #include <unistd.h> | |
50 | #include <pwd.h> | 50 | #include <pwd.h> | |
51 | #include <limits.h> | 51 | #include <limits.h> | |
52 | #include <stdlib.h> | 52 | #include <stdlib.h> | |
53 | #include <stdio.h> | 53 | #include <stdio.h> | |
@@ -501,27 +501,27 @@ subevalvar(char *p, char *str, int strlo | @@ -501,27 +501,27 @@ subevalvar(char *p, char *str, int strlo | |||
501 | char *loc = NULL; | 501 | char *loc = NULL; | |
502 | char *q; | 502 | char *q; | |
503 | int c = 0; | 503 | int c = 0; | |
504 | int saveherefd = herefd; | 504 | int saveherefd = herefd; | |
505 | struct nodelist *saveargbackq = argbackq; | 505 | struct nodelist *saveargbackq = argbackq; | |
506 | int amount, how; | 506 | int amount, how; | |
507 | 507 | |||
508 | herefd = -1; | 508 | herefd = -1; | |
509 | switch (subtype) { | 509 | switch (subtype) { | |
510 | case VSTRIMLEFT: | 510 | case VSTRIMLEFT: | |
511 | case VSTRIMLEFTMAX: | 511 | case VSTRIMLEFTMAX: | |
512 | case VSTRIMRIGHT: | 512 | case VSTRIMRIGHT: | |
513 | case VSTRIMRIGHTMAX: | 513 | case VSTRIMRIGHTMAX: | |
514 | how = (varflags & VSQUOTE) ? 0 : EXP_CASE; | 514 | how = (varflags & (VSQUOTE|VSPATQ)) == VSQUOTE ? 0 : EXP_CASE; | |
515 | break; | 515 | break; | |
516 | default: | 516 | default: | |
517 | how = 0; | 517 | how = 0; | |
518 | break; | 518 | break; | |
519 | } | 519 | } | |
520 | argstr(p, how); | 520 | argstr(p, how); | |
521 | STACKSTRNUL(expdest); | 521 | STACKSTRNUL(expdest); | |
522 | herefd = saveherefd; | 522 | herefd = saveherefd; | |
523 | argbackq = saveargbackq; | 523 | argbackq = saveargbackq; | |
524 | startp = stackblock() + startloc; | 524 | startp = stackblock() + startloc; | |
525 | if (str == NULL) | 525 | if (str == NULL) | |
526 | str = stackblock() + strloc; | 526 | str = stackblock() + strloc; | |
527 | 527 |
--- src/bin/sh/parser.c 2016/02/22 19:42:46 1.97
+++ src/bin/sh/parser.c 2016/02/22 20:02:00 1.98
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: parser.c,v 1.97 2016/02/22 19:42:46 christos Exp $ */ | 1 | /* $NetBSD: parser.c,v 1.98 2016/02/22 20:02:00 christos Exp $ */ | |
2 | 2 | |||
3 | /*- | 3 | /*- | |
4 | * Copyright (c) 1991, 1993 | 4 | * Copyright (c) 1991, 1993 | |
5 | * The Regents of the University of California. All rights reserved. | 5 | * The Regents of the University of California. All rights reserved. | |
6 | * | 6 | * | |
7 | * This code is derived from software contributed to Berkeley by | 7 | * This code is derived from software contributed to Berkeley by | |
8 | * Kenneth Almquist. | 8 | * Kenneth Almquist. | |
9 | * | 9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | 10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | 11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | 12 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | 13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | 14 | * notice, this list of conditions and the following disclaimer. | |
@@ -27,27 +27,27 @@ | @@ -27,27 +27,27 @@ | |||
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
32 | * SUCH DAMAGE. | 32 | * SUCH DAMAGE. | |
33 | */ | 33 | */ | |
34 | 34 | |||
35 | #include <sys/cdefs.h> | 35 | #include <sys/cdefs.h> | |
36 | #ifndef lint | 36 | #ifndef lint | |
37 | #if 0 | 37 | #if 0 | |
38 | static char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95"; | 38 | static char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95"; | |
39 | #else | 39 | #else | |
40 | __RCSID("$NetBSD: parser.c,v 1.97 2016/02/22 19:42:46 christos Exp $"); | 40 | __RCSID("$NetBSD: parser.c,v 1.98 2016/02/22 20:02:00 christos Exp $"); | |
41 | #endif | 41 | #endif | |
42 | #endif /* not lint */ | 42 | #endif /* not lint */ | |
43 | 43 | |||
44 | #include <stdio.h> | 44 | #include <stdio.h> | |
45 | #include <stdlib.h> | 45 | #include <stdlib.h> | |
46 | #include <limits.h> | 46 | #include <limits.h> | |
47 | 47 | |||
48 | #include "shell.h" | 48 | #include "shell.h" | |
49 | #include "parser.h" | 49 | #include "parser.h" | |
50 | #include "nodes.h" | 50 | #include "nodes.h" | |
51 | #include "expand.h" /* defines rmescapes() */ | 51 | #include "expand.h" /* defines rmescapes() */ | |
52 | #include "eval.h" /* defines commandname */ | 52 | #include "eval.h" /* defines commandname */ | |
53 | #include "redir.h" /* defines copyfd() */ | 53 | #include "redir.h" /* defines copyfd() */ | |
@@ -1507,26 +1507,28 @@ badsub: | @@ -1507,26 +1507,28 @@ badsub: | |||
1507 | c = pgetc(); | 1507 | c = pgetc(); | |
1508 | if (c == cc) | 1508 | if (c == cc) | |
1509 | subtype++; | 1509 | subtype++; | |
1510 | else | 1510 | else | |
1511 | pungetc(); | 1511 | pungetc(); | |
1512 | break; | 1512 | break; | |
1513 | } | 1513 | } | |
1514 | } | 1514 | } | |
1515 | } else { | 1515 | } else { | |
1516 | pungetc(); | 1516 | pungetc(); | |
1517 | } | 1517 | } | |
1518 | if (ISDBLQUOTE() || arinest) | 1518 | if (ISDBLQUOTE() || arinest) | |
1519 | flags |= VSQUOTE; | 1519 | flags |= VSQUOTE; | |
1520 | if (subtype >= VSTRIMLEFT && subtype <= VSTRIMRIGHTMAX) | |||
1521 | flags |= VSPATQ; | |||
1520 | *(stackblock() + typeloc) = subtype | flags; | 1522 | *(stackblock() + typeloc) = subtype | flags; | |
1521 | if (subtype != VSNORMAL) { | 1523 | if (subtype != VSNORMAL) { | |
1522 | TS_PUSH(); | 1524 | TS_PUSH(); | |
1523 | varnest++; | 1525 | varnest++; | |
1524 | arinest = 0; | 1526 | arinest = 0; | |
1525 | if (subtype > VSASSIGN) { /* # ## % %% */ | 1527 | if (subtype > VSASSIGN) { /* # ## % %% */ | |
1526 | syntax = BASESYNTAX; | 1528 | syntax = BASESYNTAX; | |
1527 | CLRDBLQUOTE(); | 1529 | CLRDBLQUOTE(); | |
1528 | } | 1530 | } | |
1529 | } | 1531 | } | |
1530 | } | 1532 | } | |
1531 | goto parsesub_return; | 1533 | goto parsesub_return; | |
1532 | } | 1534 | } |
--- src/bin/sh/parser.h 2013/10/02 19:52:58 1.18
+++ src/bin/sh/parser.h 2016/02/22 20:02:00 1.19
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: parser.h,v 1.18 2013/10/02 19:52:58 christos Exp $ */ | 1 | /* $NetBSD: parser.h,v 1.19 2016/02/22 20:02:00 christos Exp $ */ | |
2 | 2 | |||
3 | /*- | 3 | /*- | |
4 | * Copyright (c) 1991, 1993 | 4 | * Copyright (c) 1991, 1993 | |
5 | * The Regents of the University of California. All rights reserved. | 5 | * The Regents of the University of California. All rights reserved. | |
6 | * | 6 | * | |
7 | * This code is derived from software contributed to Berkeley by | 7 | * This code is derived from software contributed to Berkeley by | |
8 | * Kenneth Almquist. | 8 | * Kenneth Almquist. | |
9 | * | 9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | 10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | 11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | 12 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | 13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | 14 | * notice, this list of conditions and the following disclaimer. | |
@@ -43,26 +43,27 @@ | @@ -43,26 +43,27 @@ | |||
43 | #define CTLQUOTE 01 /* ored with CTLBACKQ code if in quotes */ | 43 | #define CTLQUOTE 01 /* ored with CTLBACKQ code if in quotes */ | |
44 | /* CTLBACKQ | CTLQUOTE == '\205' */ | 44 | /* CTLBACKQ | CTLQUOTE == '\205' */ | |
45 | #define CTLARI '\206' /* arithmetic expression */ | 45 | #define CTLARI '\206' /* arithmetic expression */ | |
46 | #define CTLENDARI '\207' | 46 | #define CTLENDARI '\207' | |
47 | #define CTLQUOTEMARK '\210' | 47 | #define CTLQUOTEMARK '\210' | |
48 | #define CTLQUOTEEND '\211' /* only inside ${...} */ | 48 | #define CTLQUOTEEND '\211' /* only inside ${...} */ | |
49 | #define CTL_LAST '\211' /* last 'special' character */ | 49 | #define CTL_LAST '\211' /* last 'special' character */ | |
50 | 50 | |||
51 | /* variable substitution byte (follows CTLVAR) */ | 51 | /* variable substitution byte (follows CTLVAR) */ | |
52 | #define VSTYPE 0x0f /* type of variable substitution */ | 52 | #define VSTYPE 0x0f /* type of variable substitution */ | |
53 | #define VSNUL 0x10 /* colon--treat the empty string as unset */ | 53 | #define VSNUL 0x10 /* colon--treat the empty string as unset */ | |
54 | #define VSLINENO 0x20 /* expansion of $LINENO, the line number | 54 | #define VSLINENO 0x20 /* expansion of $LINENO, the line number | |
55 | follows immediately */ | 55 | follows immediately */ | |
56 | #define VSPATQ 0x40 /* ensure correct pattern quoting in ${x#pat} */ | |||
56 | #define VSQUOTE 0x80 /* inside double quotes--suppress splitting */ | 57 | #define VSQUOTE 0x80 /* inside double quotes--suppress splitting */ | |
57 | 58 | |||
58 | /* values of VSTYPE field */ | 59 | /* values of VSTYPE field */ | |
59 | #define VSNORMAL 0x1 /* normal variable: $var or ${var} */ | 60 | #define VSNORMAL 0x1 /* normal variable: $var or ${var} */ | |
60 | #define VSMINUS 0x2 /* ${var-text} */ | 61 | #define VSMINUS 0x2 /* ${var-text} */ | |
61 | #define VSPLUS 0x3 /* ${var+text} */ | 62 | #define VSPLUS 0x3 /* ${var+text} */ | |
62 | #define VSQUESTION 0x4 /* ${var?message} */ | 63 | #define VSQUESTION 0x4 /* ${var?message} */ | |
63 | #define VSASSIGN 0x5 /* ${var=text} */ | 64 | #define VSASSIGN 0x5 /* ${var=text} */ | |
64 | #define VSTRIMLEFT 0x6 /* ${var#pattern} */ | 65 | #define VSTRIMLEFT 0x6 /* ${var#pattern} */ | |
65 | #define VSTRIMLEFTMAX 0x7 /* ${var##pattern} */ | 66 | #define VSTRIMLEFTMAX 0x7 /* ${var##pattern} */ | |
66 | #define VSTRIMRIGHT 0x8 /* ${var%pattern} */ | 67 | #define VSTRIMRIGHT 0x8 /* ${var%pattern} */ | |
67 | #define VSTRIMRIGHTMAX 0x9 /* ${var%%pattern} */ | 68 | #define VSTRIMRIGHTMAX 0x9 /* ${var%%pattern} */ | |
68 | #define VSLENGTH 0xa /* ${#var} */ | 69 | #define VSLENGTH 0xa /* ${#var} */ |