[WINSPOOL] EnumPrintersA: Refactor failure handling (#2304)

This commit is contained in:
Serge Gautherie 2020-04-27 22:10:22 +02:00 committed by GitHub
parent 0b530fe496
commit b139bae58d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -471,7 +471,8 @@ Cleanup:
BOOL WINAPI BOOL WINAPI
EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE pPrinterEnum, DWORD cbBuf, PDWORD pcbNeeded, PDWORD pcReturned) EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE pPrinterEnum, DWORD cbBuf, PDWORD pcbNeeded, PDWORD pcReturned)
{ {
BOOL bReturnValue = FALSE; DWORD dwErrorCode;
BOOL bResult;
DWORD cch; DWORD cch;
PWSTR pwszName = NULL; PWSTR pwszName = NULL;
PSTR pszPrinterName = 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. // 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) if (Level != 1 && Level != 2 && Level != 4 && Level != 5)
{ {
SetLastError(ERROR_INVALID_LEVEL); dwErrorCode = ERROR_INVALID_LEVEL;
ERR("Invalid Level!\n"); ERR("Invalid Level!\n");
goto Cleanup; 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)); pwszName = HeapAlloc(hProcessHeap, 0, (cch + 1) * sizeof(WCHAR));
if (!pwszName) if (!pwszName)
{ {
SetLastError(ERROR_NOT_ENOUGH_MEMORY); dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
ERR("HeapAlloc failed!\n"); ERR("HeapAlloc failed!\n");
goto Cleanup; 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 */ /* 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); bResult = EnumPrintersW(Flags, pwszName, Level, pPrinterEnum, cbBuf, pcbNeeded, pcReturned);
HeapFree(hProcessHeap, 0, pwszName); if (!bResult)
{
TRACE("*pcReturned is '%d' and bReturnValue is '%d' and GetLastError is '%ld'.\n", *pcReturned, bReturnValue, GetLastError()); dwErrorCode = GetLastError();
goto Cleanup;
}
/* We are mapping multiple different pointers to the same pPrinterEnum pointer here so that */ /* 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 */ /* 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)); 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;
} }
@ -577,7 +580,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE pPrinterEnum, DWORD cbB
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;
} }
@ -596,7 +599,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE pPrinterEnum, DWORD cbB
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;
} }
@ -620,7 +623,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE pPrinterEnum, DWORD cbB
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;
} }
@ -639,7 +642,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE pPrinterEnum, DWORD cbB
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;
} }
@ -658,7 +661,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE pPrinterEnum, DWORD cbB
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;
} }
@ -677,7 +680,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE pPrinterEnum, DWORD cbB
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;
} }
@ -696,7 +699,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE pPrinterEnum, DWORD cbB
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;
} }
@ -715,7 +718,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE pPrinterEnum, DWORD cbB
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;
} }
@ -734,7 +737,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE pPrinterEnum, DWORD cbB
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;
} }
@ -754,7 +757,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE pPrinterEnum, DWORD cbB
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;
} }
@ -773,7 +776,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE pPrinterEnum, DWORD cbB
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;
} }
@ -793,7 +796,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE pPrinterEnum, DWORD cbB
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;
} }
@ -812,7 +815,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE pPrinterEnum, DWORD cbB
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;
} }
@ -836,7 +839,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE pPrinterEnum, DWORD cbB
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;
} }
@ -855,7 +858,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE pPrinterEnum, DWORD cbB
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;
} }
@ -878,7 +881,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE pPrinterEnum, DWORD cbB
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;
} }
@ -897,7 +900,7 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE pPrinterEnum, DWORD cbB
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;
} }
@ -913,9 +916,16 @@ EnumPrintersA(DWORD Flags, PSTR Name, DWORD Level, PBYTE pPrinterEnum, DWORD cbB
} // switch } // switch
} // for } // for
Cleanup: dwErrorCode = ERROR_SUCCESS;
return bReturnValue; Cleanup:
if (pwszName)
{
HeapFree(hProcessHeap, 0, pwszName);
}
SetLastError(dwErrorCode);
return (dwErrorCode == ERROR_SUCCESS);
} }
BOOL WINAPI BOOL WINAPI