From be97a36f25c3fec7ad8ca6c43aeadfe12d9eab34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herm=C3=A8s=20B=C3=A9lusca-Ma=C3=AFto?= Date: Sun, 19 Jan 2025 22:23:56 +0100 Subject: [PATCH] [MOUNTMGR] Simplify MountMgrQueryDosVolumePath() code (#6990) - Use a variable of correct type instead of casting every time. - Remove one level of indentation by returning early. --- drivers/storage/mountmgr/device.c | 70 ++++++++++++++++--------------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/drivers/storage/mountmgr/device.c b/drivers/storage/mountmgr/device.c index ca8ed5c1395..62d28cf7192 100644 --- a/drivers/storage/mountmgr/device.c +++ b/drivers/storage/mountmgr/device.c @@ -819,6 +819,7 @@ MountMgrQueryDosVolumePath(IN PDEVICE_EXTENSION DeviceExtension, PLIST_ENTRY SymlinksEntry; UNICODE_STRING SymbolicName; PMOUNTMGR_TARGET_NAME Target; + PMOUNTMGR_VOLUME_PATHS Output; PWSTR DeviceString, OldBuffer; USHORT DeviceLength, OldLength; PDEVICE_INFORMATION DeviceInformation; @@ -992,46 +993,47 @@ TryWithVolumeName: } RtlCopyMemory(DeviceString, SymlinkInformation->Name.Buffer, DeviceLength); - /* Ensure we are in the right namespace; [1] can be ? */ + /* Ensure we are in the Win32 namespace; [1] can be '?' */ DeviceString[1] = L'\\'; } } - /* If we found something */ - if (DeviceString) + /* If we didn't find something, fail */ + if (!DeviceString) + return STATUS_NOT_FOUND; + + /* Get the output buffer */ + Output = (PMOUNTMGR_VOLUME_PATHS)Irp->AssociatedIrp.SystemBuffer; + + /* At least, we will return our length */ + Output->MultiSzLength = DeviceLength; + /* MOUNTMGR_VOLUME_PATHS is a string + a ULONG */ + Irp->IoStatus.Information = DeviceLength + sizeof(ULONG); + + /* If we have enough room for copying the string */ + if (sizeof(ULONG) + DeviceLength <= Stack->Parameters.DeviceIoControl.OutputBufferLength) { - /* At least, we will return our length */ - ((PMOUNTMGR_VOLUME_PATHS)Irp->AssociatedIrp.SystemBuffer)->MultiSzLength = DeviceLength; - /* MOUNTMGR_VOLUME_PATHS is a string + a ULONG */ - Irp->IoStatus.Information = DeviceLength + sizeof(ULONG); - - /* If we have enough room for copying the string */ - if (sizeof(ULONG) + DeviceLength <= Stack->Parameters.DeviceIoControl.OutputBufferLength) + /* Copy it */ + if (DeviceLength) { - /* Copy it */ - if (DeviceLength) - { - RtlCopyMemory(((PMOUNTMGR_VOLUME_PATHS)Irp->AssociatedIrp.SystemBuffer)->MultiSz, DeviceString, DeviceLength); - } - - /* And double zero at its end - this is needed in case of multiple paths which are separated by a single 0 */ - FreePool(DeviceString); - ((PMOUNTMGR_VOLUME_PATHS)Irp->AssociatedIrp.SystemBuffer)->MultiSz[DeviceLength / sizeof(WCHAR)] = 0; - ((PMOUNTMGR_VOLUME_PATHS)Irp->AssociatedIrp.SystemBuffer)->MultiSz[DeviceLength / sizeof(WCHAR) + 1] = 0; - - return STATUS_SUCCESS; - } - else - { - /* Just return appropriate size and leave */ - FreePool(DeviceString); - Irp->IoStatus.Information = sizeof(ULONG); - return STATUS_BUFFER_OVERFLOW; + RtlCopyMemory(Output->MultiSz, DeviceString, DeviceLength); } + + /* And double-NUL at its end - this is needed in case of + * multiple paths which are separated by a single NUL */ + FreePool(DeviceString); + Output->MultiSz[DeviceLength / sizeof(WCHAR)] = UNICODE_NULL; + Output->MultiSz[DeviceLength / sizeof(WCHAR) + 1] = UNICODE_NULL; + + return STATUS_SUCCESS; + } + else + { + /* Just return the size needed and leave */ + FreePool(DeviceString); + Irp->IoStatus.Information = sizeof(ULONG); + return STATUS_BUFFER_OVERFLOW; } - - /* Fail */ - return STATUS_NOT_FOUND; } /* @@ -1557,7 +1559,7 @@ MountMgrQueryDosVolumePaths(IN PDEVICE_EXTENSION DeviceExtension, MountMgrNotifyNameChange(DeviceExtension, &SymbolicName, FALSE); } - /* Get output buffer */ + /* Get the output buffer */ Output = (PMOUNTMGR_VOLUME_PATHS)Irp->AssociatedIrp.SystemBuffer; /* Set required size */ @@ -1566,7 +1568,7 @@ MountMgrQueryDosVolumePaths(IN PDEVICE_EXTENSION DeviceExtension, /* Compute total length */ OutputLength = Output->MultiSzLength + sizeof(ULONG); - /* If it cannot fit, just return need size and quit */ + /* If it cannot fit, just return the size needed and leave */ if (OutputLength > Stack->Parameters.DeviceIoControl.OutputBufferLength) { Irp->IoStatus.Information = sizeof(ULONG);