Code enhancement: remove goto label

Close #6053, close #6054
This commit is contained in:
Don HO 2019-08-16 03:28:49 +02:00
parent 67305b978c
commit 77da706207
No known key found for this signature in database
GPG Key ID: 6C429F1D8D84F46E
1 changed files with 23 additions and 25 deletions

View File

@ -76,39 +76,37 @@ bool PluginsManager::unloadPlugin(int index, HWND nppHandle)
return true; return true;
} }
static WORD GetBinaryArchitectureType(const TCHAR *filePath) static WORD getBinaryArchitectureType(const TCHAR *filePath)
{ {
WORD machine_type = IMAGE_FILE_MACHINE_UNKNOWN;
HANDLE hMapping = NULL;
LPVOID addrHeader = NULL;
HANDLE hFile = CreateFile(filePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL); HANDLE hFile = CreateFile(filePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
if (hFile == INVALID_HANDLE_VALUE) if (hFile == INVALID_HANDLE_VALUE)
goto cleanup; {
return IMAGE_FILE_MACHINE_UNKNOWN;
}
hMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, NULL); HANDLE hMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, NULL);
if (hMapping == NULL) if (hMapping == NULL)
goto cleanup; {
CloseHandle(hFile);
return IMAGE_FILE_MACHINE_UNKNOWN;
}
addrHeader = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0); LPVOID addrHeader = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
if (addrHeader == NULL) if (addrHeader == NULL) // couldn't memory map the file
goto cleanup; // couldn't memory map the file {
CloseHandle(hFile);
CloseHandle(hMapping);
return IMAGE_FILE_MACHINE_UNKNOWN;
}
PIMAGE_NT_HEADERS peHdr = ImageNtHeader(addrHeader); PIMAGE_NT_HEADERS peHdr = ImageNtHeader(addrHeader);
if (peHdr == NULL)
goto cleanup; // couldn't read the header
// Found the binary and architecture type // Found the binary and architecture type, if peHdr is !NULL
machine_type = peHdr->FileHeader.Machine; WORD machine_type = (peHdr == NULL) ? IMAGE_FILE_MACHINE_UNKNOWN : peHdr->FileHeader.Machine;
cleanup: // release all of our handles // release all of our handles
if (addrHeader != NULL)
UnmapViewOfFile(addrHeader); UnmapViewOfFile(addrHeader);
if (hMapping != NULL)
CloseHandle(hMapping); CloseHandle(hMapping);
if (hFile != INVALID_HANDLE_VALUE)
CloseHandle(hFile); CloseHandle(hFile);
return machine_type; return machine_type;
@ -130,7 +128,7 @@ int PluginsManager::loadPlugin(const TCHAR *pluginFilePath)
{ {
pi->_moduleName = pluginFileName; pi->_moduleName = pluginFileName;
if (GetBinaryArchitectureType(pluginFilePath) != ARCH_TYPE) if (getBinaryArchitectureType(pluginFilePath) != ARCH_TYPE)
throw generic_string(ARCH_ERR_MSG); throw generic_string(ARCH_ERR_MSG);
const DWORD dwFlags = GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "AddDllDirectory") != NULL ? LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS : 0; const DWORD dwFlags = GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "AddDllDirectory") != NULL ? LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS : 0;