10000 gh-111178: fix UBSan failures in `Modules/_interp*module.c` by picnixz · Pull Request #129779 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-111178: fix UBSan failures in Modules/_interp*module.c #129779

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 10 commits into from
Feb 17, 2025
Merged
Next Next commit
remove redundant casts in Modules/_interpchannelsmodule.c
  • Loading branch information
picnixz committed Jan 25, 2025
commit be385ff48137e09b46b6ffb819752f4e8c62b73f
55 changes: 29 additions & 26 deletions Modules/_interpchannelsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2268,6 +2268,8 @@ typedef struct channelid {
_channels *channels;
} channelid;

#define _channelid_CAST(op) ((channelid *)(op))

struct channel_id_converter_data {
PyObject *module;
int64_t cid;
Expand All @@ -2283,8 +2285,8 @@ channel_id_converter(PyObject *arg, void *ptr)
module_state *state = get_module_state(data->module);
assert(state != NULL);
if (PyObject_TypeCheck(arg, state->ChannelIDType)) {
cid = ((channelid *)arg)->cid;
end = ((channelid *)arg)->end;
cid = _channelid_CAST(arg)->cid;
end = _channelid_CAST(arg)->end;
}
else if (PyIndex_Check(arg)) {
cid = PyLong_AsLongLong(arg);
Expand Down Expand Up @@ -2398,8 +2400,8 @@ _channelid_new(PyObject *mod, PyTypeObject *cls,
static void
channelid_dealloc(PyObject *self)
{
int64_t cid = ((channelid *)self)->cid;
_channels *channels = ((channelid *)self)->channels;
int64_t cid = _channelid_CAST(self)->cid;
_channels *channels = _channelid_CAST(self)->channels;

PyTypeObject *tp = Py_TYPE(self);
tp->tp_free(self);
Expand All @@ -2420,7 +2422,7 @@ channelid_repr(PyObject *self)
PyTypeObject *type = Py_TYPE(self);
const char *name = _PyType_Name(type);

channelid *cidobj = (channelid *)self;
channelid *cidobj = _channelid_CAST(self);
const char *fmt;
if (cidobj->end == CHANNEL_SEND) {
fmt = "%s(%" PRId64 ", send=True)";
Expand All @@ -2437,21 +2439,21 @@ channelid_repr(PyObject *self)
static PyObject *
channelid_str(PyObject *self)
{
channelid *cidobj = (channelid *)self;
channelid *cidobj = _channelid_CAST(self);
return PyUnicode_FromFormat("%" PRId64 "", cidobj->cid);
}

static PyObject *
channelid_int(PyObject *self)
{
channelid *cidobj = (channelid *)self;
channelid *cidobj = _channelid_CAST(self);
return PyLong_FromLongLong(cidobj->cid);
}

static Py_hash_t
channelid_hash(PyObject *self)
{
channelid *cidobj = (channelid *)self;
channelid *cidobj = _channelid_CAST(self);
PyObject *pyid = PyLong_FromLongLong(cidobj->cid);
if (pyid == NULL) {
return -1;
Expand Down Expand Up @@ -2483,10 +2485,10 @@ channelid_richcompare(PyObject *self, PyObject *other, int op)
goto done;
}

channelid *cidobj = (channelid *)self;
channelid *cidobj = _channelid_CAST(self);
int equal;
if (PyObject_TypeCheck(other, state->ChannelIDType)) {
channelid *othercidobj = (channelid *)other;
channelid *othercidobj = (channelid *)other; // fast safe cast
equal = (cidobj->end == othercidobj->end) && (cidobj->cid == othercidobj->cid);
}
else if (PyLong_Check(other)) {
Expand Down Expand Up @@ -2606,17 +2608,18 @@ _channelid_shared(PyThreadState *tstate, PyObject *obj, _PyXIData_t *data)
return -1;
}
struct _channelid_xid *xid = (struct _channelid_xid *)_PyXIData_DATA(data);
xid->cid = ((channelid *)obj)->cid;
xid->end = ((channelid *)obj)->end;
xid->resolve = ((channelid *)obj)->resolve;
channelid *cidobj = _channelid_CAST(obj);
xid->cid = cidobj->cid;
xid->end = cidobj->end;
xid->resolve = cidobj->resolve;
return 0;
}

static PyObject *
channelid_end(PyObject *self, void *end)
{
int force = 1;
channelid *cidobj = (channelid *)self;
channelid *cidobj = _channelid_CAST(self);
if (end != NULL) {
PyObject *obj = NULL;
int err = newchannelid(Py_TYPE(self), cidobj->cid, *(int *)end,
Expand Down Expand Up @@ -2649,11 +2652,11 @@ static int _channelid_end_send = CHANNEL_SEND;
static int _channelid_end_recv = CHANNEL_RECV;

static PyGetSetDef channelid_getsets[] = {
{"end", (getter)channelid_end, NULL,
{"end", channelid_end, NULL,
PyDoc_STR("'send', 'recv', or 'both'")},
{"send", (getter)channelid_end, NULL,
{"send", channelid_end, NULL,
PyDoc_STR("the 'send' end of the channel"), &_channelid_end_send},
{"recv", (getter)channelid_end, NULL,
{"recv", channelid_end, NULL,
PyDoc_STR("the 'recv' end of the channel"), &_channelid_end_recv},
{NULL}
};
Expand All @@ -2662,16 +2665,16 @@ PyDoc_STRVAR(channelid_doc,
"A channel ID identifies a channel and may be used as an int.");

static PyType_Slot channelid_typeslots[] = {
{Py_tp_dealloc, (destructor)channelid_dealloc},
{Py_tp_dealloc, channelid_dealloc},
{Py_tp_doc, (void *)channelid_doc},
{Py_tp_repr, (reprfunc)channelid_repr},
{Py_tp_str, (reprfunc)channelid_str},
{Py_tp_repr, channelid_repr},
{Py_tp_str, channelid_str},
{Py_tp_hash, channelid_hash},
{Py_tp_richcompare, channelid_richcompare},
{Py_tp_getset, channelid_getsets},
// number slots
{Py_nb_int, (unaryfunc)channelid_int},
{Py_nb_index, (unaryfunc)channelid_int},
{Py_nb_int, channelid_int},
{Py_nb_index, channelid_int},
{0, NULL},
};

Expand Down Expand Up @@ -2904,10 +2907,10 @@ channelsmod_create(PyObject *self, PyObject *args, PyObject *kwds)
if (state == NULL) {
return NULL;
}
PyObject *cidobj = NULL;
channelid *cidobj = NULL;
int err = newchannelid(state->ChannelIDType, cid, 0,
&_globals.channels, 0, 0,
(channelid **)&cidobj);
&cidobj);
if (handle_channel_error(err, self, cid)) {
assert(cidobj == NULL);
err = channel_destroy(&_globals.channels, cid);
Expand All @@ -2917,8 +2920,8 @@ channelsmod_create(PyObject *self, PyObject *args, PyObject *kwds)
return NULL;
}
assert(cidobj != NULL);
assert(((channelid *)cidobj)->channels != NULL);
return cidobj;
assert(cidobj->channels != NULL);
return (PyObject *)cidobj;
}

PyDoc_STRVAR(channelsmod_create_doc,
Expand Down
0