]> Cypherpunks repositories - gostls13.git/commitdiff
syscall: restore EscapeArg behavior for empty string
authorIan Lance Taylor <iant@golang.org>
Wed, 7 Oct 2020 18:15:01 +0000 (11:15 -0700)
committerIan Lance Taylor <iant@golang.org>
Thu, 8 Oct 2020 20:46:25 +0000 (20:46 +0000)
Accidentally broken by CL 259978.

For #41825

Change-Id: Id663514e6eefa325faccdb66493d0bb2b3281046
Reviewed-on: https://go-review.googlesource.com/c/go/+/260397
Trust: Ian Lance Taylor <iant@golang.org>
Trust: Alex Brainman <alex.brainman@gmail.com>
Trust: Emmanuel Odeke <emm.odeke@gmail.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/syscall/exec_windows.go
src/syscall/exec_windows_test.go [new file with mode: 0644]

index 500321ef0d5a886a2124cbe452d52dc8246bde3a..4a1d74ba3f3721e89e81722a6c8aa49943ea338a 100644 (file)
@@ -24,6 +24,9 @@ var ForkLock sync.RWMutex
 // - finally, s is wrapped with double quotes (arg -> "arg"),
 //   but only if there is space or tab inside s.
 func EscapeArg(s string) string {
+       if len(s) == 0 {
+               return `""`
+       }
        for i := 0; i < len(s); i++ {
                switch s[i] {
                case '"', '\\', ' ', '\t':
diff --git a/src/syscall/exec_windows_test.go b/src/syscall/exec_windows_test.go
new file mode 100644 (file)
index 0000000..eda1d36
--- /dev/null
@@ -0,0 +1,43 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package syscall_test
+
+import (
+       "syscall"
+       "testing"
+)
+
+func TestEscapeArg(t *testing.T) {
+       var tests = []struct {
+               input, output string
+       }{
+               {``, `""`},
+               {`a`, `a`},
+               {` `, `" "`},
+               {`\`, `\`},
+               {`"`, `\"`},
+               {`\"`, `\\\"`},
+               {`\\"`, `\\\\\"`},
+               {`\\ `, `"\\ "`},
+               {` \\`, `" \\\\"`},
+               {`a `, `"a "`},
+               {`C:\`, `C:\`},
+               {`C:\Program Files (x32)\Common\`, `"C:\Program Files (x32)\Common\\"`},
+               {`C:\Users\Игорь\`, `C:\Users\Игорь\`},
+               {`Андрей\file`, `Андрей\file`},
+               {`C:\Windows\temp`, `C:\Windows\temp`},
+               {`c:\temp\newfile`, `c:\temp\newfile`},
+               {`\\?\C:\Windows`, `\\?\C:\Windows`},
+               {`\\?\`, `\\?\`},
+               {`\\.\C:\Windows\`, `\\.\C:\Windows\`},
+               {`\\server\share\file`, `\\server\share\file`},
+               {`\\newserver\tempshare\really.txt`, `\\newserver\tempshare\really.txt`},
+       }
+       for _, test := range tests {
+               if got := syscall.EscapeArg(test.input); got != test.output {
+                       t.Errorf("EscapeArg(%#q) = %#q, want %#q", test.input, got, test.output)
+               }
+       }
+}