Skip to content

Commit 25dbe38

Browse files
committed
fix UBSan failures for PyDateTime_Time
1 parent 76d2c68 commit 25dbe38

File tree

1 file changed

+56
-39
lines changed

1 file changed

+56
-39
lines changed

Modules/_datetimemodule.c

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4509,50 +4509,56 @@ look_up_timezone(PyObject *offset, PyObject *name)
45094509
*/
45104510

45114511
static PyObject *
4512-
time_hour(PyDateTime_Time *self, void *unused)
4512+
time_hour(PyObject *op, void *Py_UNUSED(closure))
45134513
{
4514+
PyDateTime_Time *self = _PyTime_CAST(op);
45144515
return PyLong_FromLong(TIME_GET_HOUR(self));
45154516
}
45164517

45174518
static PyObject *
4518-
time_minute(PyDateTime_Time *self, void *unused)
4519+
time_minute(PyObject *op, void *Py_UNUSED(closure))
45194520
{
4521+
PyDateTime_Time *self = _PyTime_CAST(op);
45204522
return PyLong_FromLong(TIME_GET_MINUTE(self));
45214523
}
45224524

45234525
/* The name time_second conflicted with some platform header file. */
45244526
static PyObject *
4525-
py_time_second(PyDateTime_Time *self, void *unused)
4527+
py_time_second(PyObject *op, void *Py_UNUSED(closure))
45264528
{
4529+
PyDateTime_Time *self = _PyTime_CAST(op);
45274530
return PyLong_FromLong(TIME_GET_SECOND(self));
45284531
}
45294532

45304533
static PyObject *
4531-
time_microsecond(PyDateTime_Time *self, void *unused)
4534+
time_microsecond(PyObject *op, void *Py_UNUSED(closure))
45324535
{
4536+
PyDateTime_Time *self = _PyTime_CAST(op);
45334537
return PyLong_FromLong(TIME_GET_MICROSECOND(self));
45344538
}
45354539

45364540
static PyObject *
4537-
time_tzinfo(PyDateTime_Time *self, void *unused)
4541+
time_tzinfo(PyObject *op, void *Py_UNUSED(closure))
45384542
{
4543+
PyDateTime_Time *self = _PyTime_CAST(op);
45394544
PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None;
45404545
return Py_NewRef(result);
45414546
}
45424547

45434548
static PyObject *
4544-
time_fold(PyDateTime_Time *self, void *unused)
4549+
time_fold(PyObject *op, void *Py_UNUSED(closure))
45454550
{
4551+
PyDateTime_Time *self = _PyTime_CAST(op);
45464552
return PyLong_FromLong(TIME_GET_FOLD(self));
45474553
}
45484554

45494555
static PyGetSetDef time_getset[] = {
4550-
{"hour", (getter)time_hour},
4551-
{"minute", (getter)time_minute},
4552-
{"second", (getter)py_time_second},
4553-
{"microsecond", (getter)time_microsecond},
4554-
{"tzinfo", (getter)time_tzinfo},
4555-
{"fold", (getter)time_fold},
4556+
{"hour", time_hour},
4557+
{"minute", time_minute},
4558+
{"second", py_time_second},
4559+
{"microsecond", time_microsecond},
4560+
{"tzinfo", time_tzinfo},
4561+
{"fold", time_fold},
45564562
{NULL}
45574563
};
45584564

@@ -4677,12 +4683,13 @@ time_strptime(PyObject *cls, PyObject *args)
46774683
*/
46784684

46794685
static void
4680-
time_dealloc(PyDateTime_Time *self)
4686+
time_dealloc(PyObject *op)
46814687
{
4688+
PyDateTime_Time *self = _PyTime_CAST(op);
46824689
if (HASTZINFO(self)) {
46834690
Py_XDECREF(self->tzinfo);
46844691
}
4685-
Py_TYPE(self)->tp_free((PyObject *)self);
4692+
Py_TYPE(self)->tp_free(self);
46864693
}
46874694

46884695
/*
@@ -4691,17 +4698,20 @@ time_dealloc(PyDateTime_Time *self)
46914698

46924699
/* These are all METH_NOARGS, so don't need to check the arglist. */
46934700
static PyObject *
4694-
time_utcoffset(PyObject *self, PyObject *unused) {
4701+
time_utcoffset(PyObject *op, PyObject *Py_UNUSED(unused)) {
4702+
PyDateTime_Time *self = _PyTime_CAST(op);
46954703
return call_utcoffset(GET_TIME_TZINFO(self), Py_None);
46964704
}
46974705

46984706
static PyObject *
4699-
time_dst(PyObject *self, PyObject *unused) {
4707+
time_dst(PyObject *op, PyObject *Py_UNUSED(unused)) {
4708+
PyDateTime_Time *self = _PyTime_CAST(op);
47004709
return call_dst(GET_TIME_TZINFO(self), Py_None);
47014710
}
47024711

47034712
static PyObject *
4704-
time_tzname(PyDateTime_Time *self, PyObject *unused) {
4713+
time_tzname(PyObject *op, PyObject *Py_UNUSED(unused)) {
4714+
PyDateTime_Time *self = _PyTime_CAST(op);
47054715
return call_tzname(GET_TIME_TZINFO(self), Py_None);
47064716
}
47074717

@@ -4710,8 +4720,9 @@ time_tzname(PyDateTime_Time *self, PyObject *unused) {
47104720
*/
47114721

47124722
static PyObject *
4713-
time_repr(PyDateTime_Time *self)
4723+
time_repr(PyObject *op)
47144724
{
4725+
PyDateTime_Time *self = _PyTime_CAST(op);
47154726
const char *type_name = Py_TYPE(self)->tp_name;
47164727
int h = TIME_GET_HOUR(self);
47174728
int m = TIME_GET_MINUTE(self);
@@ -4736,17 +4747,19 @@ time_repr(PyDateTime_Time *self)
47364747
}
47374748

47384749
static PyObject *
4739-
time_str(PyDateTime_Time *self)
4750+
time_str(PyObject *op)
47404751
{
4741-
return PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(isoformat));
4752+
return PyObject_CallMethodNoArgs(op, &_Py_ID(isoformat));
47424753
}
47434754

47444755
static PyObject *
4745-
time_isoformat(PyDateTime_Time *self, PyObject *args, PyObject *kw)
4756+
time_isoformat(PyObject *op, PyObject *args, PyObject *kw)
47464757
{
47474758
char buf[100];
47484759
const char *timespec = NULL;
47494760
static char *keywords[] = {"timespec", NULL};
4761+
PyDateTime_Time *self = _PyTime_CAST(op);
4762+
47504763
PyObject *result;
47514764
int us = TIME_GET_MICROSECOND(self);
47524765
static const char *specs[][2] = {
@@ -4807,12 +4820,13 @@ time_isoformat(PyDateTime_Time *self, PyObject *args, PyObject *kw)
48074820
}
48084821

48094822
static PyObject *
4810-
time_strftime(PyDateTime_Time *self, PyObject *args, PyObject *kw)
4823+
time_strftime(PyObject *op, PyObject *args, PyObject *kw)
48114824
{
48124825
PyObject *result;
48134826
PyObject *tuple;
48144827
PyObject *format;
48154828
static char *keywords[] = {"format", NULL};
4829+
PyDateTime_Time *self = _PyTime_CAST(op);
48164830

48174831
if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords,
48184832
&format))
@@ -4913,8 +4927,9 @@ time_richcompare(PyObject *self, PyObject *other, int op)
49134927
}
49144928

49154929
static Py_hash_t
4916-
time_hash(PyDateTime_Time *self)
4930+
time_hash(PyObject *op)
49174931
{
4932+
PyDateTime_Time *self = _PyTime_CAST(op);
49184933
if (self->hashcode == -1) {
49194934
PyObject *offset, *self0;
49204935
if (TIME_GET_FOLD(self)) {
@@ -5090,67 +5105,69 @@ time_getstate(PyDateTime_Time *self, int proto)
50905105
}
50915106

50925107
static PyObject *
5093-
time_reduce_ex(PyDateTime_Time *self, PyObject *args)
5108+
time_reduce_ex(PyObject *op, PyObject *args)
50945109
{
50955110
int proto;
50965111
if (!PyArg_ParseTuple(args, "i:__reduce_ex__", &proto))
50975112
return NULL;
50985113

5114+
PyDateTime_Time *self = _PyTime_CAST(op);
50995115
return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self, proto));
51005116
}
51015117

51025118
static PyObject *
5103-
time_reduce(PyDateTime_Time *self, PyObject *arg)
5119+
time_reduce(PyObject *op, PyObject *Py_UNUSED(arg))
51045120
{
5121+
PyDateTime_Time *self = _PyTime_CAST(op);
51055122
return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self, 2));
51065123
}
51075124

51085125
static PyMethodDef time_methods[] = {
51095126

51105127
/* Class method: */
51115128

5112-
{"strptime", (PyCFunction)time_strptime,
5129+
{"strptime", time_strptime,
51135130
METH_VARARGS | METH_CLASS,
51145131
PyDoc_STR("string, format -> new time parsed from a string "
51155132
"(like time.strptime()).")},
51165133

51175134
/* Instance methods: */
51185135

5119-
{"isoformat", _PyCFunction_CAST(time_isoformat), METH_VARARGS | METH_KEYWORDS,
5136+
{"isoformat", _PyCFunction_CAST(time_isoformat), METH_VARARGS | METH_KEYWORDS,
51205137
PyDoc_STR("Return string in ISO 8601 format, [HH[:MM[:SS[.mmm[uuu]]]]]"
51215138
"[+HH:MM].\n\n"
51225139
"The optional argument timespec specifies the number "
51235140
"of additional terms\nof the time to include. Valid "
51245141
"options are 'auto', 'hours', 'minutes',\n'seconds', "
51255142
"'milliseconds' and 'microseconds'.\n")},
51265143

5127-
{"strftime", _PyCFunction_CAST(time_strftime), METH_VARARGS | METH_KEYWORDS,
5144+
{"strftime", _PyCFunction_CAST(time_strftime), METH_VARARGS | METH_KEYWORDS,
51285145
PyDoc_STR("format -> strftime() style string.")},
51295146

5130-
{"__format__", (PyCFunction)date_format, METH_VARARGS,
5147+
{"__format__", date_format, METH_VARARGS,
51315148
PyDoc_STR("Formats self with strftime.")},
51325149

5133-
{"utcoffset", (PyCFunction)time_utcoffset, METH_NOARGS,
5150+
{"utcoffset", time_utcoffset, METH_NOARGS,
51345151
PyDoc_STR("Return self.tzinfo.utcoffset(self).")},
51355152

5136-
{"tzname", (PyCFunction)time_tzname, METH_NOARGS,
5153+
{"tzname", time_tzname, METH_NOARGS,
51375154
PyDoc_STR("Return self.tzinfo.tzname(self).")},
51385155

5139-
{"dst", (PyCFunction)time_dst, METH_NOARGS,
5156+
{"dst", time_dst, METH_NOARGS,
51405157
PyDoc_STR("Return self.tzinfo.dst(self).")},
51415158

51425159
DATETIME_TIME_REPLACE_METHODDEF
51435160

51445161
{"__replace__", _PyCFunction_CAST(datetime_time_replace), METH_FASTCALL | METH_KEYWORDS,
51455162
PyDoc_STR("__replace__($self, /, **changes)\n--\n\nThe same as replace().")},
51465163

5147-
{"fromisoformat", (PyCFunction)time_fromisoformat, METH_O | METH_CLASS,
5164+
{"fromisoformat", time_fromisoformat, METH_O | METH_CLASS,
51485165
PyDoc_STR("string -> time from a string in ISO 8601 format")},
51495166

5150-
{"__reduce_ex__", (PyCFunction)time_reduce_ex, METH_VARARGS,
5167+
{"__reduce_ex__", time_reduce_ex, METH_VARARGS,
51515168
PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")},
51525169

5153-
{"__reduce__", (PyCFunction)time_reduce, METH_NOARGS,
5170+
{"__reduce__", time_reduce, METH_NOARGS,
51545171
PyDoc_STR("__reduce__() -> (cls, state)")},
51555172

51565173
{NULL, NULL}
@@ -5167,18 +5184,18 @@ static PyTypeObject PyDateTime_TimeType = {
51675184
"datetime.time", /* tp_name */
51685185
sizeof(PyDateTime_Time), /* tp_basicsize */
51695186
0, /* tp_itemsize */
5170-
(destructor)time_dealloc, /* tp_dealloc */
5187+
time_dealloc, /* tp_dealloc */
51715188
0, /* tp_vectorcall_offset */
51725189
0, /* tp_getattr */
51735190
0, /* tp_setattr */
51745191
0, /* tp_as_async */
5175-
(reprfunc)time_repr, /* tp_repr */
5192+
time_repr, /* tp_repr */
51765193
0, /* tp_as_number */
51775194
0, /* tp_as_sequence */
51785195
0, /* tp_as_mapping */
5179-
(hashfunc)time_hash, /* tp_hash */
5196+
time_hash, /* tp_hash */
51805197
0, /* tp_call */
5181-
(reprfunc)time_str, /* tp_str */
5198+
time_str, /* tp_str */
51825199
PyObject_GenericGetAttr, /* tp_getattro */
51835200
0, /* tp_setattro */
51845201
0, /* tp_as_buffer */

0 commit comments

Comments
 (0)