Skip to content

Commit 9e0837f

Browse files
syscall: restore EscapeArg behavior for empty string
Accidentally broken by CL 259978. For #41825 Change-Id: Id663514e6eefa325faccdb66493d0bb2b3281046 Reviewed-on: https://go-review.googlesource.com/c/go/+/260397 Trust: Ian Lance Taylor <[email protected]> Trust: Alex Brainman <[email protected]> Trust: Emmanuel Odeke <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Alex Brainman <[email protected]> Reviewed-by: Emmanuel Odeke <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent a4b95cd commit 9e0837f

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/syscall/exec_windows.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ var ForkLock sync.RWMutex
2424
// - finally, s is wrapped with double quotes (arg -> "arg"),
2525
// but only if there is space or tab inside s.
2626
func EscapeArg(s string) string {
27+
if len(s) == 0 {
28+
return `""`
29+
}
2730
for i := 0; i < len(s); i++ {
2831
switch s[i] {
2932
case '"', '\\', ' ', '\t':

src/syscall/exec_windows_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2020 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package syscall_test
6+
7+
import (
8+
"syscall"
9+
"testing"
10+
)
11+
12+
func TestEscapeArg(t *testing.T) {
13+
var tests = []struct {
14+
input, output string
15+
}{
16+
{``, `""`},
17+
{`a`, `a`},
18+
{` `, `" "`},
19+
{`\`, `\`},
20+
{`"`, `\"`},
21+
{`\"`, `\\\"`},
22+
{`\\"`, `\\\\\"`},
23+
{`\\ `, `"\\ "`},
24+
{` \\`, `" \\\\"`},
25+
{`a `, `"a "`},
26+
{`C:\`, `C:\`},
27+
{`C:\Program Files (x32)\Common\`, `"C:\Program Files (x32)\Common\\"`},
28+
{`C:\Users\Игорь\`, `C:\Users\Игорь\`},
29+
{`Андрей\file`, `Андрей\file`},
30+
{`C:\Windows\temp`, `C:\Windows\temp`},
31+
{`c:\temp\newfile`, `c:\temp\newfile`},
32+
{`\\?\C:\Windows`, `\\?\C:\Windows`},
33+
{`\\?\`, `\\?\`},
34+
{`\\.\C:\Windows\`, `\\.\C:\Windows\`},
35+
{`\\server\share\file`, `\\server\share\file`},
36+
{`\\newserver\tempshare\really.txt`, `\\newserver\tempshare\really.txt`},
37+
}
38+
for _, test := range tests {
39+
if got := syscall.EscapeArg(test.input); got != test.output {
40+
t.Errorf("EscapeArg(%#q) = %#q, want %#q", test.input, got, test.output)
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)