[WINSPOOL] GetPrinterA(): Refactor failure handling (#2830)

Addendum to ecde376825.
This commit is contained in:
Serge Gautherie 2020-05-23 09:08:00 +02:00 committed by GitHub
parent 57401cb66d
commit 373a8dbb5c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1117,6 +1117,8 @@ Cleanup:
BOOL WINAPI BOOL WINAPI
GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD pcbNeeded) 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_1A ppi1a = (PPRINTER_INFO_1A)pPrinter;
PPRINTER_INFO_1W ppi1w = (PPRINTER_INFO_1W)pPrinter; PPRINTER_INFO_1W ppi1w = (PPRINTER_INFO_1W)pPrinter;
PPRINTER_INFO_2A ppi2a = (PPRINTER_INFO_2A)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_7A ppi7a = (PPRINTER_INFO_7A)pPrinter;
PPRINTER_INFO_7W ppi7w = (PPRINTER_INFO_7W)pPrinter; PPRINTER_INFO_7W ppi7w = (PPRINTER_INFO_7W)pPrinter;
DWORD cch; DWORD cch;
BOOL bReturnValue = FALSE;
TRACE("GetPrinterA(%p, %lu, %p, %lu, %p)\n", hPrinter, Level, pPrinter, cbBuf, pcbNeeded); 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. // Check for invalid levels here for early error return. Should be 1-9.
if (Level < 1 || Level > 9) if (Level < 1 || Level > 9)
{ {
SetLastError(ERROR_INVALID_LEVEL); dwErrorCode = ERROR_INVALID_LEVEL;
ERR("Invalid Level!\n"); ERR("Invalid Level!\n");
goto Cleanup; goto Cleanup;
} }
bReturnValue = GetPrinterW(hPrinter, Level, pPrinter, cbBuf, pcbNeeded); bResult = GetPrinterW(hPrinter, Level, pPrinter, cbBuf, pcbNeeded);
if (!bResult)
if (!bReturnValue)
{ {
TRACE("GetPrinterW failed!\n"); dwErrorCode = GetLastError();
goto Cleanup; goto Cleanup;
} }
@ -1162,7 +1162,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD
pszDescription = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR)); pszDescription = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
if (!pszDescription) if (!pszDescription)
{ {
SetLastError(ERROR_NOT_ENOUGH_MEMORY); dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
ERR("HeapAlloc failed!\n"); ERR("HeapAlloc failed!\n");
goto Cleanup; goto Cleanup;
} }
@ -1183,7 +1183,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD
pszName = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR)); pszName = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
if (!pszName) if (!pszName)
{ {
SetLastError(ERROR_NOT_ENOUGH_MEMORY); dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
ERR("HeapAlloc failed!\n"); ERR("HeapAlloc failed!\n");
goto Cleanup; goto Cleanup;
} }
@ -1204,7 +1204,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD
pszComment = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR)); pszComment = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
if (!pszComment) if (!pszComment)
{ {
SetLastError(ERROR_NOT_ENOUGH_MEMORY); dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
ERR("HeapAlloc failed!\n"); ERR("HeapAlloc failed!\n");
goto Cleanup; goto Cleanup;
} }
@ -1229,7 +1229,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD
pszServerName = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR)); pszServerName = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
if (!pszServerName) if (!pszServerName)
{ {
SetLastError(ERROR_NOT_ENOUGH_MEMORY); dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
ERR("HeapAlloc failed!\n"); ERR("HeapAlloc failed!\n");
goto Cleanup; goto Cleanup;
} }
@ -1250,7 +1250,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD
pszPrinterName = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR)); pszPrinterName = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
if (!pszPrinterName) if (!pszPrinterName)
{ {
SetLastError(ERROR_NOT_ENOUGH_MEMORY); dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
ERR("HeapAlloc failed!\n"); ERR("HeapAlloc failed!\n");
goto Cleanup; goto Cleanup;
} }
@ -1271,7 +1271,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD
pszShareName = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR)); pszShareName = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
if (!pszShareName) if (!pszShareName)
{ {
SetLastError(ERROR_NOT_ENOUGH_MEMORY); dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
ERR("HeapAlloc failed!\n"); ERR("HeapAlloc failed!\n");
goto Cleanup; goto Cleanup;
} }
@ -1292,7 +1292,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD
pszPortName = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR)); pszPortName = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
if (!pszPortName) if (!pszPortName)
{ {
SetLastError(ERROR_NOT_ENOUGH_MEMORY); dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
ERR("HeapAlloc failed!\n"); ERR("HeapAlloc failed!\n");
goto Cleanup; goto Cleanup;
} }
@ -1313,7 +1313,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD
pszDriverName = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR)); pszDriverName = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
if (!pszDriverName) if (!pszDriverName)
{ {
SetLastError(ERROR_NOT_ENOUGH_MEMORY); dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
ERR("HeapAlloc failed!\n"); ERR("HeapAlloc failed!\n");
goto Cleanup; goto Cleanup;
} }
@ -1334,7 +1334,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD
pszComment = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR)); pszComment = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
if (!pszComment) if (!pszComment)
{ {
SetLastError(ERROR_NOT_ENOUGH_MEMORY); dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
ERR("HeapAlloc failed!\n"); ERR("HeapAlloc failed!\n");
goto Cleanup; goto Cleanup;
} }
@ -1355,7 +1355,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD
pszLocation = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR)); pszLocation = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
if (!pszLocation) if (!pszLocation)
{ {
SetLastError(ERROR_NOT_ENOUGH_MEMORY); dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
ERR("HeapAlloc failed!\n"); ERR("HeapAlloc failed!\n");
goto Cleanup; goto Cleanup;
} }
@ -1376,7 +1376,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD
pszSepFile = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR)); pszSepFile = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
if (!pszSepFile) if (!pszSepFile)
{ {
SetLastError(ERROR_NOT_ENOUGH_MEMORY); dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
ERR("HeapAlloc failed!\n"); ERR("HeapAlloc failed!\n");
goto Cleanup; goto Cleanup;
} }
@ -1397,7 +1397,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD
pszPrintProcessor = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR)); pszPrintProcessor = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
if (!pszPrintProcessor) if (!pszPrintProcessor)
{ {
SetLastError(ERROR_NOT_ENOUGH_MEMORY); dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
ERR("HeapAlloc failed!\n"); ERR("HeapAlloc failed!\n");
goto Cleanup; goto Cleanup;
} }
@ -1418,7 +1418,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD
pszDatatype = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR)); pszDatatype = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
if (!pszDatatype) if (!pszDatatype)
{ {
SetLastError(ERROR_NOT_ENOUGH_MEMORY); dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
ERR("HeapAlloc failed!\n"); ERR("HeapAlloc failed!\n");
goto Cleanup; goto Cleanup;
} }
@ -1439,7 +1439,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD
pszParameters = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR)); pszParameters = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
if (!pszParameters) if (!pszParameters)
{ {
SetLastError(ERROR_NOT_ENOUGH_MEMORY); dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
ERR("HeapAlloc failed!\n"); ERR("HeapAlloc failed!\n");
goto Cleanup; goto Cleanup;
} }
@ -1464,7 +1464,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD
pszPrinterName = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR)); pszPrinterName = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
if (!pszPrinterName) if (!pszPrinterName)
{ {
SetLastError(ERROR_NOT_ENOUGH_MEMORY); dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
ERR("HeapAlloc failed!\n"); ERR("HeapAlloc failed!\n");
goto Cleanup; goto Cleanup;
} }
@ -1485,7 +1485,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD
pszServerName = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR)); pszServerName = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
if (!pszServerName) if (!pszServerName)
{ {
SetLastError(ERROR_NOT_ENOUGH_MEMORY); dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
ERR("HeapAlloc failed!\n"); ERR("HeapAlloc failed!\n");
goto Cleanup; goto Cleanup;
} }
@ -1510,7 +1510,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD
pszPrinterName = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR)); pszPrinterName = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
if (!pszPrinterName) if (!pszPrinterName)
{ {
SetLastError(ERROR_NOT_ENOUGH_MEMORY); dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
ERR("HeapAlloc failed!\n"); ERR("HeapAlloc failed!\n");
goto Cleanup; goto Cleanup;
} }
@ -1531,7 +1531,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD
pszPortName = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR)); pszPortName = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
if (!pszPortName) if (!pszPortName)
{ {
SetLastError(ERROR_NOT_ENOUGH_MEMORY); dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
ERR("HeapAlloc failed!\n"); ERR("HeapAlloc failed!\n");
goto Cleanup; goto Cleanup;
} }
@ -1556,7 +1556,7 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD
pszaObjectGUID = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR)); pszaObjectGUID = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(CHAR));
if (!pszaObjectGUID) if (!pszaObjectGUID)
{ {
SetLastError(ERROR_NOT_ENOUGH_MEMORY); dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
ERR("HeapAlloc failed!\n"); ERR("HeapAlloc failed!\n");
goto Cleanup; goto Cleanup;
} }
@ -1570,8 +1570,11 @@ GetPrinterA(HANDLE hPrinter, DWORD Level, LPBYTE pPrinter, DWORD cbBuf, LPDWORD
} }
} // switch } // switch
dwErrorCode = ERROR_SUCCESS;
Cleanup: Cleanup:
return bReturnValue; SetLastError(dwErrorCode);
return (dwErrorCode == ERROR_SUCCESS);
} }
BOOL WINAPI BOOL WINAPI