]> Cypherpunks repositories - gostls13.git/commitdiff
os: keep the $ if a variable is not detected
authorAgniva De Sarker <agnivade@yahoo.co.in>
Wed, 28 Mar 2018 08:15:26 +0000 (13:45 +0530)
committerIan Lance Taylor <iant@golang.org>
Wed, 28 Mar 2018 18:25:41 +0000 (18:25 +0000)
If the character after $ cannot be detected as a valid
variable declaration, do not gobble the $.

Fixes #24345

Change-Id: Iec47be1f2e4f8147b8ceb64c30778eae8045b58f
Reviewed-on: https://go-review.googlesource.com/103055
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/os/env.go
src/os/env_test.go

index 4e0171f40830b1590dc3b4f7e3c31634a95a7021..b3b615cb336e745186166f0d6ffcb9fde4dac36a 100644 (file)
@@ -21,7 +21,12 @@ func Expand(s string, mapping func(string) string) string {
                if s[j] == '$' && j+1 < len(s) {
                        buf = append(buf, s[i:j]...)
                        name, w := getShellName(s[j+1:])
-                       buf = append(buf, mapping(name)...)
+                       // If the name is empty, keep the $.
+                       if name == "" {
+                               buf = append(buf, s[j])
+                       } else {
+                               buf = append(buf, mapping(name)...)
+                       }
                        j += w
                        i = j + 1
                }
index 16f194563811eed0495503e23bbe124c69ef9f1b..78c8fe57cb5d9a4c4b118c8c5e7ba44daaac2968 100644 (file)
@@ -49,6 +49,8 @@ var expandTests = []struct {
        {"${HOME}", "/usr/gopher"},
        {"${H}OME", "(Value of H)OME"},
        {"A$$$#$1$H$home_1*B", "APIDNARGSARGUMENT1(Value of H)/usr/foo*B"},
+       {"start$+middle$^end$", "start$+middle$^end$"},
+       {"mixed$|bag$$$", "mixed$|bagPID$"},
 }
 
 func TestExpand(t *testing.T) {