Skip to content

Commit 18c9764

Browse files
committed
[Ref] mpt/base/utility.hpp: Add mpt::function_pointer_cast<Tdst>(Tsrc).
[Fix] mpt/base/utility.hpp: Silence Clang 19 warning that warns when casting between function pointer types of different signature, which is a necessity to do when using WinAPI GetProcAddress(). [Ref] mpt/library/library.hpp: Do not cast from void* to void(*)(void) and then to the destination function pointer. Instead, do the cast directly. [Ref] Replace all reinterpret_cast function pointer casts with mpt::function_pointer_cast. [Ref] mptLibrary: Use mpt::library::bind() instead of mpt::library::get_address(). git-svn-id: https://source.openmpt.org/svn/openmpt/trunk/OpenMPT@22969 56274372-70c3-4bfc-bfc3-4c3a0b034d27
1 parent 66d09e6 commit 18c9764

File tree

7 files changed

+51
-19
lines changed

7 files changed

+51
-19
lines changed

misc/WriteMemoryDump.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#include "openmpt/all/BuildSettings.hpp"
1414

15+
#include "mpt/base/utility.hpp"
16+
1517
#if MPT_COMPILER_MSVC
1618
#pragma warning(push)
1719
#pragma warning(disable:4091) // 'typedef ': ignored on left of '' when no variable is declared
@@ -32,7 +34,7 @@ inline bool WriteMemoryDump(_EXCEPTION_POINTERS *pExceptionInfo, const TCHAR *fi
3234
HMODULE hDll = ::LoadLibrary(_T("DBGHELP.DLL"));
3335
if(hDll)
3436
{
35-
MINIDUMPWRITEDUMP pDump = (MINIDUMPWRITEDUMP)::GetProcAddress(hDll, "MiniDumpWriteDump");
37+
MINIDUMPWRITEDUMP pDump = mpt::function_pointer_cast<MINIDUMPWRITEDUMP>(::GetProcAddress(hDll, "MiniDumpWriteDump"));
3638
if(pDump)
3739
{
3840

misc/mptLibrary.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ class LibraryHandle
119119
{
120120
return nullptr;
121121
}
122-
return lib->get_address(symbol);
122+
FuncPtr result = nullptr;
123+
lib->bind(result, symbol);
124+
return result;
123125
}
124126

125127
};

misc/mptLibrary.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#include "openmpt/all/BuildSettings.hpp"
1414

15+
#include "mpt/base/utility.hpp"
16+
1517

1618
OPENMPT_NAMESPACE_BEGIN
1719

@@ -97,7 +99,7 @@ class Library
9799
static_assert(std::is_function<Tfunc>::value);
98100
#endif
99101
const FuncPtr addr = GetProcAddress(symbol);
100-
f = reinterpret_cast<Tfunc*>(addr);
102+
f = mpt::function_pointer_cast<Tfunc*>(addr);
101103
return (addr != nullptr);
102104
}
103105
};

mptrack/Vstplug.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,9 @@ std::pair<Vst::AEffect *, Vst::MainProc> CVstPlugin::LoadPluginInternal(bool mas
219219

220220
if(library != nullptr && library != INVALID_HANDLE_VALUE)
221221
{
222-
auto pMainProc = reinterpret_cast<Vst::MainProc>(GetProcAddress(library, "VSTPluginMain"));
222+
auto pMainProc = mpt::function_pointer_cast<Vst::MainProc>(GetProcAddress(library, "VSTPluginMain"));
223223
if(pMainProc == nullptr)
224-
pMainProc = reinterpret_cast<Vst::MainProc>(GetProcAddress(library, "main"));
224+
pMainProc = mpt::function_pointer_cast<Vst::MainProc>(GetProcAddress(library, "main"));
225225

226226
if(pMainProc != nullptr)
227227
{

pluginBridge/Bridge.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,9 @@ void PluginBridge::InitBridge(InitMsg &msg)
368368
return;
369369
}
370370

371-
m_mainProc = reinterpret_cast<Vst::MainProc>(GetProcAddress(m_library, "VSTPluginMain"));
371+
m_mainProc = mpt::function_pointer_cast<Vst::MainProc>(GetProcAddress(m_library, "VSTPluginMain"));
372372
if(m_mainProc == nullptr)
373-
m_mainProc = reinterpret_cast<Vst::MainProc>(GetProcAddress(m_library, "main"));
373+
m_mainProc = mpt::function_pointer_cast<Vst::MainProc>(GetProcAddress(m_library, "main"));
374374

375375
if(m_mainProc != nullptr)
376376
{

src/mpt/base/utility.hpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66

77

8-
#include "mpt/base/detect_compiler.hpp"
9-
#include "mpt/base/detect_libcxx.hpp"
8+
#include "mpt/base/detect.hpp"
109
#include "mpt/base/namespace.hpp"
1110

1211
#if MPT_CXX_BEFORE(20) || MPT_LIBCXX_LLVM_BEFORE(13000)
@@ -37,6 +36,28 @@ MPT_CONSTEXPRINLINE Tdst c_cast(Tsrc && x) {
3736

3837

3938

39+
template <typename Tdst, typename Tsrc>
40+
MPT_CONSTEXPRINLINE Tdst function_pointer_cast(Tsrc f) {
41+
#if !(MPT_OS_WINDOWS && MPT_COMPILER_GCC)
42+
// MinGW64 std::is_function is always false for non __cdecl functions.
43+
// Issue is similar to <https://connect.microsoft.com/VisualStudio/feedback/details/774720/stl-is-function-bug>.
44+
static_assert(std::is_pointer<typename std::remove_cv<Tsrc>::type>::value);
45+
static_assert(std::is_pointer<typename std::remove_cv<Tdst>::type>::value);
46+
static_assert(std::is_function<typename std::remove_pointer<typename std::remove_cv<Tsrc>::type>::type>::value);
47+
static_assert(std::is_function<typename std::remove_pointer<typename std::remove_cv<Tdst>::type>::type>::value);
48+
#endif
49+
#if MPT_CLANG_AT_LEAST(19, 0, 0)
50+
#pragma clang diagnostic push
51+
#pragma clang diagnostic ignored "-Wcast-function-type-mismatch"
52+
#endif
53+
return reinterpret_cast<Tdst>(f);
54+
#if MPT_CLANG_AT_LEAST(19, 0, 0)
55+
#pragma clang diagnostic pop
56+
#endif
57+
}
58+
59+
60+
4061
#if MPT_CXX_AT_LEAST(20) && !MPT_LIBCXX_LLVM_BEFORE(13000)
4162

4263
using std::in_range;

src/mpt/library/library.hpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "mpt/base/macros.hpp"
1010
#include "mpt/base/namespace.hpp"
1111
#include "mpt/base/saturate_cast.hpp"
12+
#include "mpt/base/utility.hpp"
1213
#include "mpt/detect/dl.hpp"
1314
#include "mpt/detect/ltdl.hpp"
1415
#include "mpt/fs/common_directories.hpp"
@@ -315,8 +316,8 @@ class library {
315316
return library{hModule};
316317
}
317318

318-
func_ptr get_address(const std::string & symbol) const {
319-
return reinterpret_cast<func_ptr>(::GetProcAddress(m_hModule, symbol.c_str()));
319+
auto get_address(const std::string & symbol) const {
320+
return ::GetProcAddress(m_hModule, symbol.c_str());
320321
}
321322

322323
~library() {
@@ -370,8 +371,8 @@ class library {
370371
return library{handle};
371372
}
372373

373-
func_ptr get_address(const std::string & symbol) const {
374-
return reinterpret_cast<func_ptr>(dlsym(handle, symbol.c_str()));
374+
auto get_address(const std::string & symbol) const {
375+
return dlsym(handle, symbol.c_str());
375376
}
376377

377378
~library() {
@@ -428,8 +429,8 @@ class library {
428429
return library{handle};
429430
}
430431

431-
func_ptr get_address(const std::string & symbol) const {
432-
return reinterpret_cast<func_ptr>(lt_dlsym(handle, symbol.c_str()));
432+
auto get_address(const std::string & symbol) const {
433+
return lt_dlsym(handle, symbol.c_str());
433434
}
434435

435436
~library() {
@@ -468,7 +469,7 @@ class library {
468469
return std::nullopt;
469470
}
470471

471-
func_ptr get_address(const std::string & symbol) const {
472+
void * get_address(const std::string & symbol) const {
472473
MPT_UNUSED(symbol);
473474
return nullptr;
474475
}
@@ -486,9 +487,13 @@ class library {
486487
// Issue is similar to <https://connect.microsoft.com/VisualStudio/feedback/details/774720/stl-is-function-bug>.
487488
static_assert(std::is_function<Tfunc>::value);
488489
#endif
489-
const func_ptr addr = get_address(symbol);
490-
f = reinterpret_cast<Tfunc *>(addr);
491-
return (addr != nullptr);
490+
auto sym_ptr = get_address(symbol);
491+
if constexpr (std::is_same<decltype(sym_ptr), void>::value) {
492+
f = reinterpret_cast<Tfunc *>(sym_ptr);
493+
} else {
494+
f = mpt::function_pointer_cast<Tfunc *>(sym_ptr);
495+
}
496+
return (sym_ptr != nullptr);
492497
}
493498
};
494499

0 commit comments

Comments
 (0)