cppcoreguidelines-pro-type-union-access
According to https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md using union for type-punning is undefined behaviour. Replace it with std::memcpy
This commit is contained in:
@@ -47,6 +47,7 @@
|
||||
#endif
|
||||
#define slots
|
||||
#include <bitset>
|
||||
#include <cstring>
|
||||
|
||||
#include "Exception.h"
|
||||
#ifndef PYCXX_PYTHON_2TO3
|
||||
@@ -90,15 +91,6 @@
|
||||
#define PyMOD_INIT_FUNC(name) PyMODINIT_FUNC PyInit_##name(void)
|
||||
#define PyMOD_Return(name) return name
|
||||
|
||||
/**
|
||||
* Union to convert from PyTypeObject to PyObject pointer.
|
||||
*/
|
||||
union PyType_Object {
|
||||
PyTypeObject *t;
|
||||
PyObject *o;
|
||||
};
|
||||
|
||||
|
||||
|
||||
/*------------------------------
|
||||
* Basic defines
|
||||
@@ -122,6 +114,14 @@ inline void Assert(int expr, char *msg) // C++ assert
|
||||
};
|
||||
}
|
||||
|
||||
inline PyObject* getTypeAsObject(PyTypeObject* type) {
|
||||
// See https://en.cppreference.com/w/cpp/string/byte/memcpy
|
||||
// and https://en.cppreference.com/w/cpp/language/reinterpret_cast
|
||||
PyObject* obj;
|
||||
std::memcpy(&obj, &type, sizeof type);
|
||||
return obj;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*------------------------------
|
||||
|
||||
Reference in New Issue
Block a user