aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--qt/ScintillaEditBase/PlatQt.cpp16
-rw-r--r--win32/PlatWin.cxx13
2 files changed, 14 insertions, 15 deletions
diff --git a/qt/ScintillaEditBase/PlatQt.cpp b/qt/ScintillaEditBase/PlatQt.cpp
index 515052a48..3d285a337 100644
--- a/qt/ScintillaEditBase/PlatQt.cpp
+++ b/qt/ScintillaEditBase/PlatQt.cpp
@@ -1119,17 +1119,17 @@ public:
}
Function FindFunction(const char *name) override {
if (lib) {
- // C++ standard doesn't like casts between function pointers and void pointers so use a union
- union {
+ // Use memcpy as it doesn't invoke undefined or conditionally defined behaviour.
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
- QFunctionPointer fp;
+ QFunctionPointer fp {};
#else
- void *fp;
+ void *fp = nullptr;
#endif
- Function f;
- } fnConv;
- fnConv.fp = lib->resolve(name);
- return fnConv.f;
+ fp = lib->resolve(name);
+ Function f = nullptr;
+ static_assert(sizeof(f) == sizeof(fp));
+ memcpy(&f, &fp, sizeof(f));
+ return f;
}
return nullptr;
}
diff --git a/win32/PlatWin.cxx b/win32/PlatWin.cxx
index 73ac50271..4c9291fd1 100644
--- a/win32/PlatWin.cxx
+++ b/win32/PlatWin.cxx
@@ -3257,13 +3257,12 @@ public:
// Use GetProcAddress to get a pointer to the relevant function.
Function FindFunction(const char *name) noexcept override {
if (h) {
- // C++ standard doesn't like casts between function pointers and void pointers so use a union
- union {
- FARPROC fp;
- Function f;
- } fnConv;
- fnConv.fp = ::GetProcAddress(h, name);
- return fnConv.f;
+ // Use memcpy as it doesn't invoke undefined or conditionally defined behaviour.
+ FARPROC fp = ::GetProcAddress(h, name);
+ Function f = nullptr;
+ static_assert(sizeof(f) == sizeof(fp));
+ memcpy(&f, &fp, sizeof(f));
+ return f;
} else {
return nullptr;
}