From 492258549717d4e73a22170c507fb26a731c4aba Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Tue, 6 Oct 2020 16:31:00 -0700 Subject: syscall: rewrite Windows makeCmdLine to use []byte It's faster to append to a []byte and only convert to string at the end then it is to build up a string by concatenating characters. Fixes #41825 Change-Id: I45ddf77dcc62726c919f0533c95d483cee8ba366 Reviewed-on: https://go-review.googlesource.com/c/go/+/259978 Trust: Ian Lance Taylor Trust: Alex Brainman Run-TryBot: Ian Lance Taylor TryBot-Result: Go Bot Reviewed-by: Alex Brainman --- src/syscall/exec_windows.go | 73 ++++++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 30 deletions(-) (limited to 'src/syscall/exec_windows.go') diff --git a/src/syscall/exec_windows.go b/src/syscall/exec_windows.go index 8d6141c0ca..500321ef0d 100644 --- a/src/syscall/exec_windows.go +++ b/src/syscall/exec_windows.go @@ -24,74 +24,87 @@ 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 { + for i := 0; i < len(s); i++ { + switch s[i] { + case '"', '\\', ' ', '\t': + // Some escaping required. + b := make([]byte, 0, len(s)+2) + b = appendEscapeArg(b, s) + return string(b) + } + } + return s +} + +// appendEscapeArg escapes the string s, as per escapeArg, +// appends the result to b, and returns the updated slice. +func appendEscapeArg(b []byte, s string) []byte { if len(s) == 0 { - return "\"\"" + return append(b, `""`...) } - n := len(s) + + needsBackslash := false hasSpace := false for i := 0; i < len(s); i++ { switch s[i] { case '"', '\\': - n++ + needsBackslash = true case ' ', '\t': hasSpace = true } } - if hasSpace { - n += 2 + + if !needsBackslash && !hasSpace { + // No special handling required; normal case. + return append(b, s...) } - if n == len(s) { - return s + if !needsBackslash { + // hasSpace is true, so we need to quote the string. + b = append(b, '"') + b = append(b, s...) + return append(b, '"') } - qs := make([]byte, n) - j := 0 if hasSpace { - qs[j] = '"' - j++ + b = append(b, '"') } slashes := 0 for i := 0; i < len(s); i++ { - switch s[i] { + c := s[i] + switch c { default: slashes = 0 - qs[j] = s[i] case '\\': slashes++ - qs[j] = s[i] case '"': for ; slashes > 0; slashes-- { - qs[j] = '\\' - j++ + b = append(b, '\\') } - qs[j] = '\\' - j++ - qs[j] = s[i] + b = append(b, '\\') } - j++ + b = append(b, c) } if hasSpace { for ; slashes > 0; slashes-- { - qs[j] = '\\' - j++ + b = append(b, '\\') } - qs[j] = '"' - j++ + b = append(b, '"') } - return string(qs[:j]) + + return b } // makeCmdLine builds a command line out of args by escaping "special" // characters and joining the arguments with spaces. func makeCmdLine(args []string) string { - var s string + var b []byte for _, v := range args { - if s != "" { - s += " " + if len(b) > 0 { + b = append(b, ' ') } - s += EscapeArg(v) + b = appendEscapeArg(b, v) } - return s + return string(b) } // createEnvBlock converts an array of environment strings into -- cgit v1.3 From 9e0837f2e9967b883bebf4b8928108d9199ae677 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Wed, 7 Oct 2020 11:15:01 -0700 Subject: 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 Trust: Alex Brainman Trust: Emmanuel Odeke Run-TryBot: Ian Lance Taylor TryBot-Result: Go Bot Reviewed-by: Alex Brainman Reviewed-by: Emmanuel Odeke Reviewed-by: Brad Fitzpatrick --- src/syscall/exec_windows.go | 3 +++ src/syscall/exec_windows_test.go | 43 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/syscall/exec_windows_test.go (limited to 'src/syscall/exec_windows.go') diff --git a/src/syscall/exec_windows.go b/src/syscall/exec_windows.go index 500321ef0d..4a1d74ba3f 100644 --- a/src/syscall/exec_windows.go +++ b/src/syscall/exec_windows.go @@ -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 index 0000000000..eda1d36877 --- /dev/null +++ b/src/syscall/exec_windows_test.go @@ -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) + } + } +} -- cgit v1.3 From 1e3b535b6eb7f13eb6d903f7998c384a36e9bba8 Mon Sep 17 00:00:00 2001 From: Johan Knutzen Date: Wed, 21 Oct 2020 18:45:03 +0000 Subject: syscall: expose bInheritHandles of CreateProcess Certain use cases require this parameter to be false. This includes spawning a child process in a different windows session than session 0. Docs regarding the behavior of this parameter to CreateProcess: https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa Fixes #42098 Change-Id: If998f57d6f2962824aacbee75e1b508b255ab293 GitHub-Last-Rev: 584eb13e36a3ef7e0cd959295e92fb129f21d1f8 GitHub-Pull-Request: golang/go#41957 Reviewed-on: https://go-review.googlesource.com/c/go/+/261917 Run-TryBot: Ian Lance Taylor TryBot-Result: Go Bot Reviewed-by: Ian Lance Taylor Trust: Tobias Klauser Trust: Alex Brainman --- doc/go1.16.html | 8 ++++++++ src/syscall/exec_windows.go | 5 +++-- 2 files changed, 11 insertions(+), 2 deletions(-) (limited to 'src/syscall/exec_windows.go') diff --git a/doc/go1.16.html b/doc/go1.16.html index 5d29307886..5ae85e6c29 100644 --- a/doc/go1.16.html +++ b/doc/go1.16.html @@ -378,6 +378,14 @@ Do not send CLs removing the interior tags from such phrases. +
syscall
+
+

+ SysProcAttr on Windows has a new NoInheritHandles field that disables inheriting handles when creating a new process. +

+
+
+
strconv

diff --git a/src/syscall/exec_windows.go b/src/syscall/exec_windows.go index 4a1d74ba3f..46cbd7567d 100644 --- a/src/syscall/exec_windows.go +++ b/src/syscall/exec_windows.go @@ -241,6 +241,7 @@ type SysProcAttr struct { Token Token // if set, runs new process in the security context represented by the token ProcessAttributes *SecurityAttributes // if set, applies these security attributes as the descriptor for the new process ThreadAttributes *SecurityAttributes // if set, applies these security attributes as the descriptor for the main thread of the new process + NoInheritHandles bool // if set, each inheritable handle in the calling process is not inherited by the new process } var zeroProcAttr ProcAttr @@ -341,9 +342,9 @@ func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle flags := sys.CreationFlags | CREATE_UNICODE_ENVIRONMENT if sys.Token != 0 { - err = CreateProcessAsUser(sys.Token, argv0p, argvp, sys.ProcessAttributes, sys.ThreadAttributes, true, flags, createEnvBlock(attr.Env), dirp, si, pi) + err = CreateProcessAsUser(sys.Token, argv0p, argvp, sys.ProcessAttributes, sys.ThreadAttributes, !sys.NoInheritHandles, flags, createEnvBlock(attr.Env), dirp, si, pi) } else { - err = CreateProcess(argv0p, argvp, sys.ProcessAttributes, sys.ThreadAttributes, true, flags, createEnvBlock(attr.Env), dirp, si, pi) + err = CreateProcess(argv0p, argvp, sys.ProcessAttributes, sys.ThreadAttributes, !sys.NoInheritHandles, flags, createEnvBlock(attr.Env), dirp, si, pi) } if err != nil { return 0, 0, err -- cgit v1.3