https://github.com/python/cpython/commit/a1a6df282d732b54ba99de93416f8f9d85b3de2f
commit: a1a6df282d732b54ba99de93416f8f9d85b3de2f
branch: main
author: Bénédikt Tran <[email protected]>
committer: picnixz <[email protected]>
date: 2025-02-10T11:02:47+01:00
summary:
gh-111178: fix UBSan failures in `Modules/_dbmmodule.c` (#129775)
This fixes UBSan failures for `dbmobject`.
In addition, we perform some minor cleanup changes such as renaming
some `args` parameter to `dummy` in some `METH_NOARGS` methods and
suppressing an unused return value in `_dbm_module_free`.
files:
M Modules/_dbmmodule.c
diff --git a/Modules/_dbmmodule.c b/Modules/_dbmmodule.c
index 1be4234aad3291..cc65cbd98d71dc 100644
--- a/Modules/_dbmmodule.c
+++ b/Modules/_dbmmodule.c
@@ -64,6 +64,8 @@ typedef struct {
DBM *di_dbm;
} dbmobject;
+#define dbmobject_CAST(op) ((dbmobject *)(op))
+
#include "clinic/_dbmmodule.c.h"
#define check_dbmobject_open(v, err) \
@@ -94,15 +96,16 @@ newdbmobject(_dbm_state *state, const char *file, int
flags, int mode)
/* Methods */
static int
-dbm_traverse(dbmobject *dp, visitproc visit, void *arg)
+dbm_traverse(PyObject *dp, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(dp));
return 0;
}
static void
-dbm_dealloc(dbmobject *dp)
+dbm_dealloc(PyObject *self)
{
+ dbmobject *dp = dbmobject_CAST(self);
PyObject_GC_UnTrack(dp);
if (dp->di_dbm) {
dbm_close(dp->di_dbm);
@@ -113,8 +116,9 @@ dbm_dealloc(dbmobject *dp)
}
static Py_ssize_t
-dbm_length(dbmobject *dp)
+dbm_length(PyObject *self)
{
+ dbmobject *dp = dbmobject_CAST(self);
_dbm_state *state = PyType_GetModuleState(Py_TYPE(dp));
assert(state != NULL);
if (dp->di_dbm == NULL) {
@@ -135,8 +139,9 @@ dbm_length(dbmobject *dp)
}
static int
-dbm_bool(dbmobject *dp)
+dbm_bool(PyObject *self)
{
+ dbmobject *dp = dbmobject_CAST(self);
_dbm_state *state = PyType_GetModuleState(Py_TYPE(dp));
assert(state != NULL);
@@ -166,10 +171,11 @@ dbm_bool(dbmobject *dp)
}
static PyObject *
-dbm_subscript(dbmobject *dp, PyObject *key)
+dbm_subscript(PyObject *self, PyObject *key)
{
datum drec, krec;
Py_ssize_t tmp_size;
+ dbmobject *dp = dbmobject_CAST(self);
_dbm_state *state = PyType_GetModuleState(Py_TYPE(dp));
assert(state != NULL);
if (!PyArg_Parse(key, "s#", &krec.dptr, &tmp_size)) {
@@ -192,10 +198,11 @@ dbm_subscript(dbmobject *dp, PyObject *key)
}
static int
-dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w)
+dbm_ass_sub(PyObject *self, PyObject *v, PyObject *w)
{
datum krec, drec;
Py_ssize_t tmp_size;
+ dbmobject *dp = dbmobject_CAST(self);
if ( !PyArg_Parse(v, "s#", &krec.dptr, &tmp_size) ) {
PyErr_SetString(PyExc_TypeError,
@@ -305,7 +312,7 @@ _dbm_dbm_keys_impl(dbmobject *self, PyTypeObject *cls)
static int
dbm_contains(PyObject *self, PyObject *arg)
{
- dbmobject *dp = (dbmobject *)self;
+ dbmobject *dp = dbmobject_CAST(self);
datum key, val;
Py_ssize_t size;
@@ -452,15 +459,16 @@ _dbm_dbm_clear_impl(dbmobject *self, PyTypeObject *cls)
}
static PyObject *
-dbm__enter__(PyObject *self, PyObject *args)
+dbm__enter__(PyObject *self, PyObject *Py_UNUSED(dummy))
{
return Py_NewRef(self);
}
static PyObject *
-dbm__exit__(PyObject *self, PyObject *args)
+dbm__exit__(PyObject *self, PyObject *Py_UNUSED(args))
{
- return _dbm_dbm_close_impl((dbmobject *)self);
+ dbmobject *dp = dbmobject_CAST(self);
+ return _dbm_dbm_close_impl(dp);
}
static PyMethodDef dbm_methods[] = {
@@ -610,7 +618,7 @@ _dbm_module_clear(PyObject *module)
static void
_dbm_module_free(void *module)
{
- _dbm_module_clear((PyObject *)module);
+ (void)_dbm_module_clear((PyObject *)module);
}
static PyModuleDef_Slot _dbmmodule_slots[] = {
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]