--- trunk/reactos/lib/kernel32/process/create.c 2005-12-11 20:22:21 UTC (rev 20080)
+++ trunk/reactos/lib/kernel32/process/create.c 2005-12-11 21:12:22 UTC (rev 20081)
@@ -160,7 +160,11 @@
&Context,
&InitialTeb,
TRUE);
-
+ if (!NT_SUCCESS(Status))
+ {
+ return NULL;
+ }
+
/* Success */
return hThread;
}
@@ -603,7 +607,7 @@
NULL);
/* Cleanup */
- RtlFreeHeap(GetProcessHeap(), 0, DllPath.Buffer);
+ RtlFreeHeap(RtlGetProcessHeap(), 0, DllPath.Buffer);
RtlDestroyProcessParameters(ProcessParameters);
DPRINT("Completed\n");
@@ -635,10 +639,10 @@
BOOLEAN FoundQuotes = FALSE;
BOOLEAN QuotesNeeded = FALSE;
BOOLEAN CmdLineIsAppName = FALSE;
- UNICODE_STRING ApplicationName;
+ UNICODE_STRING ApplicationName = {0};
OBJECT_ATTRIBUTES LocalObjectAttributes;
POBJECT_ATTRIBUTES ObjectAttributes;
- HANDLE hSection, hProcess, hThread;
+ HANDLE hSection = NULL, hProcess = NULL, hThread = NULL;
SECTION_IMAGE_INFORMATION SectionImageInfo;
LPWSTR CurrentDirectory = NULL;
LPWSTR CurrentDirectoryPart;
@@ -662,6 +666,7 @@
PPEB OurPeb = NtCurrentPeb();
PPEB RemotePeb;
SIZE_T EnvSize = 0;
+ BOOL Ret = FALSE;
DPRINT("CreateProcessW: lpApplicationName: %S lpCommandLine: %S"
" lpEnvironment: %p lpCurrentDirectory: %S dwCreationFlags: %lx\n",
@@ -767,9 +772,14 @@
if (!lpApplicationName)
{
/* The fun begins */
- NameBuffer = RtlAllocateHeap(GetProcessHeap(),
+ NameBuffer = RtlAllocateHeap(RtlGetProcessHeap(),
0,
MAX_PATH * sizeof(WCHAR));
+ if (NameBuffer == NULL)
+ {
+ SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+ goto Cleanup;
+ }
/* This is all we have to work with :( */
lpApplicationName = lpCommandLine;
@@ -929,7 +939,7 @@
}
/* We totally failed */
- return FALSE;
+ goto Cleanup;
}
/* Put back the command line */
@@ -963,8 +973,8 @@
if ((BasepCheckDosApp(&ApplicationName)))
{
DPRINT1("Launching VDM...\n");
- RtlFreeHeap(GetProcessHeap(), 0, NameBuffer);
- RtlFreeHeap(GetProcessHeap(), 0, ApplicationName.Buffer);
+ RtlFreeHeap(RtlGetProcessHeap(), 0, NameBuffer);
+ RtlFreeHeap(RtlGetProcessHeap(), 0, ApplicationName.Buffer);
return CreateProcessW(L"ntvdm.exe",
(LPWSTR)lpApplicationName,
lpProcessAttributes,
@@ -996,9 +1006,14 @@
CmdLineLength *= sizeof(WCHAR);
/* Allocate space for the new command line */
- BatchCommandLine = RtlAllocateHeap(GetProcessHeap(),
+ BatchCommandLine = RtlAllocateHeap(RtlGetProcessHeap(),
0,
CmdLineLength);
+ if (BatchCommandLine == NULL)
+ {
+ SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+ goto Cleanup;
+ }
/* Build it */
wcscpy(BatchCommandLine, CMD_STRING);
@@ -1020,7 +1035,7 @@
lpApplicationName = NULL;
/* Free memory */
- RtlFreeHeap(GetProcessHeap(), 0, ApplicationName.Buffer);
+ RtlFreeHeap(RtlGetProcessHeap(), 0, ApplicationName.Buffer);
ApplicationName.Buffer = NULL;
goto GetAppName;
break;
@@ -1029,8 +1044,8 @@
/* It's a Win16 Image, use VDM */
DPRINT1("Launching VDM...\n");
- RtlFreeHeap(GetProcessHeap(), 0, NameBuffer);
- RtlFreeHeap(GetProcessHeap(), 0, ApplicationName.Buffer);
+ RtlFreeHeap(RtlGetProcessHeap(), 0, NameBuffer);
+ RtlFreeHeap(RtlGetProcessHeap(), 0, ApplicationName.Buffer);
return CreateProcessW(L"ntvdm.exe",
(LPWSTR)lpApplicationName,
lpProcessAttributes,
@@ -1045,7 +1060,7 @@
default:
/* Invalid Image Type */
SetLastError(ERROR_BAD_EXE_FORMAT);
- return FALSE;
+ goto Cleanup;
}
}
@@ -1067,19 +1082,17 @@
NULL);
if(!NT_SUCCESS(Status))
{
- NtClose(hSection);
DPRINT1("Unable to get SectionImageInformation, status 0x%x\n", Status);
SetLastErrorByStatus(Status);
- return FALSE;
+ goto Cleanup;
}
/* Don't execute DLLs */
if (SectionImageInfo.ImageCharacteristics & IMAGE_FILE_DLL)
{
- NtClose(hSection);
DPRINT1("Can't execute a DLL\n");
SetLastError(ERROR_BAD_EXE_FORMAT);
- return FALSE;
+ goto Cleanup;
}
/* FIXME: Check for Debugger */
@@ -1090,10 +1103,9 @@
if (IMAGE_SUBSYSTEM_WINDOWS_GUI != SectionImageInfo.SubsystemType &&
IMAGE_SUBSYSTEM_WINDOWS_CUI != SectionImageInfo.SubsystemType)
{
- NtClose(hSection);
DPRINT1("Invalid subsystem %d\n", SectionImageInfo.SubsystemType);
SetLastError(ERROR_BAD_EXE_FORMAT);
- return FALSE;
+ goto Cleanup;
}
/* Initialize the process object attributes */
@@ -1112,10 +1124,9 @@
NULL);
if(!NT_SUCCESS(Status))
{
- NtClose(hSection);
DPRINT1("Unable to create process, status 0x%x\n", Status);
SetLastErrorByStatus(Status);
- return FALSE;
+ goto Cleanup;
}
/* Set new class */
@@ -1125,11 +1136,9 @@
sizeof(PROCESS_PRIORITY_CLASS));
if(!NT_SUCCESS(Status))
{
- NtClose(hProcess);
- NtClose(hSection);
DPRINT1("Unable to set new process priority, status 0x%x\n", Status);
SetLastErrorByStatus(Status);
- return FALSE;
+ goto Cleanup;
}
/* Set Error Mode */
@@ -1146,9 +1155,15 @@
if (lpCurrentDirectory)
{
/* Allocate a buffer */
- CurrentDirectory = RtlAllocateHeap(GetProcessHeap(),
+ CurrentDirectory = RtlAllocateHeap(RtlGetProcessHeap(),
0,
- MAX_PATH * sizeof(WCHAR) + 2);
+ (MAX_PATH + 1) * sizeof(WCHAR));
+ if (CurrentDirectory == NULL)
+ {
+ DPRINT1("Cannot allocate memory for directory name\n");
+ SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+ goto Cleanup;
+ }
/* Get the length */
if (GetFullPathNameW(lpCurrentDirectory,
@@ -1158,7 +1173,7 @@
{
DPRINT1("Directory name too long\n");
SetLastError(ERROR_DIRECTORY);
- return FALSE;
+ goto Cleanup;
}
}
@@ -1166,10 +1181,16 @@
if (QuotesNeeded || CmdLineIsAppName)
{
/* Allocate a buffer */
- QuotedCmdLine = RtlAllocateHeap(GetProcessHeap(),
+ QuotedCmdLine = RtlAllocateHeap(RtlGetProcessHeap(),
0,
(wcslen(lpCommandLine) + 2 + 1) *
sizeof(WCHAR));
+ if (QuotedCmdLine == NULL)
+ {
+ DPRINT1("Cannot allocate memory for quoted command line\n");
+ SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+ goto Cleanup;
+ }
/* Copy the first quote */
wcscpy(QuotedCmdLine, L"\"");
@@ -1199,9 +1220,14 @@
{
if (QuotedCmdLine == NULL)
{
- QuotedCmdLine = RtlAllocateHeap(GetProcessHeap(),
+ QuotedCmdLine = RtlAllocateHeap(RtlGetProcessHeap(),
0,
(wcslen(lpCommandLine) + 1) * sizeof(WCHAR));
+ if (QuotedCmdLine == NULL)
+ {
+ SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+ goto Cleanup;
+ }
wcscpy(QuotedCmdLine, lpCommandLine);
}
@@ -1227,7 +1253,7 @@
if(lpEnvironment && !(dwCreationFlags & CREATE_UNICODE_ENVIRONMENT))
{
lpEnvironment = BasepConvertUnicodeEnvironment(&EnvSize, lpEnvironment);
- if (!lpEnvironment) return FALSE;
+ if (!lpEnvironment) goto Cleanup;
}
/* Create Process Environment */
@@ -1254,7 +1280,7 @@
{
DPRINT1("Could not initialize Process Environment\n");
SetLastErrorByStatus(Status);
- return FALSE;
+ goto Cleanup;
}
/* Close the section */
@@ -1276,7 +1302,7 @@
if (!NT_SUCCESS(Status))
{
DPRINT1("Failed to read memory\n");
- return FALSE;
+ goto Cleanup;
}
/* Duplicate and write the handles */
@@ -1292,7 +1318,7 @@
}
/* Create the first thread */
- DPRINT("Creating thread for process (EntryPoint = 0x%.08x)\n",
+ DPRINT("Creating thread for process (EntryPoint = 0x%p)\n",
SectionImageInfo.TransferAddress);
hThread = BasepCreateFirstThread(hProcess,
lpThreadAttributes,
@@ -1302,7 +1328,8 @@
if (hThread == NULL)
{
DPRINT1("Could not create Initial Thread\n");
- return FALSE;
+ /* FIXME - set last error code */
+ goto Cleanup;
}
@@ -1315,7 +1342,7 @@
{
DPRINT1("CSR Notification Failed");
SetLastErrorByStatus(Status);
- return FALSE;
+ goto Cleanup;
}
if (!(dwCreationFlags & CREATE_SUSPENDED))
@@ -1328,16 +1355,18 @@
lpProcessInformation->dwThreadId = (DWORD)ClientId.UniqueThread;
lpProcessInformation->hProcess = hProcess;
lpProcessInformation->hThread = hThread;
- DPRINT("hThread[%lx]: %lx inside hProcess[%lx]: %lx\n", hThread,
+ DPRINT("hThread[%p]: %p inside hProcess[%p]: %p\n", hThread,
ClientId.UniqueThread, ClientId.UniqueProcess, hProcess);
hProcess = hThread = NULL;
-
+ Ret = TRUE;
+
+Cleanup:
/* De-allocate heap strings */
- if (NameBuffer) RtlFreeHeap(GetProcessHeap(), 0, NameBuffer);
+ if (NameBuffer) RtlFreeHeap(RtlGetProcessHeap(), 0, NameBuffer);
if (ApplicationName.Buffer)
- RtlFreeHeap(GetProcessHeap(), 0, ApplicationName.Buffer);
- if (CurrentDirectory) RtlFreeHeap(GetProcessHeap(), 0, CurrentDirectory);
- if (QuotedCmdLine) RtlFreeHeap(GetProcessHeap(), 0, QuotedCmdLine);
+ RtlFreeHeap(RtlGetProcessHeap(), 0, ApplicationName.Buffer);
+ if (CurrentDirectory) RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentDirectory);
+ if (QuotedCmdLine) RtlFreeHeap(RtlGetProcessHeap(), 0, QuotedCmdLine);
/* Kill any handles still alive */
if (hSection) NtClose(hSection);
@@ -1350,7 +1379,7 @@
if (hProcess) NtClose(hProcess);
/* Return Success */
- return TRUE;
+ return Ret;
}
/*
@@ -1498,9 +1527,9 @@
RtlFreeUnicodeString(&ApplicationName);
RtlFreeUnicodeString(&LiveCommandLine);
RtlFreeUnicodeString(&CurrentDirectory);
- RtlFreeHeap(GetProcessHeap(), 0, StartupInfo.lpDesktop);
- RtlFreeHeap(GetProcessHeap(), 0, StartupInfo.lpReserved);
- RtlFreeHeap(GetProcessHeap(), 0, StartupInfo.lpTitle);
+ RtlFreeHeap(RtlGetProcessHeap(), 0, StartupInfo.lpDesktop);
+ RtlFreeHeap(RtlGetProcessHeap(), 0, StartupInfo.lpReserved);
+ RtlFreeHeap(RtlGetProcessHeap(), 0, StartupInfo.lpTitle);
/* Return what Unicode did */
return bRetVal;