[NTOS:PNP]
authorThomas Faber <thomas.faber@reactos.org>
Sat, 4 Mar 2017 15:17:30 +0000 (15:17 +0000)
committerThomas Faber <thomas.faber@reactos.org>
Sat, 4 Mar 2017 15:17:30 +0000 (15:17 +0000)
- Use UNICODE_STRINGs in IopCreateDeviceInstancePath instead of messing around with unsafe string functions and stack buffers

svn path=/trunk/; revision=74052

reactos/ntoskrnl/io/pnpmgr/pnpmgr.c

index 07e8075..88d1bae 100644 (file)
@@ -1879,9 +1879,8 @@ IopCreateDeviceInstancePath(
     _Out_ PUNICODE_STRING InstancePath)
 {
     IO_STATUS_BLOCK IoStatusBlock;
-    PWSTR DeviceId;
-    PWSTR InstanceId;
-    WCHAR InstancePathBuffer[MAX_PATH];
+    UNICODE_STRING DeviceId;
+    UNICODE_STRING InstanceId;
     IO_STACK_LOCATION Stack;
     NTSTATUS Status;
     UNICODE_STRING ParentIdPrefix = { 0, 0, NULL };
@@ -1900,23 +1899,21 @@ IopCreateDeviceInstancePath(
         return Status;
     }
 
-    /* Copy the device id string */
-    DeviceId = (PWSTR)IoStatusBlock.Information;
-    wcscpy(InstancePathBuffer, DeviceId);
+    /* Save the device id string */
+    RtlInitUnicodeString(&DeviceId, (PWSTR)IoStatusBlock.Information);
 
     /*
      * FIXME: Check for valid characters, if there is invalid characters
      * then bugcheck.
      */
 
-    ExFreePoolWithTag(DeviceId, 0);
-
     DPRINT("Sending IRP_MN_QUERY_CAPABILITIES to device stack (after enumeration)\n");
 
     Status = IopQueryDeviceCapabilities(DeviceNode, &DeviceCapabilities);
     if (!NT_SUCCESS(Status))
     {
         DPRINT1("IopQueryDeviceCapabilities() failed (Status 0x%08lx)\n", Status);
+        RtlFreeUnicodeString(&DeviceId);
         return Status;
     }
 
@@ -1925,6 +1922,7 @@ IopCreateDeviceInstancePath(
     {
         /* FIXME: Cleanup device */
         DeviceNode->Flags |= DNF_DISABLED;
+        RtlFreeUnicodeString(&DeviceId);
         return STATUS_PLUGPLAY_NO_DEVICE;
     }
     else
@@ -1940,6 +1938,7 @@ IopCreateDeviceInstancePath(
         if (!NT_SUCCESS(Status))
         {
             DPRINT1("IopGetParentIdPrefix() failed (Status 0x%08lx)\n", Status);
+            RtlFreeUnicodeString(&DeviceId);
             return Status;
         }
     }
@@ -1953,46 +1952,59 @@ IopCreateDeviceInstancePath(
                                &Stack);
     if (NT_SUCCESS(Status))
     {
-        InstanceId = (PWSTR)IoStatusBlock.Information;
+        RtlInitUnicodeString(&InstanceId,
+                             (PWSTR)IoStatusBlock.Information);
+
+        InstancePath->Length = 0;
+        InstancePath->MaximumLength = DeviceId.Length + sizeof(WCHAR) +
+                                      ParentIdPrefix.Length +
+                                      InstanceId.Length +
+                                      sizeof(UNICODE_NULL);
+        if (ParentIdPrefix.Length && InstanceId.Length)
+        {
+            InstancePath->MaximumLength += sizeof(WCHAR);
+        }
 
-        /* Append the instance id string */
-        wcscat(InstancePathBuffer, L"\\");
-        if (ParentIdPrefix.Length > 0)
+        InstancePath->Buffer = ExAllocatePoolWithTag(PagedPool,
+                                                     InstancePath->MaximumLength,
+                                                     TAG_IO);
+        if (!InstancePath->Buffer)
         {
-            /* Add information from parent bus device to InstancePath */
-            wcscat(InstancePathBuffer, ParentIdPrefix.Buffer);
-            if (InstanceId && *InstanceId)
-            {
-                wcscat(InstancePathBuffer, L"&");
-            }
+            RtlFreeUnicodeString(&InstanceId);
+            RtlFreeUnicodeString(&ParentIdPrefix);
+            RtlFreeUnicodeString(&DeviceId);
+            return STATUS_INSUFFICIENT_RESOURCES;
         }
-        if (InstanceId)
+
+        /* Start with the device id */
+        RtlCopyUnicodeString(InstancePath, &DeviceId);
+        RtlAppendUnicodeToString(InstancePath, L"\\");
+
+        /* Add information from parent bus device to InstancePath */
+        RtlAppendUnicodeStringToString(InstancePath, &ParentIdPrefix);
+        if (ParentIdPrefix.Length && InstanceId.Length)
         {
-            wcscat(InstancePathBuffer, InstanceId);
+            RtlAppendUnicodeToString(InstancePath, L"&");
         }
 
+        /* Finally, add the id returned by the driver stack */
+        RtlAppendUnicodeStringToString(InstancePath, &InstanceId);
+
         /*
          * FIXME: Check for valid characters, if there is invalid characters
          * then bugcheck
          */
 
-        if (InstanceId)
-        {
-            ExFreePoolWithTag(InstanceId, 0);
-        }
+        RtlFreeUnicodeString(&InstanceId);
+        RtlFreeUnicodeString(&DeviceId);
     }
     else
     {
         DPRINT("IopInitiatePnpIrp(BusQueryInstanceID) failed (Status %x)\n", Status);
+        *InstancePath = DeviceId;
     }
     RtlFreeUnicodeString(&ParentIdPrefix);
 
-    if (!RtlCreateUnicodeString(InstancePath, InstancePathBuffer))
-    {
-        DPRINT1("RtlCreateUnicodeString failed\n");
-        return STATUS_INSUFFICIENT_RESOURCES;
-    }
-
     return STATUS_SUCCESS;
 }