Skip to content

Commit 1aeeff8

Browse files
committed
fix UBSan failures for PyDateTime_Date
1 parent 6d1f263 commit 1aeeff8

File tree

1 file changed

+53
-52
lines changed

1 file changed

+53
-52
lines changed

Modules/_datetimemodule.c

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,7 +1676,7 @@ tzinfo_from_isoformat_results(int rv, int tzoffset, int tz_useconds)
16761676
*/
16771677

16781678
static PyObject *
1679-
format_ctime(PyDateTime_Date *date, int hours, int minutes, int seconds)
1679+
format_ctime(PyObject *date, int hours, int minutes, int seconds)
16801680
{
16811681
static const char * const DayNames[] = {
16821682
"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
@@ -3140,27 +3140,30 @@ look_up_delta(int days, int seconds, int microseconds, PyTypeObject *type)
31403140
/* Accessor properties. */
31413141

31423142
static PyObject *
3143-
date_year(PyDateTime_Date *self, void *unused)
3143+
date_year(PyObject *op, void *Py_UNUSED(closure))
31443144
{
3145+
PyDateTime_Date *self = _PyDate_CAST(op);
31453146
return PyLong_FromLong(GET_YEAR(self));
31463147
}
31473148

31483149
static PyObject *
3149-
date_month(PyDateTime_Date *self, void *unused)
3150+
date_month(PyObject *op, void *Py_UNUSED(closure))
31503151
{
3152+
PyDateTime_Date *self = _PyDate_CAST(op);
31513153
return PyLong_FromLong(GET_MONTH(self));
31523154
}
31533155

31543156
static PyObject *
3155-
date_day(PyDateTime_Date *self, void *unused)
3157+
date_day(PyObject *op, void *Py_UNUSED(closure))
31563158
{
3159+
PyDateTime_Date *self = _PyDate_CAST(op);
31573160
return PyLong_FromLong(GET_DAY(self));
31583161
}
31593162

31603163
static PyGetSetDef date_getset[] = {
3161-
{"year", (getter)date_year},
3162-
{"month", (getter)date_month},
3163-
{"day", (getter)date_day},
3164+
{"year", date_year},
3165+
{"month", date_month},
3166+
{"day", date_day},
31643167
{NULL}
31653168
};
31663169

@@ -3519,36 +3522,38 @@ date_subtract(PyObject *left, PyObject *right)
35193522
/* Various ways to turn a date into a string. */
35203523

35213524
static PyObject *
3522-
date_repr(PyDateTime_Date *self)
3525+
date_repr(PyObject *op)
35233526
{
3527+
PyDateTime_Date *self = _PyDate_CAST(op);
35243528
return PyUnicode_FromFormat("%s(%d, %d, %d)",
35253529
Py_TYPE(self)->tp_name,
35263530
GET_YEAR(self), GET_MONTH(self), GET_DAY(self));
35273531
}
35283532

35293533
static PyObject *
3530-
date_isoformat(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored))
3534+
date_isoformat(PyObject *op, PyObject *Py_UNUSED(ignored))
35313535
{
3536+
PyDateTime_Date *self = _PyDate_CAST(op);
35323537
return PyUnicode_FromFormat("%04d-%02d-%02d",
35333538
GET_YEAR(self), GET_MONTH(self), GET_DAY(self));
35343539
}
35353540

35363541
/* str() calls the appropriate isoformat() method. */
35373542
static PyObject *
3538-
date_str(PyDateTime_Date *self)
3543+
date_str(PyObject *self)
35393544
{
3540-
return PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(isoformat));
3545+
return PyObject_CallMethodNoArgs(self, &_Py_ID(isoformat));
35413546
}
35423547

35433548

35443549
static PyObject *
3545-
date_ctime(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored))
3550+
date_ctime(PyObject *self, PyObject *Py_UNUSED(ignored))
35463551
{
35473552
return format_ctime(self, 0, 0, 0);
35483553
}
35493554

35503555
static PyObject *
3551-
date_strftime(PyDateTime_Date *self, PyObject *args, PyObject *kw)
3556+
date_strftime(PyObject *self, PyObject *args, PyObject *kw)
35523557
{
35533558
/* This method can be inherited, and needs to call the
35543559
* timetuple() method appropriate to self's class.
@@ -3558,21 +3563,20 @@ date_strftime(PyDateTime_Date *self, PyObject *args, PyObject *kw)
35583563
PyObject *format;
35593564
static char *keywords[] = {"format", NULL};
35603565

3561-
if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords,
3562-
&format))
3566+
if (!PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords,
3567+
&format))
35633568
return NULL;
35643569

3565-
tuple = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(timetuple));
3570+
tuple = PyObject_CallMethodNoArgs(self, &_Py_ID(timetuple));
35663571
if (tuple == NULL)
35673572
return NULL;
3568-
result = wrap_strftime((PyObject *)self, format, tuple,
3569-
(PyObject *)self);
3573+
result = wrap_strftime(self, format, tuple, self);
35703574
Py_DECREF(tuple);
35713575
return result;
35723576
}
35733577

35743578
static PyObject *
3575-
date_format(PyDateTime_Date *self, PyObject *args)
3579+
date_format(PyObject *self, PyObject *args)
35763580
{
35773581
PyObject *format;
35783582

@@ -3581,16 +3585,15 @@ date_format(PyDateTime_Date *self, PyObject *args)
35813585

35823586
/* if the format is zero length, return str(self) */
35833587
if (PyUnicode_GetLength(format) == 0)
3584-
return PyObject_Str((PyObject *)self);
3588+
return PyObject_Str(self);
35853589

3586-
return PyObject_CallMethodOneArg((PyObject *)self, &_Py_ID(strftime),
3587-
format);
3590+
return PyObject_CallMethodOneArg(self, &_Py_ID(strftime), format);
35883591
}
35893592

35903593
/* ISO methods. */
35913594

35923595
static PyObject *
3593-
date_isoweekday(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored))
3596+
date_isoweekday(PyObject *self, PyObject *Py_UNUSED(ignored))
35943597
{
35953598
int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self));
35963599

@@ -3748,7 +3751,7 @@ iso_calendar_date_new_impl(PyTypeObject *type, int year, int week,
37483751
}
37493752

37503753
static PyObject *
3751-
date_isocalendar(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored))
3754+
date_isocalendar(PyObject *self, PyObject *Py_UNUSED(ignored))
37523755
{
37533756
int year = GET_YEAR(self);
37543757
int week1_monday = iso_week1_monday(year);
@@ -3803,7 +3806,7 @@ date_richcompare(PyObject *self, PyObject *other, int op)
38033806
}
38043807

38053808
static PyObject *
3806-
date_timetuple(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored))
3809+
date_timetuple(PyObject *self, PyObject *Py_UNUSED(ignored))
38073810
{
38083811
return build_struct_time(GET_YEAR(self),
38093812
GET_MONTH(self),
@@ -3839,8 +3842,9 @@ generic_hash(unsigned char *data, int len)
38393842
static PyObject *date_getstate(PyDateTime_Date *self);
38403843

38413844
static Py_hash_t
3842-
date_hash(PyDateTime_Date *self)
3845+
date_hash(PyObject *op)
38433846
{
3847+
PyDateTime_Date *self = _PyDate_CAST(op);
38443848
if (self->hashcode == -1) {
38453849
self->hashcode = generic_hash(
38463850
(unsigned char *)self->data, _PyDateTime_DATE_DATASIZE);
@@ -3850,17 +3854,16 @@ date_hash(PyDateTime_Date *self)
38503854
}
38513855

38523856
static PyObject *
3853-
date_toordinal(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored))
3857+
date_toordinal(PyObject *self, PyObject *Py_UNUSED(ignored))
38543858
{
38553859
return PyLong_FromLong(ymd_to_ord(GET_YEAR(self), GET_MONTH(self),
3856-
GET_DAY(self)));
3860+
GET_DAY(self)));
38573861
}
38583862

38593863
static PyObject *
3860-
date_weekday(PyDateTime_Date *self, PyObject *Py_UNUSED(ignored))
3864+
date_weekday(PyObject *self, PyObject *Py_UNUSED(ignored))
38613865
{
38623866
int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self));
3863-
38643867
return PyLong_FromLong(dow);
38653868
}
38663869

@@ -3877,8 +3880,9 @@ date_getstate(PyDateTime_Date *self)
38773880
}
38783881

38793882
static PyObject *
3880-
date_reduce(PyDateTime_Date *self, PyObject *arg)
3883+
date_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
38813884
{
3885+
PyDateTime_Date *self = _PyDate_CAST(op);
38823886
return Py_BuildValue("(ON)", Py_TYPE(self), date_getstate(self));
38833887
}
38843888

@@ -3887,13 +3891,11 @@ static PyMethodDef date_methods[] = {
38873891
/* Class methods: */
38883892
DATETIME_DATE_FROMTIMESTAMP_METHODDEF
38893893

3890-
{"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS |
3891-
METH_CLASS,
3894+
{"fromordinal", date_fromordinal, METH_VARARGS | METH_CLASS,
38923895
PyDoc_STR("int -> date corresponding to a proleptic Gregorian "
38933896
"ordinal.")},
38943897

3895-
{"fromisoformat", (PyCFunction)date_fromisoformat, METH_O |
3896-
METH_CLASS,
3898+
{"fromisoformat", date_fromisoformat, METH_O | METH_CLASS,
38973899
PyDoc_STR("str -> Construct a date from a string in ISO 8601 format.")},
38983900

38993901
{"fromisocalendar", _PyCFunction_CAST(date_fromisocalendar),
@@ -3902,45 +3904,44 @@ static PyMethodDef date_methods[] = {
39023904
"number and weekday.\n\n"
39033905
"This is the inverse of the date.isocalendar() function")},
39043906

3905-
{"strptime", (PyCFunction)date_strptime,
3906-
METH_VARARGS | METH_CLASS,
3907+
{"strptime", date_strptime, METH_VARARGS | METH_CLASS,
39073908
PyDoc_STR("string, format -> new date parsed from a string "
39083909
"(like time.strptime()).")},
39093910

3910-
{"today", (PyCFunction)date_today, METH_NOARGS | METH_CLASS,
3911+
{"today", date_today, METH_NOARGS | METH_CLASS,
39113912
PyDoc_STR("Current date or datetime: same as "
39123913
"self.__class__.fromtimestamp(time.time()).")},
39133914

39143915
/* Instance methods: */
39153916

3916-
{"ctime", (PyCFunction)date_ctime, METH_NOARGS,
3917+
{"ctime", date_ctime, METH_NOARGS,
39173918
PyDoc_STR("Return ctime() style string.")},
39183919

3919-
{"strftime", _PyCFunction_CAST(date_strftime), METH_VARARGS | METH_KEYWORDS,
3920+
{"strftime", _PyCFunction_CAST(date_strftime), METH_VARARGS | METH_KEYWORDS,
39203921
PyDoc_STR("format -> strftime() style string.")},
39213922

3922-
{"__format__", (PyCFunction)date_format, METH_VARARGS,
3923+
{"__format__", date_format, METH_VARARGS,
39233924
PyDoc_STR("Formats self with strftime.")},
39243925

3925-
{"timetuple", (PyCFunction)date_timetuple, METH_NOARGS,
3926+
{"timetuple", date_timetuple, METH_NOARGS,
39263927
PyDoc_STR("Return time tuple, compatible with time.localtime().")},
39273928

3928-
{"isocalendar", (PyCFunction)date_isocalendar, METH_NOARGS,
3929+
{"isocalendar", date_isocalendar, METH_NOARGS,
39293930
PyDoc_STR("Return a named tuple containing ISO year, week number, and "
39303931
"weekday.")},
39313932

3932-
{"isoformat", (PyCFunction)date_isoformat, METH_NOARGS,
3933+
{"isoformat", date_isoformat, METH_NOARGS,
39333934
PyDoc_STR("Return string in ISO 8601 format, YYYY-MM-DD.")},
39343935

3935-
{"isoweekday", (PyCFunction)date_isoweekday, METH_NOARGS,
3936+
{"isoweekday", date_isoweekday, METH_NOARGS,
39363937
PyDoc_STR("Return the day of the week represented by the date.\n"
39373938
"Monday == 1 ... Sunday == 7")},
39383939

3939-
{"toordinal", (PyCFunction)date_toordinal, METH_NOARGS,
3940+
{"toordinal", date_toordinal, METH_NOARGS,
39403941
PyDoc_STR("Return proleptic Gregorian ordinal. January 1 of year "
39413942
"1 is day 1.")},
39423943

3943-
{"weekday", (PyCFunction)date_weekday, METH_NOARGS,
3944+
{"weekday", date_weekday, METH_NOARGS,
39443945
PyDoc_STR("Return the day of the week represented by the date.\n"
39453946
"Monday == 0 ... Sunday == 6")},
39463947

@@ -3949,7 +3950,7 @@ static PyMethodDef date_methods[] = {
39493950
{"__replace__", _PyCFunction_CAST(datetime_date_replace), METH_FASTCALL | METH_KEYWORDS,
39503951
PyDoc_STR("__replace__($self, /, **changes)\n--\n\nThe same as replace().")},
39513952

3952-
{"__reduce__", (PyCFunction)date_reduce, METH_NOARGS,
3953+
{"__reduce__", date_reduce, METH_NOARGS,
39533954
PyDoc_STR("__reduce__() -> (cls, state)")},
39543955

39553956
{NULL, NULL}
@@ -3981,13 +3982,13 @@ static PyTypeObject PyDateTime_DateType = {
39813982
0, /* tp_getattr */
39823983
0, /* tp_setattr */
39833984
0, /* tp_as_async */
3984-
(reprfunc)date_repr, /* tp_repr */
3985+
date_repr, /* tp_repr */
39853986
&date_as_number, /* tp_as_number */
39863987
0, /* tp_as_sequence */
39873988
0, /* tp_as_mapping */
3988-
(hashfunc)date_hash, /* tp_hash */
3989+
date_hash, /* tp_hash */
39893990
0, /* tp_call */
3990-
(reprfunc)date_str, /* tp_str */
3991+
date_str, /* tp_str */
39913992
PyObject_GenericGetAttr, /* tp_getattro */
39923993
0, /* tp_setattro */
39933994
0, /* tp_as_buffer */
@@ -6240,7 +6241,7 @@ datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
62406241
static PyObject *
62416242
datetime_ctime(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored))
62426243
{
6243-
return format_ctime((PyDateTime_Date *)self,
6244+
return format_ctime((PyObject *)self,
62446245
DATE_GET_HOUR(self),
62456246
DATE_GET_MINUTE(self),
62466247
DATE_GET_SECOND(self));

0 commit comments

Comments
 (0)