Skip to content

Commit 6c52899

Browse files
committed
windows: return error if DecomposeCommandLine parameter contains NUL
DecomposeCommandLine is documented to use CommandLineToArgv, and the CommandLineToArgvW system call inherently does not support strings with internal NUL bytes. This CL changes DecomposeCommandLine to reject those strings with an error instead of panicking. Fixes golang/go#58817 Change-Id: I22a026bf2e69344a21f04849c50ba19b6e7b2007 Reviewed-on: https://go-review.googlesource.com/c/sys/+/487695 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Run-TryBot: Alex Brainman <[email protected]>
1 parent 9524d49 commit 6c52899

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

windows/exec_windows.go

+6-1
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/syscall_windows_test.go

+16
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)