Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to pass null as lpCommandLine parameter to CreateProcess() #874

Closed
skst opened this issue Mar 4, 2023 · 1 comment · Fixed by #877
Closed

Unable to pass null as lpCommandLine parameter to CreateProcess() #874

skst opened this issue Mar 4, 2023 · 1 comment · Fixed by #877
Labels
bug Something isn't working

Comments

@skst
Copy link

skst commented Mar 4, 2023

Actual behavior

Generated code:

internal static unsafe winmdroot.Foundation.BOOL CreateProcess(string lpApplicationName, ref Span<char>lpCommandLine, winmdroot.Security.SECURITY_ATTRIBUTES? lpProcessAttributes, winmdroot.Security.SECURITY_ATTRIBUTES? lpThreadAttributes, winmdroot.Foundation.BOOL bInheritHandles, winmdroot.System.Threading.PROCESS_CREATION_FLAGS dwCreationFlags, void* lpEnvironment, string lpCurrentDirectory, in winmdroot.System.Threading.STARTUPINFOW lpStartupInfo, out winmdroot.System.Threading.PROCESS_INFORMATION lpProcessInformation)
		{
			if (lpCommandLine.LastIndexOf('\0') == -1)throw new ArgumentException("Required null terminator missing.", "lpCommandLine");

Expected behavior

CreateProcess

"The lpCommandLine parameter can be NULL. In that case, the function uses the string pointed to by lpApplicationName as the command line."

Repro steps

  1. NativeMethods.txt content:
CreateProcess
PROCESS_CREATION_FLAGS
  1. Any of your own code that should be shared?
	Span<char> spanCmdLine = new(null);
	bool ok = PInvoke.CreateProcess(path, ref spanCmdLine, lpProcessAttributes: null, lpThreadAttributes: null, bInheritHandles: false,
												Windows.Win32.System.Threading.PROCESS_CREATION_FLAGS.CREATE_SUSPENDED,
												lpEnvironment: null, lpCurrentDirectory: wd, startupInfo, out process);
System.ArgumentException
  HResult=0x80070057
  Message=Required null terminator missing. (Parameter 'lpCommandLine')

Context

  • CsWin32 version: 0.2.188-beta
  • Win32Metadata version (if explicitly set by project):
  • Target Framework: .NET 7
  • LangVersion (if explicitly set by project): latest (C# 11)
@skst skst added the bug Something isn't working label Mar 4, 2023
@AArnott
Copy link
Member

AArnott commented Mar 6, 2023

It seems our null termination check should include a null span check first. Something like this:

-if (lpCommandLine.LastIndexOf('\0') == -1)
+if (lpCommandLine != null && lpCommandLine.LastIndexOf('\0') == -1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants