Skip to content

Commit 3626b01

Browse files
committed
Merge remote-tracking branch 'upstream/master' into 59016-FailureActionsFlag
2 parents 8df3816 + ca59eda commit 3626b01

File tree

6 files changed

+54
-8
lines changed

6 files changed

+54
-8
lines changed

execabs/execabs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
// Copied from internal/testenv.HasExec
2222
func hasExec() bool {
2323
switch runtime.GOOS {
24-
case "js", "ios":
24+
case "wasip1", "js", "ios":
2525
return false
2626
}
2727
return true

windows/env_windows.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ func (token Token) Environ(inheritExisting bool) (env []string, err error) {
3737
return nil, err
3838
}
3939
defer DestroyEnvironmentBlock(block)
40-
blockp := uintptr(unsafe.Pointer(block))
40+
blockp := unsafe.Pointer(block)
4141
for {
42-
entry := UTF16PtrToString((*uint16)(unsafe.Pointer(blockp)))
42+
entry := UTF16PtrToString((*uint16)(blockp))
4343
if len(entry) == 0 {
4444
break
4545
}
4646
env = append(env, entry)
47-
blockp += 2 * (uintptr(len(entry)) + 1)
47+
blockp = unsafe.Add(blockp, 2*(len(entry)+1))
4848
}
4949
return env, nil
5050
}

windows/exec_windows.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,17 @@ func ComposeCommandLine(args []string) string {
9595
// DecomposeCommandLine breaks apart its argument command line into unescaped parts using CommandLineToArgv,
9696
// as gathered from GetCommandLine, QUERY_SERVICE_CONFIG's BinaryPathName argument, or elsewhere that
9797
// command lines are passed around.
98+
// DecomposeCommandLine returns error if commandLine contains NUL.
9899
func DecomposeCommandLine(commandLine string) ([]string, error) {
99100
if len(commandLine) == 0 {
100101
return []string{}, nil
101102
}
103+
utf16CommandLine, err := UTF16FromString(commandLine)
104+
if err != nil {
105+
return nil, errorspkg.New("string with NUL passed to DecomposeCommandLine")
106+
}
102107
var argc int32
103-
argv, err := CommandLineToArgv(StringToUTF16Ptr(commandLine), &argc)
108+
argv, err := CommandLineToArgv(&utf16CommandLine[0], &argc)
104109
if err != nil {
105110
return nil, err
106111
}

windows/svc/mgr/mgr_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"testing"
1818
"time"
1919

20+
"golang.org/x/sys/windows"
2021
"golang.org/x/sys/windows/svc"
2122
"golang.org/x/sys/windows/svc/mgr"
2223
)
@@ -259,6 +260,16 @@ func testMultipleRecoverySettings(t *testing.T, s *mgr.Service, rebootMsgShould,
259260
}
260261
}
261262

263+
func testControl(t *testing.T, s *mgr.Service, c svc.Cmd, expectedErr error, expectedStatus svc.Status) {
264+
status, err := s.Control(c)
265+
if err != expectedErr {
266+
t.Fatalf("Unexpected return from s.Control: %v (expected %v)", err, expectedErr)
267+
}
268+
if expectedStatus != status {
269+
t.Fatalf("Unexpected status from s.Control: %+v (expected %+v)", status, expectedStatus)
270+
}
271+
}
272+
262273
func remove(t *testing.T, s *mgr.Service) {
263274
err := s.Delete()
264275
if err != nil {
@@ -302,6 +313,7 @@ func TestMyService(t *testing.T) {
302313
t.Fatalf("service %s is not installed", name)
303314
}
304315
defer s.Close()
316+
defer s.Delete()
305317

306318
c.BinaryPathName = exepath
307319
c = testConfig(t, s, c)
@@ -347,6 +359,11 @@ func TestMyService(t *testing.T) {
347359
testRecoveryActionsOnNonCrashFailures(t, s, false)
348360
testMultipleRecoverySettings(t, s, fmt.Sprintf("%s failed", name), fmt.Sprintf("sc query %s", name), true)
349361

362+
expectedStatus := svc.Status{
363+
State: svc.Stopped,
364+
}
365+
testControl(t, s, svc.Stop, windows.ERROR_SERVICE_NOT_ACTIVE, expectedStatus)
366+
350367
remove(t, s)
351368
}
352369

windows/svc/mgr/service.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,25 @@ func (s *Service) Start(args ...string) error {
4545
return windows.StartService(s.Handle, uint32(len(args)), p)
4646
}
4747

48-
// Control sends state change request c to the service s.
48+
// Control sends state change request c to the service s. It returns the most
49+
// recent status the service reported to the service control manager, and an
50+
// error if the state change request was not accepted.
51+
// Note that the returned service status is only set if the status change
52+
// request succeeded, or if it failed with error ERROR_INVALID_SERVICE_CONTROL,
53+
// ERROR_SERVICE_CANNOT_ACCEPT_CTRL, or ERROR_SERVICE_NOT_ACTIVE.
4954
func (s *Service) Control(c svc.Cmd) (svc.Status, error) {
5055
var t windows.SERVICE_STATUS
5156
err := windows.ControlService(s.Handle, uint32(c), &t)
52-
if err != nil {
57+
if err != nil &&
58+
err != windows.ERROR_INVALID_SERVICE_CONTROL &&
59+
err != windows.ERROR_SERVICE_CANNOT_ACCEPT_CTRL &&
60+
err != windows.ERROR_SERVICE_NOT_ACTIVE {
5361
return svc.Status{}, err
5462
}
5563
return svc.Status{
5664
State: svc.State(t.CurrentState),
5765
Accepts: svc.Accepted(t.ControlsAccepted),
58-
}, nil
66+
}, err
5967
}
6068

6169
// Query returns current status of service s.

windows/syscall_windows_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,22 @@ func TestCommandLineRecomposition(t *testing.T) {
626626
continue
627627
}
628628
}
629+
630+
// check that windows.DecomposeCommandLine returns error for strings with NUL
631+
testsWithNUL := []string{
632+
"\x00abcd",
633+
"ab\x00cd",
634+
"abcd\x00",
635+
"\x00abcd\x00",
636+
"\x00ab\x00cd\x00",
637+
"\x00\x00\x00",
638+
}
639+
for _, test := range testsWithNUL {
640+
_, err := windows.DecomposeCommandLine(test)
641+
if err == nil {
642+
t.Errorf("Failed to return error while decomposing %#q string with NUL inside", test)
643+
}
644+
}
629645
}
630646

631647
func TestWinVerifyTrust(t *testing.T) {

0 commit comments

Comments
 (0)