Skip to content

gh-111178: fix UBSan failures in Modules/_datetimemodule.c #129774

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 15 commits into from
Feb 19, 2025

Conversation

picnixz
Copy link
Member

@picnixz picnixz commented Feb 7, 2025

This PR fixes the UBSan failures and addresses some minor cosmetic changes. PEP-7 changes were not applied since they could scramble the diff but other semantic changes affecting the signature of touched functions may have been done.

@picnixz
Copy link
Member Author

picnixz commented Feb 7, 2025

Ah I'm sorry I put this one as ready for review but I learned that _ + capital letter was also UB (#128248 (comment)) so I'll fix them tomorrow.

@picnixz picnixz marked this pull request as draft February 7, 2025 18:31
@picnixz picnixz marked this pull request as ready for review February 8, 2025 08:58
Copy link
Member

@chris-eibl chris-eibl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just nitpicks most probably related to mechanical changes.

At some places, the casts would not be needed and the PR could be kept smaller.

Most probably you'd still want the renamings like

time_utcoffset(PyObject *op, PyObject *Py_UNUSED(dummy))

for consistency with the rest of the PR, even though there and at some other places no change would be needed at all.

Tried to locate them, hopefully no false positives - need a break now :)

So just happily ignore where not appropriate.

@@ -4665,17 +4697,20 @@ time_dealloc(PyDateTime_Time *self)

/* These are all METH_NOARGS, so don't need to check the arglist. */
static PyObject *
time_utcoffset(PyObject *self, PyObject *unused) {
time_utcoffset(PyObject *op, PyObject *Py_UNUSED(dummy)) {
PyDateTime_Time *self = PyTime_CAST(op);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before the PR, time_utcoffset already got a PyObject as first parameter and GET_TIME_TZINFO happily accepts it.
Maybe omit the PyTime_CAST to keep the diff smaller?

return call_utcoffset(GET_TIME_TZINFO(self), Py_None);
}

static PyObject *
time_dst(PyObject *self, PyObject *unused) {
time_dst(PyObject *op, PyObject *Py_UNUSED(dummy)) {
PyDateTime_Time *self = PyTime_CAST(op);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to above: maybe omit the PyTime_CAST to keep the diff smaller?

return call_dst(GET_TIME_TZINFO(self), Py_None);
}

static PyObject *
time_tzname(PyDateTime_Time *self, PyObject *unused) {
time_tzname(PyObject *op, PyObject *Py_UNUSED(dummy)) {
PyDateTime_Time *self = PyTime_CAST(op);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to above: maybe omit the PyTime_CAST to keep the diff smaller?

{
PyDateTime_DateTime *self = PyDateTime_CAST(op);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The DATE_GET_* macros would not need the PyDateTime_CAST.

{
PyDateTime_DateTime *self = PyDateTime_CAST(op);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The DATE_GET_* macros would not need the PyDateTime_CAST.

datetime_tzname(PyObject *self, PyObject *unused) {
return call_tzname(GET_DT_TZINFO(self), self);
datetime_tzname(PyObject *op, PyObject *Py_UNUSED(dummy)) {
PyDateTime_DateTime *self = PyDateTime_CAST(op);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

{
PyDateTime_DateTime *self = PyDateTime_CAST(op);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The DATE_GET_* macros would not need the PyDateTime_CAST.

{
return format_ctime((PyDateTime_Date *)self,
PyDateTime_DateTime *self = PyDateTime_CAST(op);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The DATE_GET_* macros would not need the PyDateTime_CAST.

{
PyDateTime_DateTime *self = PyDateTime_CAST(op);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The DATE_GET_* macros would not need the PyDateTime_CAST.

{
PyDateTime_DateTime *self = PyDateTime_CAST(op);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The DATE_GET_* macros would not need the PyDateTime_CAST.

@picnixz
Copy link
Member Author

picnixz commented Feb 9, 2025

The reason why I made the changes here is because the macros may be extended to static inline functions with runtime checks in the future and we definitely don't want to fill out missing pieces. For now, they don't do anything but ideally, the GET_ macros should be using fast casts (no checks) while _CAST macros will be using real checks.

@picnixz
Copy link
Member Author

picnixz commented Feb 9, 2025

Ah! I remember what I did. I wrote a note saying that a postmerge task should be done where I eliminate the unnecessary casts in the GET macros so that we can add a check in the macro instead of in the getters.

@encukou encukou enabled auto-merge (squash) February 19, 2025 10:56
@encukou encukou merged commit c637bce into python:main Feb 19, 2025
41 checks passed
@picnixz picnixz deleted the fix/ubsan/datetime-111178 branch February 21, 2025 12:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants