From dd931d9b48647e898dc80927c532ae93cc09e192 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Mon, 24 Sep 2018 21:46:21 +0000 Subject: libgo: update to Go 1.11 Reviewed-on: https://go-review.googlesource.com/136435 gotools/: * Makefile.am (mostlyclean-local): Run chmod on check-go-dir to make sure it is writable. (check-go-tools): Likewise. (check-vet): Copy internal/objabi to check-vet-dir. * Makefile.in: Rebuild. From-SVN: r264546 --- libgo/go/os/env.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'libgo/go/os/env.go') diff --git a/libgo/go/os/env.go b/libgo/go/os/env.go index 4e0171f..330297b 100644 --- a/libgo/go/os/env.go +++ b/libgo/go/os/env.go @@ -14,18 +14,33 @@ import ( // Expand replaces ${var} or $var in the string based on the mapping function. // For example, os.ExpandEnv(s) is equivalent to os.Expand(s, os.Getenv). func Expand(s string, mapping func(string) string) string { - buf := make([]byte, 0, 2*len(s)) + var buf []byte // ${} is all ASCII, so bytes are fine for this operation. i := 0 for j := 0; j < len(s); j++ { if s[j] == '$' && j+1 < len(s) { + if buf == nil { + buf = make([]byte, 0, 2*len(s)) + } buf = append(buf, s[i:j]...) name, w := getShellName(s[j+1:]) - buf = append(buf, mapping(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)...) + } j += w i = j + 1 } } + if buf == nil { + return s + } return string(buf) + s[i:] } @@ -63,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 } -- cgit v1.1