[ros-diffs] [fireball] 34096: - Rewrite David Welch's Ansi function wrappers to reduce their code size, improve readability, and fix a bug of freeing of a non-allocated string. Spotted by Christoph vW.

fireball at svn.reactos.org fireball at svn.reactos.org
Thu Jun 26 13:49:47 CEST 2008


Author: fireball
Date: Thu Jun 26 06:49:47 2008
New Revision: 34096

URL: http://svn.reactos.org/svn/reactos?rev=34096&view=rev
Log:
- Rewrite David Welch's Ansi function wrappers to reduce their code size, improve readability, and fix a bug of freeing of a non-allocated string. Spotted by Christoph vW.

Modified:
    trunk/reactos/dll/win32/user32/misc/desktop.c
    trunk/reactos/dll/win32/user32/misc/winsta.c

Modified: trunk/reactos/dll/win32/user32/misc/desktop.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/user32/misc/desktop.c?rev=34096&r1=34095&r2=34096&view=diff
==============================================================================
--- trunk/reactos/dll/win32/user32/misc/desktop.c [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/user32/misc/desktop.c [iso-8859-1] Thu Jun 26 06:49:47 2008
@@ -468,35 +468,34 @@
 	       ACCESS_MASK dwDesiredAccess,
 	       LPSECURITY_ATTRIBUTES lpsa)
 {
-  ANSI_STRING DesktopNameA;
-  UNICODE_STRING DesktopNameU;
-  HDESK hDesktop;
-  LPDEVMODEW DevmodeW = NULL;
-
-  if (lpszDesktop != NULL)
+    UNICODE_STRING DesktopNameU;
+    HDESK hDesktop;
+    LPDEVMODEW DevmodeW = NULL;
+
+    if (lpszDesktop)
     {
-      RtlInitAnsiString(&DesktopNameA, (LPSTR)lpszDesktop);
-      RtlAnsiStringToUnicodeString(&DesktopNameU, &DesktopNameA, TRUE);
+        /* After conversion, the buffer is zero-terminated */
+        RtlCreateUnicodeStringFromAsciiz(&DesktopNameU, lpszDesktop);
     }
-  else
+    else
     {
-      RtlInitUnicodeString(&DesktopNameU, NULL);
+        RtlInitUnicodeString(&DesktopNameU, NULL);
     }
 
-  if (pDevmode)
-    {
-      DevmodeW = GdiConvertToDevmodeW(pDevmode);
-    }
-
-  hDesktop = CreateDesktopW(DesktopNameU.Buffer,
-			    NULL,
-			    DevmodeW,
-			    dwFlags,
-			    dwDesiredAccess,
-			    lpsa);
-
-  RtlFreeUnicodeString(&DesktopNameU);
-  return(hDesktop);
+    if (pDevmode)
+        DevmodeW = GdiConvertToDevmodeW(pDevmode);
+
+    hDesktop = CreateDesktopW(DesktopNameU.Buffer,
+                              NULL,
+                              DevmodeW,
+                              dwFlags,
+                              dwDesiredAccess,
+                              lpsa);
+
+    /* Free the string, if it was allocated */
+    if (lpszDesktop) RtlFreeUnicodeString(&DesktopNameU);
+
+    return hDesktop;
 }
 
 
@@ -580,26 +579,28 @@
   BOOL fInherit,
   ACCESS_MASK dwDesiredAccess)
 {
-  ANSI_STRING DesktopNameA;
-  UNICODE_STRING DesktopNameU;
-  HDESK hDesktop;
-
-	if (lpszDesktop != NULL) {
-		RtlInitAnsiString(&DesktopNameA, lpszDesktop);
-		RtlAnsiStringToUnicodeString(&DesktopNameU, &DesktopNameA, TRUE);
-  } else {
-    RtlInitUnicodeString(&DesktopNameU, NULL);
-  }
-
-  hDesktop = OpenDesktopW(
-    DesktopNameU.Buffer,
-    dwFlags,
-    fInherit,
-    dwDesiredAccess);
-
-	RtlFreeUnicodeString(&DesktopNameU);
-
-  return hDesktop;
+    UNICODE_STRING DesktopNameU;
+    HDESK hDesktop;
+
+    if (lpszDesktop)
+    {
+        /* After conversion, the buffer is zero-terminated */
+        RtlCreateUnicodeStringFromAsciiz(&DesktopNameU, lpszDesktop);
+    }
+    else
+    {
+        RtlInitUnicodeString(&DesktopNameU, NULL);
+    }
+
+    hDesktop = OpenDesktopW(DesktopNameU.Buffer,
+                            dwFlags,
+                            fInherit,
+                            dwDesiredAccess);
+
+    /* Free the string, if it was allocated */
+    if (lpszDesktop) RtlFreeUnicodeString(&DesktopNameU);
+
+    return hDesktop;
 }
 
 

Modified: trunk/reactos/dll/win32/user32/misc/winsta.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/user32/misc/winsta.c?rev=34096&r1=34095&r2=34096&view=diff
==============================================================================
--- trunk/reactos/dll/win32/user32/misc/winsta.c [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/user32/misc/winsta.c [iso-8859-1] Thu Jun 26 06:49:47 2008
@@ -32,29 +32,28 @@
 		     ACCESS_MASK dwDesiredAccess,
 		     LPSECURITY_ATTRIBUTES lpsa)
 {
-  ANSI_STRING WindowStationNameA;
-  UNICODE_STRING WindowStationNameU;
-  HWINSTA hWinSta;
-
-  if (lpwinsta != NULL)
+    UNICODE_STRING WindowStationNameU;
+    HWINSTA hWinSta;
+
+    if (lpwinsta)
     {
-      RtlInitAnsiString(&WindowStationNameA, lpwinsta);
-      RtlAnsiStringToUnicodeString(&WindowStationNameU, &WindowStationNameA,
-				   TRUE);
+        /* After conversion, the buffer is zero-terminated */
+        RtlCreateUnicodeStringFromAsciiz(&WindowStationNameU, lpwinsta);
     }
-  else
+    else
     {
-      RtlInitUnicodeString(&WindowStationNameU, NULL);
+        RtlInitUnicodeString(&WindowStationNameU, NULL);
     }
 
-  hWinSta = CreateWindowStationW(WindowStationNameU.Buffer,
-				 dwReserved,
-				 dwDesiredAccess,
-				 lpsa);
-
-  RtlFreeUnicodeString(&WindowStationNameU);
-
-  return hWinSta;
+    hWinSta = CreateWindowStationW(WindowStationNameU.Buffer,
+                                   dwReserved,
+                                   dwDesiredAccess,
+                                   lpsa);
+
+    /* Free the string, if it was allocated */
+    if (lpwinsta) RtlFreeUnicodeString(&WindowStationNameU);
+
+    return hWinSta;
 }
 
 
@@ -291,28 +290,27 @@
 		   BOOL fInherit,
 		   ACCESS_MASK dwDesiredAccess)
 {
-  ANSI_STRING WindowStationNameA;
-  UNICODE_STRING WindowStationNameU;
-  HWINSTA hWinSta;
-
-  if (lpszWinSta != NULL)
+    UNICODE_STRING WindowStationNameU;
+    HWINSTA hWinSta;
+
+    if (lpszWinSta)
     {
-      RtlInitAnsiString(&WindowStationNameA, lpszWinSta);
-      RtlAnsiStringToUnicodeString(&WindowStationNameU, &WindowStationNameA,
-				   TRUE);
+        /* After conversion, the buffer is zero-terminated */
+        RtlCreateUnicodeStringFromAsciiz(&WindowStationNameU, lpszWinSta);
     }
-  else
+    else
     {
-      RtlInitUnicodeString(&WindowStationNameU, NULL);
+        RtlInitUnicodeString(&WindowStationNameU, NULL);
     }
 
-  hWinSta = OpenWindowStationW(WindowStationNameU.Buffer,
-			       fInherit,
-			       dwDesiredAccess);
-
-  RtlFreeUnicodeString(&WindowStationNameU);
-
-  return hWinSta;
+    hWinSta = OpenWindowStationW(WindowStationNameU.Buffer,
+                                 fInherit,
+                                 dwDesiredAccess);
+
+    /* Free the string, if it was allocated */
+    if (lpszWinSta) RtlFreeUnicodeString(&WindowStationNameU);
+
+    return hWinSta;
 }
 
 



More information about the Ros-diffs mailing list