Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/bin/sh Add sub tests to the quoting test case, to demo...



details:   https://anonhg.NetBSD.org/src/rev/027f0f7a0f3f
branches:  trunk
changeset: 447724:027f0f7a0f3f
user:      kre <kre%NetBSD.org@localhost>
date:      Tue Jan 22 14:31:53 2019 +0000

description:
Add sub tests to the quoting test case, to demonstrate a parsing
bug (and its fix (depending upon whether the test is run against
an unfixed, or fixed, shell).

An obvious indication of the failure is the following (one of the
new sub-tests)

p=A
cat <<EOF
${p+\%$p\%}
${p+%$p%}
EOF

which should output

\%A\%
%A%


as a here doc is treated as a double quoted string, except
that the " character is just a character.  In such a string,
the \ is only an escape character when the following character
is special, otherwise it represents a literal \ (which is the
case here).

An unfixed shell will omit the backslashes in the output.

It gets even more wrong if the % chars are replaced by "
(double quote) chars, which should make no difference, other
than the corresponding change, in the output.   But doesn't
(it doesn't even produce output broken in a similar way).

This one is a harder case to be specific about however,
as while the fixed (and expected in the test) output is what
is technically correct, only a few shells actually produce
it, most generate something different (but not all the same.)

diffstat:

 tests/bin/sh/t_here.sh |  49 ++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 44 insertions(+), 5 deletions(-)

diffs (70 lines):

diff -r 90afc697a862 -r 027f0f7a0f3f tests/bin/sh/t_here.sh
--- a/tests/bin/sh/t_here.sh    Tue Jan 22 13:48:28 2019 +0000
+++ b/tests/bin/sh/t_here.sh    Tue Jan 22 14:31:53 2019 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: t_here.sh,v 1.6 2016/03/31 16:21:52 christos Exp $
+# $NetBSD: t_here.sh,v 1.7 2019/01/22 14:31:53 kre Exp $
 #
 # Copyright (c) 2007 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -78,10 +78,13 @@
        rm -f "${TEMP_FILE}"
 
        # Remove newlines (use local shell for this)
-       oifs="$IFS"
-       IFS="$nl"
-       result="$(echo $result)"
-       IFS="$oifs"
+       result="$(
+               IFS="$nl"
+               set -f
+               set -- $result
+               IFS=' '
+               printf %s "$*"
+       )"
        if [ "$2" != "$result" ]
        then
                echo >&2 "[$TEST_NUM] Expected output '$2', received '$result'"
@@ -473,6 +476,42 @@
        EOF
        '       '5string1 line1?-line2string1 -line2 ""'\'\'' string1 66666' 0
 
+       # check that \ only quotes the magic chars, otherwise is retained
+       check 'p=A; cat <<-EOF
+               ${p+\%$p\%}
+               ${p+%$p%}
+       EOF
+       '       '\%A\% %A%' 0
+
+       # and check that " is not magic, so \ does not quote it
+       check 'p=A; cat <<-EOF
+               ${p+\"$p\"}
+               ${p+"$p"}
+       EOF
+       '       '\"A\" "A"' 0
+
+       # except in a ${var%<word>} word, base syntax reapplies, and
+       # there quotes are magic again
+       check 'p=ABCD; cat <<-EOF
+               ${p%B?D}
+               ${p%B\?D}
+               ${p%"BCD"}
+               "${p%??}"
+               ${p#"${p%??}"}
+               "${p#"${p%?"?"}"}"
+       EOF
+       '       'A ABCD A "AB" CD ""'   0
+
+       check 'p=AB??; cat <<-EOF
+               ${p%B?D}
+               ${p%B\??}
+               ${p%"B??"}
+               "${p%??}"
+               ${p#"${p%??}"}
+               "${p#"${p%?"?"}"}"
+       EOF
+       '       'AB?? A A "AB" ?? "??"' 0
+
        results
 }
 



Home | Main Index | Thread Index | Old Index