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

commit b139bae58dd40f8d378c0eb7154fafc03374961c
Author:     Serge Gautherie <[email protected]>
AuthorDate: Mon Apr 27 22:10:22 2020 +0200
Commit:     GitHub <[email protected]>
CommitDate: Mon Apr 27 22:10:22 2020 +0200

    [WINSPOOL] EnumPrintersA: Refactor failure handling (#2304)
---
 win32ss/printing/base/winspool/printers.c | 62 ++++++++++++++++++-------------
 1 file changed, 36 insertions(+), 26 deletions(-)

diff --git a/win32ss/printing/base/winspool/printers.c 
b/win32ss/printing/base/winspool/printers.c
index 4180dfb3d1c..219e831cb16 100644
--- a/win32ss/printing/base/winspool/printers.c
+++ b/win32ss/printing/base/winspool/printers.c
@@ -471,7 +471,8 @@ Cleanup:
 BOOL WINAPI
 EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE pPrinterEnum, DWORD 
cbBuf, PDWORD pcbNeeded, PDWORD pcReturned)
 {
-    BOOL bReturnValue = FALSE;
+    DWORD dwErrorCode;
+    BOOL bResult;
     DWORD cch;
     PWSTR pwszName = NULL;
     PSTR pszPrinterName = NULL;
@@ -502,7 +503,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE 
pPrinterEnum, DWORD cbB
     // Check for invalid levels here for early error return. MSDN says that 
only 1, 2, 4, and 5 are allowable.
     if (Level !=  1 && Level != 2 && Level != 4 && Level != 5)
     {
-        SetLastError(ERROR_INVALID_LEVEL);
+        dwErrorCode = ERROR_INVALID_LEVEL;
         ERR("Invalid Level!\n");
         goto Cleanup;
     }
@@ -515,7 +516,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE 
pPrinterEnum, DWORD cbB
         pwszName = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(WCHAR));
         if (!pwszName)
         {
-            SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+            dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
             ERR("HeapAlloc failed!\n");
             goto Cleanup;
         }
@@ -524,10 +525,12 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE 
pPrinterEnum, DWORD cbB
     }
  
     /* Ref: 
https://stackoverflow.com/questions/41147180/why-enumprintersa-and-enumprintersw-request-the-same-amount-of-memory
 */
-    bReturnValue = EnumPrintersW(Flags, pwszName, Level, pPrinterEnum, cbBuf, 
pcbNeeded, pcReturned);
-    HeapFree(hProcessHeap, 0, pwszName);
-
-    TRACE("*pcReturned is '%d' and bReturnValue is '%d' and GetLastError is 
'%ld'.\n", *pcReturned, bReturnValue, GetLastError());
+    bResult = EnumPrintersW(Flags, pwszName, Level, pPrinterEnum, cbBuf, 
pcbNeeded, pcReturned);
+    if (!bResult)
+    {
+        dwErrorCode = GetLastError();
+        goto Cleanup;
+    }
 
     /* We are mapping multiple different pointers to the same pPrinterEnum 
pointer here so that */
     /* we can do in-place conversion. We read the Unicode response from the 
EnumPrintersW and */
@@ -558,7 +561,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE 
pPrinterEnum, DWORD cbB
                     pszDescription = HeapAlloc(hProcessHeap, 0, (cch + 1) * 
sizeof(CHAR));
                     if (!pszDescription)
                     {
-                        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+                        dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
                         ERR("HeapAlloc failed!\n");
                         goto Cleanup;
                     }
@@ -577,7 +580,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE 
pPrinterEnum, DWORD cbB
                     pszName = HeapAlloc(hProcessHeap, 0, (cch + 1) * 
sizeof(CHAR));
                     if (!pszName)
                     {
-                        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+                        dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
                         ERR("HeapAlloc failed!\n");
                         goto Cleanup;
                     }
@@ -596,7 +599,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE 
pPrinterEnum, DWORD cbB
                     pszComment = HeapAlloc(hProcessHeap, 0, (cch + 1) * 
sizeof(CHAR));
                     if (!pszComment)
                     {
-                        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+                        dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
                         ERR("HeapAlloc failed!\n");
                         goto Cleanup;
                     }
@@ -620,7 +623,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE 
pPrinterEnum, DWORD cbB
                     pszServerName = HeapAlloc(hProcessHeap, 0, (cch + 1) * 
sizeof(CHAR));
                     if (!pszServerName)
                     {
-                        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+                        dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
                         ERR("HeapAlloc failed!\n");
                         goto Cleanup;
                     }
@@ -639,7 +642,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE 
pPrinterEnum, DWORD cbB
                     pszPrinterName = HeapAlloc(hProcessHeap, 0, (cch + 1) * 
sizeof(CHAR));
                     if (!pszPrinterName)
                     {
-                        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+                        dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
                         ERR("HeapAlloc failed!\n");
                         goto Cleanup;
                     }
@@ -658,7 +661,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE 
pPrinterEnum, DWORD cbB
                     pszShareName = HeapAlloc(hProcessHeap, 0, (cch + 1) * 
sizeof(CHAR));
                     if (!pszShareName)
                     {
-                        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+                        dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
                         ERR("HeapAlloc failed!\n");
                         goto Cleanup;
                     }
@@ -677,7 +680,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE 
pPrinterEnum, DWORD cbB
                     pszPortName = HeapAlloc(hProcessHeap, 0, (cch + 1) * 
sizeof(CHAR));
                     if (!pszPortName)
                     {
-                        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+                        dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
                         ERR("HeapAlloc failed!\n");
                         goto Cleanup;
                     }
@@ -696,7 +699,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE 
pPrinterEnum, DWORD cbB
                     pszDriverName = HeapAlloc(hProcessHeap, 0, (cch + 1) * 
sizeof(CHAR));
                     if (!pszDriverName)
                     {
-                        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+                        dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
                         ERR("HeapAlloc failed!\n");
                         goto Cleanup;
                     }
@@ -715,7 +718,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE 
pPrinterEnum, DWORD cbB
                     pszComment = HeapAlloc(hProcessHeap, 0, (cch + 1) * 
sizeof(CHAR));
                     if (!pszComment)
                     {
-                        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+                        dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
                         ERR("HeapAlloc failed!\n");
                         goto Cleanup;
                     }
@@ -734,7 +737,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE 
pPrinterEnum, DWORD cbB
                     pszLocation = HeapAlloc(hProcessHeap, 0, (cch + 1) * 
sizeof(CHAR));
                     if (!pszLocation)
                     {
-                        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+                        dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
                         ERR("HeapAlloc failed!\n");
                         goto Cleanup;
                     }
@@ -754,7 +757,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE 
pPrinterEnum, DWORD cbB
                     pszSepFile = HeapAlloc(hProcessHeap, 0, (cch + 1) * 
sizeof(CHAR));
                     if (!pszSepFile)
                     {
-                        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+                        dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
                         ERR("HeapAlloc failed!\n");
                         goto Cleanup;
                     }
@@ -773,7 +776,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE 
pPrinterEnum, DWORD cbB
                     pszPrintProcessor = HeapAlloc(hProcessHeap, 0, (cch + 1) * 
sizeof(CHAR));
                     if (!pszPrintProcessor)
                     {
-                        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+                        dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
                         ERR("HeapAlloc failed!\n");
                         goto Cleanup;
                     }
@@ -793,7 +796,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE 
pPrinterEnum, DWORD cbB
                     pszDatatype = HeapAlloc(hProcessHeap, 0, (cch + 1) * 
sizeof(CHAR));
                     if (!pszDatatype)
                     {
-                        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+                        dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
                         ERR("HeapAlloc failed!\n");
                         goto Cleanup;
                     }
@@ -812,7 +815,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE 
pPrinterEnum, DWORD cbB
                     pszParameters = HeapAlloc(hProcessHeap, 0, (cch + 1) * 
sizeof(CHAR));
                     if (!pszParameters)
                     {
-                        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+                        dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
                         ERR("HeapAlloc failed!\n");
                         goto Cleanup;
                     }
@@ -836,7 +839,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE 
pPrinterEnum, DWORD cbB
                     pszPrinterName = HeapAlloc(hProcessHeap, 0, (cch + 1) * 
sizeof(CHAR));
                     if (!pszPrinterName)
                     {
-                        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+                        dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
                         ERR("HeapAlloc failed!\n");
                         goto Cleanup;
                     }
@@ -855,7 +858,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE 
pPrinterEnum, DWORD cbB
                     pszServerName = HeapAlloc(hProcessHeap, 0, (cch + 1) * 
sizeof(CHAR));
                     if (!pszServerName)
                     {
-                        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+                        dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
                         ERR("HeapAlloc failed!\n");
                         goto Cleanup;
                     }
@@ -878,7 +881,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE 
pPrinterEnum, DWORD cbB
                     pszPrinterName = HeapAlloc(hProcessHeap, 0, (cch + 1) * 
sizeof(CHAR));
                     if (!pszPrinterName)
                     {
-                        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+                        dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
                         ERR("HeapAlloc failed!\n");
                         goto Cleanup;
                     }
@@ -897,7 +900,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE 
pPrinterEnum, DWORD cbB
                     pszPortName = HeapAlloc(hProcessHeap, 0, (cch + 1) * 
sizeof(CHAR));
                     if (!pszPortName)
                     {
-                        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+                        dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
                         ERR("HeapAlloc failed!\n");
                         goto Cleanup;
                     }
@@ -913,9 +916,16 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE 
pPrinterEnum, DWORD cbB
         }   // switch
     }       // for
 
+    dwErrorCode = ERROR_SUCCESS;
+
 Cleanup:
+    if (pwszName)
+    {
+        HeapFree(hProcessHeap, 0, pwszName);
+    }
 
-    return bReturnValue;
+    SetLastError(dwErrorCode);
+    return (dwErrorCode == ERROR_SUCCESS);
 }
 
 BOOL WINAPI

Reply via email to