Skip to content
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
Prev Previous commit
Next Next commit
fix UBSan failures for PyDateTime_IsoCalendarDate
  • Loading branch information
picnixz committed Jan 26, 2025
commit 19bc7e1420c44378876837f50c8490ff3d1b4458
39 changes: 19 additions & 20 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3611,17 +3611,17 @@ typedef struct {
} PyDateTime_IsoCalendarDate;

static PyObject *
iso_calendar_date_repr(PyDateTime_IsoCalendarDate *self)
iso_calendar_date_repr(PyObject *self)
{
PyObject* year = PyTuple_GetItem((PyObject *)self, 0);
PyObject *year = PyTuple_GetItem(self, 0);
if (year == NULL) {
return NULL;
}
PyObject* week = PyTuple_GetItem((PyObject *)self, 1);
PyObject *week = PyTuple_GetItem(self, 1);
if (week == NULL) {
return NULL;
}
PyObject* weekday = PyTuple_GetItem((PyObject *)self, 2);
PyObject *weekday = PyTuple_GetItem(self, 2);
if (weekday == NULL) {
return NULL;
}
Expand All @@ -3634,7 +3634,7 @@ static PyObject *
iso_calendar_date_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
{
// Construct the tuple that this reduces to
PyObject * reduce_tuple = Py_BuildValue(
PyObject *reduce_tuple = Py_BuildValue(
"O((OOO))", &PyTuple_Type,
PyTuple_GET_ITEM(self, 0),
PyTuple_GET_ITEM(self, 1),
Expand All @@ -3645,61 +3645,60 @@ iso_calendar_date_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
}

static PyObject *
iso_calendar_date_year(PyDateTime_IsoCalendarDate *self, void *unused)
iso_calendar_date_year(PyObject *self, void *unused)
{
PyObject *year = PyTuple_GetItem((PyObject *)self, 0);
PyObject *year = PyTuple_GetItem(self, 0);
if (year == NULL) {
return NULL;
}
return Py_NewRef(year);
}

static PyObject *
iso_calendar_date_week(PyDateTime_IsoCalendarDate *self, void *unused)
iso_calendar_date_week(PyObject *self, void *unused)
{
PyObject *week = PyTuple_GetItem((PyObject *)self, 1);
PyObject *week = PyTuple_GetItem(self, 1);
if (week == NULL) {
return NULL;
}
return Py_NewRef(week);
}

static PyObject *
iso_calendar_date_weekday(PyDateTime_IsoCalendarDate *self, void *unused)
iso_calendar_date_weekday(PyObject *self, void *unused)
{
PyObject *weekday = PyTuple_GetItem((PyObject *)self, 2);
PyObject *weekday = PyTuple_GetItem(self, 2);
if (weekday == NULL) {
return NULL;
}
return Py_NewRef(weekday);
}

static PyGetSetDef iso_calendar_date_getset[] = {
{"year", (getter)iso_calendar_date_year},
{"week", (getter)iso_calendar_date_week},
{"weekday", (getter)iso_calendar_date_weekday},
{"year", iso_calendar_date_year},
{"week", iso_calendar_date_week},
{"weekday", iso_calendar_date_weekday},
{NULL}
};

static PyMethodDef iso_calendar_date_methods[] = {
{"__reduce__", (PyCFunction)iso_calendar_date_reduce, METH_NOARGS,
{"__reduce__", iso_calendar_date_reduce, METH_NOARGS,
PyDoc_STR("__reduce__() -> (cls, state)")},
{NULL, NULL},
};

static int
iso_calendar_date_traverse(PyDateTime_IsoCalendarDate *self, visitproc visit,
void *arg)
iso_calendar_date_traverse(PyObject *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
return PyTuple_Type.tp_traverse((PyObject *)self, visit, arg);
return PyTuple_Type.tp_traverse(self, visit, arg);
}

static void
iso_calendar_date_dealloc(PyDateTime_IsoCalendarDate *self)
iso_calendar_date_dealloc(PyObject *self)
{
PyTypeObject *tp = Py_TYPE(self);
PyTuple_Type.tp_dealloc((PyObject *)self); // delegate GC-untrack as well
PyTuple_Type.tp_dealloc(self); // delegate GC-untrack as well
Py_DECREF(tp);
}

Expand Down