From: Pali Rohár Date: Thu, 21 Nov 2024 22:51:26 +0000 (+0100) Subject: windows: Do not cast FARPROC to LPVOID and then to some function pointer X-Git-Tag: v3.14.0~24 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=39147c1081aff7445c105673fc5729c547d1d5b1;p=thirdparty%2Fpciutils.git windows: Do not cast FARPROC to LPVOID and then to some function pointer FARPROC is function pointer type intptr_t(__stdcall*)() and LPVOID is data pointer type void*. Casting from function pointer to data pointer and back is undefined in C, and moreover in all cases it is not needed. In all cases it is just needed to cast FARPROC function pointer type to some specific function pointer type, and casting via intermediate LPVOID was there just to mute compiler warnings about casting between two incompatible function pointer types. To mute that compiler warning, do casting via intermediate generic function pointer type void(*)(void) which is preferred according to gcc documentation and does not throw any compiler warnings neither by gcc, nor by msvc compilers. --- diff --git a/lib/win32-helpers.c b/lib/win32-helpers.c index 0190f21..3d514a2 100644 --- a/lib/win32-helpers.c +++ b/lib/win32-helpers.c @@ -177,7 +177,7 @@ win32_change_error_mode(UINT new_mode) */ kernel32 = GetModuleHandle(TEXT("kernel32.dll")); if (kernel32) - MySetThreadErrorMode = (SetThreadErrorModeProt)(LPVOID)GetProcAddress(kernel32, "SetThreadErrorMode"); + MySetThreadErrorMode = (SetThreadErrorModeProt)(void(*)(void))GetProcAddress(kernel32, "SetThreadErrorMode"); if (MySetThreadErrorMode && MySetThreadErrorMode(new_mode, &old_mode)) @@ -597,7 +597,7 @@ prepare_security_descriptor_for_set_operation(PSECURITY_DESCRIPTOR security_desc if (!advapi32) return FALSE; - MySetSecurityDescriptorControl = (SetSecurityDescriptorControlProt)(LPVOID)GetProcAddress(advapi32, "SetSecurityDescriptorControl"); + MySetSecurityDescriptorControl = (SetSecurityDescriptorControlProt)(void(*)(void))GetProcAddress(advapi32, "SetSecurityDescriptorControl"); if (!MySetSecurityDescriptorControl) return FALSE; @@ -940,8 +940,8 @@ win32_find_and_open_process_for_query(LPCSTR exe_file) * kernel32.dll library with K32 prefix. */ MyGetModuleFileNameExW = NULL; - MyGetProcessImageFileNameW = (GetProcessImageFileNameWProt)(LPVOID)GetProcAddress(kernel32, "K32GetProcessImageFileNameW"); - MyEnumProcesses = (EnumProcessesProt)(LPVOID)GetProcAddress(kernel32, "K32EnumProcesses"); + MyGetProcessImageFileNameW = (GetProcessImageFileNameWProt)(void(*)(void))GetProcAddress(kernel32, "K32GetProcessImageFileNameW"); + MyEnumProcesses = (EnumProcessesProt)(void(*)(void))GetProcAddress(kernel32, "K32EnumProcesses"); if (!MyGetProcessImageFileNameW || !MyEnumProcesses) { /* @@ -960,9 +960,9 @@ win32_find_and_open_process_for_query(LPCSTR exe_file) * Windows XP and higher systems. On older versions is * available function GetModuleFileNameExW(). */ - MyGetProcessImageFileNameW = (GetProcessImageFileNameWProt)(LPVOID)GetProcAddress(psapi, "GetProcessImageFileNameW"); - MyGetModuleFileNameExW = (GetModuleFileNameExWProt)(LPVOID)GetProcAddress(psapi, "GetModuleFileNameExW"); - MyEnumProcesses = (EnumProcessesProt)(LPVOID)GetProcAddress(psapi, "EnumProcesses"); + MyGetProcessImageFileNameW = (GetProcessImageFileNameWProt)(void(*)(void))GetProcAddress(psapi, "GetProcessImageFileNameW"); + MyGetModuleFileNameExW = (GetModuleFileNameExWProt)(void(*)(void))GetProcAddress(psapi, "GetModuleFileNameExW"); + MyEnumProcesses = (EnumProcessesProt)(void(*)(void))GetProcAddress(psapi, "EnumProcesses"); if ((!MyGetProcessImageFileNameW && !MyGetModuleFileNameExW) || !MyEnumProcesses) { FreeLibrary(psapi);