Fix bunch of bugs in GetVolumeNameForVolumeMountPointW. Thanks to w3seek.
authorFilip Navara <filip.navara@gmail.com>
Sat, 17 Dec 2005 11:31:12 +0000 (11:31 +0000)
committerFilip Navara <filip.navara@gmail.com>
Sat, 17 Dec 2005 11:31:12 +0000 (11:31 +0000)
svn path=/trunk/; revision=20218

reactos/lib/kernel32/file/volume.c

index 31fb8b2..d0cf382 100644 (file)
@@ -826,10 +826,17 @@ GetVolumeNameForVolumeMountPointW(
     * an NT acceptable name.
     */
 
     * an NT acceptable name.
     */
 
-   RtlDosPathNameToNtPathName_U(L"e:\\", &NtFileName, NULL, NULL);
+   if (!RtlDosPathNameToNtPathName_U(VolumeName, &NtFileName, NULL, NULL))
+   {
+      SetLastError(ERROR_PATH_NOT_FOUND);
+      return FALSE;
+   }
+
    if (NtFileName.Length > sizeof(WCHAR) &&
        NtFileName.Buffer[(NtFileName.Length / sizeof(WCHAR)) - 1] == '\\')
    if (NtFileName.Length > sizeof(WCHAR) &&
        NtFileName.Buffer[(NtFileName.Length / sizeof(WCHAR)) - 1] == '\\')
+   {
       NtFileName.Length -= sizeof(WCHAR);
       NtFileName.Length -= sizeof(WCHAR);
+   }
 
    /*
     * Query mount point device name which we will later use for determining
 
    /*
     * Query mount point device name which we will later use for determining
@@ -852,17 +859,26 @@ GetVolumeNameForVolumeMountPointW(
    do
    {
       MountDevName = Buffer = RtlAllocateHeap(GetProcessHeap(), 0, BufferLength);
    do
    {
       MountDevName = Buffer = RtlAllocateHeap(GetProcessHeap(), 0, BufferLength);
+      if (Buffer == NULL)
+      {
+         NtClose(FileHandle);
+         SetLastErrorByStatus(Status);
+         return FALSE;
+      }
+
       Status = NtDeviceIoControlFile(FileHandle, NULL, NULL, NULL, &Iosb,
                                      IOCTL_MOUNTDEV_QUERY_DEVICE_NAME,
                                      NULL, 0, Buffer, BufferLength);
       if (Status == STATUS_BUFFER_OVERFLOW)
       {
          BufferLength = sizeof(MOUNTDEV_NAME) + MountDevName->NameLength;
       Status = NtDeviceIoControlFile(FileHandle, NULL, NULL, NULL, &Iosb,
                                      IOCTL_MOUNTDEV_QUERY_DEVICE_NAME,
                                      NULL, 0, Buffer, BufferLength);
       if (Status == STATUS_BUFFER_OVERFLOW)
       {
          BufferLength = sizeof(MOUNTDEV_NAME) + MountDevName->NameLength;
+         RtlFreeHeap(GetProcessHeap(), 0, Buffer);
          continue;
       }
       else if (!NT_SUCCESS(Status))
       {
          RtlFreeHeap(GetProcessHeap(), 0, Buffer);
          continue;
       }
       else if (!NT_SUCCESS(Status))
       {
          RtlFreeHeap(GetProcessHeap(), 0, Buffer);
+         NtClose(FileHandle);
          SetLastErrorByStatus(Status);
          return FALSE;
       }
          SetLastErrorByStatus(Status);
          return FALSE;
       }
@@ -877,6 +893,12 @@ GetVolumeNameForVolumeMountPointW(
 
    MountPointSize = MountDevName->NameLength + sizeof(MOUNTMGR_MOUNT_POINT);
    MountPoint = RtlAllocateHeap(GetProcessHeap(), 0, MountPointSize);
 
    MountPointSize = MountDevName->NameLength + sizeof(MOUNTMGR_MOUNT_POINT);
    MountPoint = RtlAllocateHeap(GetProcessHeap(), 0, MountPointSize);
+   if (MountPoint == NULL)
+   {
+      RtlFreeHeap(GetProcessHeap(), 0, MountDevName);
+      SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+      return FALSE;
+   }
    RtlZeroMemory(MountPoint, sizeof(MOUNTMGR_MOUNT_POINT));
    MountPoint->DeviceNameOffset = sizeof(MOUNTMGR_MOUNT_POINT);
    MountPoint->DeviceNameLength = MountDevName->NameLength;
    RtlZeroMemory(MountPoint, sizeof(MOUNTMGR_MOUNT_POINT));
    MountPoint->DeviceNameOffset = sizeof(MOUNTMGR_MOUNT_POINT);
    MountPoint->DeviceNameLength = MountDevName->NameLength;
@@ -899,6 +921,14 @@ GetVolumeNameForVolumeMountPointW(
    do
    {
       MountPoints = Buffer = RtlAllocateHeap(GetProcessHeap(), 0, BufferLength);
    do
    {
       MountPoints = Buffer = RtlAllocateHeap(GetProcessHeap(), 0, BufferLength);
+      if (Buffer == NULL)
+      {
+         RtlFreeHeap(GetProcessHeap(), 0, MountPoint);
+         NtClose(FileHandle);
+         SetLastErrorByStatus(Status);
+         return FALSE;
+      }
+
       Status = NtDeviceIoControlFile(FileHandle, NULL, NULL, NULL, &Iosb,
                                      IOCTL_MOUNTMGR_QUERY_POINTS,
                                      MountPoint, MountPointSize,
       Status = NtDeviceIoControlFile(FileHandle, NULL, NULL, NULL, &Iosb,
                                      IOCTL_MOUNTMGR_QUERY_POINTS,
                                      MountPoint, MountPointSize,
@@ -906,6 +936,7 @@ GetVolumeNameForVolumeMountPointW(
       if (Status == STATUS_BUFFER_OVERFLOW)
       {
          BufferLength = MountPoints->Size;
       if (Status == STATUS_BUFFER_OVERFLOW)
       {
          BufferLength = MountPoints->Size;
+         RtlFreeHeap(GetProcessHeap(), 0, Buffer);
          continue;
       }
       else if (!NT_SUCCESS(Status))
          continue;
       }
       else if (!NT_SUCCESS(Status))
@@ -938,7 +969,7 @@ GetVolumeNameForVolumeMountPointW(
 
       if (MountPoint->SymbolicLinkNameLength == 48 * sizeof(WCHAR) ||
           (MountPoint->SymbolicLinkNameLength == 49 * sizeof(WCHAR) &&
 
       if (MountPoint->SymbolicLinkNameLength == 48 * sizeof(WCHAR) ||
           (MountPoint->SymbolicLinkNameLength == 49 * sizeof(WCHAR) &&
-           SymbolicLinkName[49] == L'\\'))
+           SymbolicLinkName[48] == L'\\'))
       {
          if (RtlCompareMemory(SymbolicLinkName, L"\\??\\Volume{",
                               11 * sizeof(WCHAR)) == 11 * sizeof(WCHAR) &&
       {
          if (RtlCompareMemory(SymbolicLinkName, L"\\??\\Volume{",
                               11 * sizeof(WCHAR)) == 11 * sizeof(WCHAR) &&