https://git.reactos.org/?p=reactos.git;a=commitdiff;h=1ed7f27466c8fdd70779504d93b2383a5764a495

commit 1ed7f27466c8fdd70779504d93b2383a5764a495
Author:     Pierre Schweitzer <[email protected]>
AuthorDate: Sun May 5 09:25:34 2019 +0200
Commit:     Pierre Schweitzer <[email protected]>
CommitDate: Sun May 5 10:17:16 2019 +0200

    [KERNEL32] Reduce DefineDosDeviceA memory footprint
    
    by using TEB static unicode string (which is already
    preallocated).
---
 dll/win32/kernel32/client/dosdev.c | 60 +++++++++++++++++++++++++-------------
 1 file changed, 40 insertions(+), 20 deletions(-)

diff --git a/dll/win32/kernel32/client/dosdev.c 
b/dll/win32/kernel32/client/dosdev.c
index 5352e57f06..f077b5b3b3 100644
--- a/dll/win32/kernel32/client/dosdev.c
+++ b/dll/win32/kernel32/client/dosdev.c
@@ -30,41 +30,61 @@ DefineDosDeviceA(
     LPCSTR lpTargetPath
     )
 {
-    UNICODE_STRING DeviceNameU = {0};
-    UNICODE_STRING TargetPathU = {0};
     BOOL Result;
+    NTSTATUS Status;
+    ANSI_STRING AnsiString;
+    PWSTR TargetPathBuffer;
+    UNICODE_STRING TargetPathU;
+    PUNICODE_STRING DeviceNameU;
 
-    if (lpDeviceName &&
-        !RtlCreateUnicodeStringFromAsciiz(&DeviceNameU, lpDeviceName))
+    /* Convert DeviceName using static unicode string */
+    RtlInitAnsiString(&AnsiString, lpDeviceName);
+    DeviceNameU = &NtCurrentTeb()->StaticUnicodeString;
+    Status = RtlAnsiStringToUnicodeString(DeviceNameU, &AnsiString, FALSE);
+    if (!NT_SUCCESS(Status))
     {
-        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        return 0;
+        /*
+         * If the static unicode string is too small,
+         * it's because the name is too long...
+         * so, return appropriate status!
+         */
+        if (Status == STATUS_BUFFER_OVERFLOW)
+        {
+            SetLastError(ERROR_FILENAME_EXCED_RANGE);
+            return FALSE;
+        }
+
+        BaseSetLastNTError(Status);
+        return FALSE;
     }
 
-    if (lpTargetPath &&
-        !RtlCreateUnicodeStringFromAsciiz(&TargetPathU, lpTargetPath))
+    /* Convert target path if existing */
+    if (lpTargetPath != NULL)
     {
-        if (DeviceNameU.Buffer)
+        RtlInitAnsiString(&AnsiString, lpTargetPath);
+        Status = RtlAnsiStringToUnicodeString(&TargetPathU, &AnsiString, TRUE);
+        if (!NT_SUCCESS(Status))
         {
-            RtlFreeUnicodeString(&DeviceNameU);
+            BaseSetLastNTError(Status);
+            return FALSE;
         }
-        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        return 0;
+
+        TargetPathBuffer = TargetPathU.Buffer;
+    }
+    else
+    {
+        TargetPathBuffer = NULL;
     }
 
-    Result = DefineDosDeviceW(dwFlags,
-                              DeviceNameU.Buffer,
-                              TargetPathU.Buffer);
+    /* Call W */
+    Result = DefineDosDeviceW(dwFlags, DeviceNameU->Buffer, TargetPathBuffer);
 
-    if (TargetPathU.Buffer)
+    /* Free target path if allocated */
+    if (TargetPathBuffer != NULL)
     {
         RtlFreeUnicodeString(&TargetPathU);
     }
 
-    if (DeviceNameU.Buffer)
-    {
-        RtlFreeUnicodeString(&DeviceNameU);
-    }
     return Result;
 }
 

Reply via email to