]> Cypherpunks repositories - gostls13.git/commitdiff
os: treat "${}" in Expand like in Go 1.10
authorDaniel Martí <mvdan@mvdan.cc>
Fri, 29 Jun 2018 15:59:04 +0000 (16:59 +0100)
committerDaniel Martí <mvdan@mvdan.cc>
Fri, 29 Jun 2018 21:07:28 +0000 (21:07 +0000)
CL 103055 made it so that invalid parameter expansions, like "$|", did
not make the dollar sign silently disappear.

A few edge cases were not taken into account, such as "${}" and "${",
which were now printing just "$". For consistency and to not break
existing programs, go back to eating up the characters when invalid
syntax is encountered.

For completeness, add a "$" test case too, even though its behavior is
unchanged by this CL.

Fixes #26135.

Change-Id: I5d25db9a8356dc6047a8502e318355113a99b247
Reviewed-on: https://go-review.googlesource.com/121636
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/os/env.go
src/os/env_test.go

index 544c03446f50113814687cc0ddf9e72e4e713075..330297b36ac58ce81f67fdef6e8b5dd1b88b0200 100644 (file)
@@ -24,8 +24,12 @@ func Expand(s string, mapping func(string) string) string {
                        }
                        buf = append(buf, s[i:j]...)
                        name, w := getShellName(s[j+1:])
-                       // If the name is empty, keep the $.
-                       if name == "" {
+                       if name == "" && w > 0 {
+                               // Encountered invalid syntax; eat the
+                               // characters.
+                       } else if name == "" {
+                               // Valid syntax, but $ was not followed by a
+                               // name. Leave the dollar character untouched.
                                buf = append(buf, s[j])
                        } else {
                                buf = append(buf, mapping(name)...)
@@ -74,10 +78,13 @@ func getShellName(s string) (string, int) {
                // Scan to closing brace
                for i := 1; i < len(s); i++ {
                        if s[i] == '}' {
+                               if i == 1 {
+                                       return "", 2 // Bad syntax; eat "${}"
+                               }
                                return s[1:i], i + 1
                        }
                }
-               return "", 1 // Bad syntax; just eat the brace.
+               return "", 1 // Bad syntax; eat "${"
        case isShellSpecialVar(s[0]):
                return s[0:1], 1
        }
index 218205e7c3ffdb4a43bbf92ad9ae7cde36ddafbe..4b860157b4c24257cf5e4989b0941ced17a7b088 100644 (file)
@@ -51,6 +51,10 @@ var expandTests = []struct {
        {"A$$$#$1$H$home_1*B", "APIDNARGSARGUMENT1(Value of H)/usr/foo*B"},
        {"start$+middle$^end$", "start$+middle$^end$"},
        {"mixed$|bag$$$", "mixed$|bagPID$"},
+       {"$", "$"},
+       {"$}", "$}"},
+       {"${", ""},  // invalid syntax; eat up the characters
+       {"${}", ""}, // invalid syntax; eat up the characters
 }
 
 func TestExpand(t *testing.T) {