8000 gh-111178: fix UBSan failures in `Modules/_dbmmodule.c` by picnixz · Pull Request #129775 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-111178: fix UBSan failures in Modules/_db 8000 mmodule.c #129775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 10, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix UBSan failures for dbmobject
  • Loading branch information
picnixz committed Jan 25, 2025
commit 77e8041f17c4d235f138f1538347c4907d06b785
28 changes: 18 additions & 10 deletions Modules/_dbmmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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);

Expand Down Expand Up @@ -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)) {
Expand All @@ -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,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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(args))
{
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[] = {
Expand Down
0