https://git.reactos.org/?p=reactos.git;a=commitdiff;h=373a8dbb5c654e9cbde05…
commit 373a8dbb5c654e9cbde05f77363f537548c838eb
Author:     Serge Gautherie <32623169+SergeGautherie(a)users.noreply.github.com>
AuthorDate: Sat May 23 09:08:00 2020 +0200
Commit:     GitHub <noreply(a)github.com>
CommitDate: Sat May 23 09:08:00 2020 +0200
    [WINSPOOL] GetPrinterA(): Refactor failure handling (#2830)
    Addendum to ecde3768254664ca40a3f2fa49d1342d4bd92c02.
---
 win32ss/printing/base/winspool/printers.c | 55 ++++++++++++++++---------------
 1 file changed, 29 insertions(+), 26 deletions(-)
diff --git a/win32ss/printing/base/winspool/printers.c
b/win32ss/printing/base/winspool/printers.c
index 219e831cb16..1576ca0edd4 100644
--- a/win32ss/printing/base/winspool/printers.c
+++ b/win32ss/printing/base/winspool/printers.c
@@ -1117,6 +1117,8 @@ Cleanup:
 BOOL WINAPI
 GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD
pcbNeeded)
 {
+    DWORD dwErrorCode;
+    BOOL bResult;
     PPRINTER_INFO_1A ppi1a = (PPRINTER_INFO_1A)pPrinter;
     PPRINTER_INFO_1W ppi1w = (PPRINTER_INFO_1W)pPrinter;
     PPRINTER_INFO_2A ppi2a = (PPRINTER_INFO_2A)pPrinter;
@@ -1128,23 +1130,21 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD
cbBuf, LPDWORD
     PPRINTER_INFO_7A ppi7a = (PPRINTER_INFO_7A)pPrinter;
     PPRINTER_INFO_7W ppi7w = (PPRINTER_INFO_7W)pPrinter;
     DWORD cch;
-    BOOL bReturnValue = FALSE;
     TRACE("GetPrinterA(%p, %lu, %p, %lu, %p)\n", hPrinter, Level, pPrinter,
cbBuf, pcbNeeded);
     // Check for invalid levels here for early error return. Should be 1-9.
     if (Level <  1 || Level > 9)
     {
-        SetLastError(ERROR_INVALID_LEVEL);
+        dwErrorCode = ERROR_INVALID_LEVEL;
         ERR("Invalid Level!\n");
         goto Cleanup;
     }
-    bReturnValue = GetPrinterW(hPrinter, Level, pPrinter, cbBuf, pcbNeeded);
-
-    if (!bReturnValue)
+    bResult = GetPrinterW(hPrinter, Level, pPrinter, cbBuf, pcbNeeded);
+    if (!bResult)
     {
-        TRACE("GetPrinterW failed!\n");
+        dwErrorCode = GetLastError();
         goto Cleanup;
     }
@@ -1162,7 +1162,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD
cbBuf, LPDWORD
                 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;
                 }
@@ -1183,7 +1183,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD
cbBuf, LPDWORD
                 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;
                 }
@@ -1204,7 +1204,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD
cbBuf, LPDWORD
                 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;
                 }
@@ -1229,7 +1229,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD
cbBuf, LPDWORD
                 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;
                 }
@@ -1250,7 +1250,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD
cbBuf, LPDWORD
                 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;
                 }
@@ -1271,7 +1271,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD
cbBuf, LPDWORD
                 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;
                 }
@@ -1292,7 +1292,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD
cbBuf, LPDWORD
                 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;
                 }
@@ -1313,7 +1313,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD
cbBuf, LPDWORD
                 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;
                 }
@@ -1334,7 +1334,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD
cbBuf, LPDWORD
                 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;
                 }
@@ -1355,7 +1355,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD
cbBuf, LPDWORD
                 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;
                 }
@@ -1376,7 +1376,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD
cbBuf, LPDWORD
                 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;
                 }
@@ -1397,7 +1397,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD
cbBuf, LPDWORD
                 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;
                 }
@@ -1418,7 +1418,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD
cbBuf, LPDWORD
                 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;
                 }
@@ -1439,7 +1439,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD
cbBuf, LPDWORD
                 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;
                 }
@@ -1464,7 +1464,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD
cbBuf, LPDWORD
                 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;
                 }
@@ -1485,7 +1485,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD
cbBuf, LPDWORD
                 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;
                 }
@@ -1510,7 +1510,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD
cbBuf, LPDWORD
                 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;
                 }
@@ -1531,7 +1531,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD
cbBuf, LPDWORD
                 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;
                 }
@@ -1556,7 +1556,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD
cbBuf, LPDWORD
                 pszaObjectGUID = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
                 if (!pszaObjectGUID)
                 {
-                    SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+                    dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
                     ERR("HeapAlloc failed!\n");
                     goto Cleanup;
                 }
@@ -1570,8 +1570,11 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD
cbBuf, LPDWORD
         }
     }       // switch
+    dwErrorCode = ERROR_SUCCESS;
+
 Cleanup:
-    return bReturnValue;
+    SetLastError(dwErrorCode);
+    return (dwErrorCode == ERROR_SUCCESS);
 }
 BOOL WINAPI