Check for failed allocations. Spotted by Martin Bealby.
Modified: trunk/reactos/lib/ntdll/csr/capture.c
Modified: trunk/reactos/lib/ntdll/csr/connect.c
Modified: trunk/reactos/lib/ntdll/ldr/startup.c
Modified: trunk/reactos/lib/ntdll/ldr/utils.c

Modified: trunk/reactos/lib/ntdll/csr/capture.c
--- trunk/reactos/lib/ntdll/csr/capture.c	2005-12-12 18:15:19 UTC (rev 20105)
+++ trunk/reactos/lib/ntdll/csr/capture.c	2005-12-12 19:23:52 UTC (rev 20106)
@@ -95,6 +95,7 @@
 
     /* Allocate memory from the port heap */
     CaptureBuffer = RtlAllocateHeap(CsrPortHeap, 0, BufferSize);
+    if (CaptureBuffer == NULL) return NULL;
 
     /* Initialize the header */
     CaptureBuffer->Size = BufferSize;

Modified: trunk/reactos/lib/ntdll/csr/connect.c
--- trunk/reactos/lib/ntdll/csr/connect.c	2005-12-12 18:15:19 UTC (rev 20105)
+++ trunk/reactos/lib/ntdll/csr/connect.c	2005-12-12 19:23:52 UTC (rev 20106)
@@ -203,6 +203,10 @@
 
     /* Allocate a buffer for it */
     PortName.Buffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, PortNameLength);
+    if (PortName.Buffer == NULL)
+    {
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
 
     /* Create the name */
     RtlAppendUnicodeToString(&PortName, ObjectDirectory );
@@ -255,6 +259,13 @@
                                           0,
                                           0,
                                           &SystemSid);
+    if (!NT_SUCCESS(Status))
+    {
+        /* Failure */
+        DPRINT1("Couldn't allocate SID\n");
+        NtClose(CsrSectionHandle);
+        return Status;
+    }
 
     /* Connect to the port */
     Status = NtSecureConnectPort(&CsrApiPort,
@@ -293,6 +304,12 @@
                                 PAGE_SIZE,
                                 0,
                                 0);
+    if (CsrPortHeap == NULL)
+    {
+        NtClose(CsrApiPort);
+        CsrApiPort = NULL;
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
 
     /* Return success */
     return STATUS_SUCCESS;
@@ -399,6 +416,10 @@
         /* Setup a buffer for the connection info */
         CaptureBuffer = CsrAllocateCaptureBuffer(1,
                                                  ClientConnect->ConnectionInfoSize);
+        if (CaptureBuffer == NULL)
+        {
+            return STATUS_INSUFFICIENT_RESOURCES;
+        }
 
         /* Allocate a pointer for the connection info*/
         CsrAllocateMessagePointer(CaptureBuffer,

Modified: trunk/reactos/lib/ntdll/ldr/startup.c
--- trunk/reactos/lib/ntdll/ldr/startup.c	2005-12-12 18:15:19 UTC (rev 20105)
+++ trunk/reactos/lib/ntdll/ldr/startup.c	2005-12-12 19:23:52 UTC (rev 20106)
@@ -266,7 +266,7 @@
        if (ImageBase <= (PVOID)0x1000)
          {
            DPRINT("ImageBase is null\n");
-           ZwTerminateProcess(NtCurrentProcess(), STATUS_UNSUCCESSFUL);
+           ZwTerminateProcess(NtCurrentProcess(), STATUS_INVALID_IMAGE_FORMAT);
          }
 
        /*  If MZ header exists  */
@@ -278,7 +278,7 @@
            *(PULONG)((PUCHAR)ImageBase + PEDosHeader->e_lfanew) != IMAGE_NT_SIGNATURE)
          {
            DPRINT1("Image has bad header\n");
-           ZwTerminateProcess(NtCurrentProcess(), STATUS_UNSUCCESSFUL);
+           ZwTerminateProcess(NtCurrentProcess(), STATUS_INVALID_IMAGE_FORMAT);
          }
 
        /* normalize process parameters */
@@ -321,7 +321,7 @@
        if (Peb->ProcessHeap == 0)
          {
            DPRINT1("Failed to create process heap\n");
-           ZwTerminateProcess(NtCurrentProcess(),STATUS_UNSUCCESSFUL);
+           ZwTerminateProcess(NtCurrentProcess(), STATUS_INSUFFICIENT_RESOURCES);
          }
 
        /* initialized vectored exception handling */
@@ -345,6 +345,11 @@
          RtlAllocateHeap(RtlGetProcessHeap(),
                          0,
                          sizeof(PVOID) * (USER32_CALLBACK_MAXIMUM + 1));
+       if (Peb->KernelCallbackTable == NULL)
+         {
+           DPRINT1("Failed to create callback table\n");
+           ZwTerminateProcess(NtCurrentProcess(),STATUS_INSUFFICIENT_RESOURCES);
+         }
 
        /* initalize loader lock */
        RtlInitializeCriticalSection (&LoaderLock);
@@ -357,7 +362,7 @@
        if (Peb->Ldr == NULL)
          {
            DPRINT1("Failed to create loader data\n");
-           ZwTerminateProcess(NtCurrentProcess(),STATUS_UNSUCCESSFUL);
+           ZwTerminateProcess(NtCurrentProcess(), STATUS_INSUFFICIENT_RESOURCES);
          }
        Peb->Ldr->Length = sizeof(PEB_LDR_DATA);
        Peb->Ldr->Initialized = FALSE;
@@ -383,7 +388,7 @@
        if (NtModule == NULL)
          {
            DPRINT1("Failed to create loader module entry (NTDLL)\n");
-           ZwTerminateProcess(NtCurrentProcess(),STATUS_UNSUCCESSFUL);
+           ZwTerminateProcess(NtCurrentProcess(), STATUS_INSUFFICIENT_RESOURCES);
 	 }
        memset(NtModule, 0, sizeof(LDR_DATA_TABLE_ENTRY));
 
@@ -422,7 +427,7 @@
        if (ExeModule == NULL)
          {
            DPRINT1("Failed to create loader module infomation\n");
-           ZwTerminateProcess(NtCurrentProcess(),STATUS_UNSUCCESSFUL);
+           ZwTerminateProcess(NtCurrentProcess(), STATUS_INSUFFICIENT_RESOURCES);
          }
        ExeModule->DllBase = Peb->ImageBaseAddress;
 
@@ -473,7 +478,7 @@
        if (EntryPoint == NULL)
          {
            DPRINT1("Failed to initialize image\n");
-           ZwTerminateProcess(NtCurrentProcess(),STATUS_UNSUCCESSFUL);
+           ZwTerminateProcess(NtCurrentProcess(), STATUS_INVALID_IMAGE_FORMAT);
          }
      }
    /* attach the thread */

Modified: trunk/reactos/lib/ntdll/ldr/utils.c
--- trunk/reactos/lib/ntdll/ldr/utils.c	2005-12-12 18:15:19 UTC (rev 20105)
+++ trunk/reactos/lib/ntdll/ldr/utils.c	2005-12-12 19:23:52 UTC (rev 20106)
@@ -2986,6 +2986,11 @@
   KeyInfo = RtlAllocateHeap (RtlGetProcessHeap(),
                              HEAP_ZERO_MEMORY,
                              KeyInfoSize);
+  if (KeyInfo == NULL)
+    {
+      NtClose (KeyHandle);
+      return STATUS_INSUFFICIENT_RESOURCES;
+    }
 
   RtlInitUnicodeString (&ValueNameString,
                         (PWSTR)ValueName);
@@ -3007,7 +3012,7 @@
       if (KeyInfo == NULL)
         {
           NtClose (KeyHandle);
-          return Status;
+          return STATUS_INSUFFICIENT_RESOURCES;
         }
 
       Status = NtQueryValueKey (KeyHandle,