Skip to content

Commit 6fa08b0

Browse files
committed
add a helper to validate a final heap type
1 parent 0d9d489 commit 6fa08b0

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

Modules/hashlib.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
11
/* Common code for use by all hashlib related modules. */
22

3-
#include "pycore_lock.h" // PyMutex
3+
#include "pycore_lock.h" // PyMutex
4+
#include "pycore_moduleobject.h" // _PyModule_GetDef()
5+
6+
#ifndef NDEBUG
7+
/*
8+
* Assert that a type cannot be subclassed and that
9+
* its associated module definition matches 'moddef'.
10+
*
11+
* Use this helper to ensure that _PyType_GetModuleState() can be safely used.
12+
*/
13+
static inline void
14+
_Py_hashlib_check_exported_type(PyTypeObject *type, PyModuleDef *moddef)
15+
{
16+
assert(type != NULL);
17+
assert(moddef != NULL);
18+
/* ensure that the type is a final heap type */
19+
assert(PyType_Check(type));
20+
assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
21+
assert(!(type->tp_flags & Py_TPFLAGS_BASETYPE));
22+
/* ensure that the associated module definition matches 'moddef' */
23+
PyHeapTypeObject *ht = (PyHeapTypeObject *)type;
24+
assert(ht->ht_module != NULL);
25+
assert(moddef == _PyModule_GetDef(ht->ht_module));
26+
}
27+
#else
28+
#define _Py_hashlib_check_exported_type(_TYPE, _MODDEF)
29+
#endif
430

531
/*
632
* Given a PyObject* obj, fill in the Py_buffer* viewp with the result

0 commit comments

Comments
 (0)