https://git.reactos.org/?p=reactos.git;a=commitdiff;h=c0e7eaf4036fd23cf4fb79d15d82150e3fd0e689
commit c0e7eaf4036fd23cf4fb79d15d82150e3fd0e689 Author: Thomas Faber <thomas.fa...@reactos.org> AuthorDate: Sat Jan 14 17:40:14 2023 -0500 Commit: Thomas Faber <thomas.fa...@reactos.org> CommitDate: Sun Jan 22 09:42:08 2023 -0500 [NTOS:PNP] Avoid recursion when walking the device tree. --- ntoskrnl/io/pnpmgr/plugplay.c | 55 +++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/ntoskrnl/io/pnpmgr/plugplay.c b/ntoskrnl/io/pnpmgr/plugplay.c index 192612c66a2..46585458b84 100644 --- a/ntoskrnl/io/pnpmgr/plugplay.c +++ b/ntoskrnl/io/pnpmgr/plugplay.c @@ -18,6 +18,12 @@ typedef struct _PNP_EVENT_ENTRY PLUGPLAY_EVENT_BLOCK Event; } PNP_EVENT_ENTRY, *PPNP_EVENT_ENTRY; +typedef struct _IOP_FIND_DEVICE_INSTANCE_TRAVERSE_CONTEXT +{ + PCUNICODE_STRING InstancePath; + PDEVICE_OBJECT DeviceObject; +} IOP_FIND_DEVICE_INSTANCE_TRAVERSE_CONTEXT, *PIOP_FIND_DEVICE_INSTANCE_TRAVERSE_CONTEXT; + /* GLOBALS *******************************************************************/ @@ -88,39 +94,32 @@ IopQueueTargetDeviceEvent(const GUID *Guid, return STATUS_SUCCESS; } - -static PDEVICE_OBJECT -IopTraverseDeviceNode(PDEVICE_NODE Node, PUNICODE_STRING DeviceInstance) +NTSTATUS +IopFindDeviceInstanceTraverse( + _In_ PDEVICE_NODE DeviceNode, + _Inout_ PVOID Context) { - PDEVICE_OBJECT DeviceObject; - PDEVICE_NODE ChildNode; + PIOP_FIND_DEVICE_INSTANCE_TRAVERSE_CONTEXT DeviceInstanceContext = Context; - if (RtlEqualUnicodeString(&Node->InstancePath, - DeviceInstance, TRUE)) + if (RtlEqualUnicodeString(&DeviceNode->InstancePath, + DeviceInstanceContext->InstancePath, TRUE)) { - ObReferenceObject(Node->PhysicalDeviceObject); - return Node->PhysicalDeviceObject; - } + ObReferenceObject(DeviceNode->PhysicalDeviceObject); + DeviceInstanceContext->DeviceObject = DeviceNode->PhysicalDeviceObject; - /* Traversal of all children nodes */ - for (ChildNode = Node->Child; - ChildNode != NULL; - ChildNode = ChildNode->Sibling) - { - DeviceObject = IopTraverseDeviceNode(ChildNode, DeviceInstance); - if (DeviceObject != NULL) - { - return DeviceObject; - } + /* Stop enumeration */ + return STATUS_UNSUCCESSFUL; } - return NULL; + return STATUS_SUCCESS; } - PDEVICE_OBJECT IopGetDeviceObjectFromDeviceInstance(PUNICODE_STRING DeviceInstance) { + DEVICETREE_TRAVERSE_CONTEXT Context; + IOP_FIND_DEVICE_INSTANCE_TRAVERSE_CONTEXT DeviceInstanceContext; + if (IopRootDeviceNode == NULL) return NULL; @@ -136,7 +135,17 @@ IopGetDeviceObjectFromDeviceInstance(PUNICODE_STRING DeviceInstance) return NULL; } - return IopTraverseDeviceNode(IopRootDeviceNode, DeviceInstance); + /* Traverse the device tree to find the matching device node */ + DeviceInstanceContext.InstancePath = DeviceInstance; + DeviceInstanceContext.DeviceObject = NULL; + IopInitDeviceTreeTraverseContext(&Context, + IopRootDeviceNode, + IopFindDeviceInstanceTraverse, + &DeviceInstanceContext); + (void)IopTraverseDeviceTree(&Context); + + /* In case of error or instance not found, this will still be NULL from above. */ + return DeviceInstanceContext.DeviceObject; } static NTSTATUS