8000 gh-111178: fix UBSan failures in `Modules/_datetimemodule.c` by picnixz · Pull Request #129774 · python/cpython · GitHub
[go: up one dir, main page]

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
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
Next Next commit
add cast macros for date and time objects
  • Loading branch information
picnixz committed Jan 26, 2025
commit d4be207477317426a1e3ae04a10b1efc2499fe3d
16 changes: 15 additions & 1 deletion Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ typedef struct {
* the per-module objects stored in module state. The macros for the
* static objects don't need to be passed a state, but the consistency
* of doing so is more clear. We use a dedicated noop macro, NO_STATE,
* to make the special case obvious. */
* to make the special case obvious.
*
* The casting macros perform a simple fast pointer cast without
* checking the runtime type. In the future, we may decide whether
* to include that check and whether to provide a fast pointer cast
* macro for pointers known to be of correct time.
*/

#define NO_STATE NULL

Expand All @@ -70,23 +76,31 @@ typedef struct {
#define TIMEZONE_TYPE(st) &PyDateTime_TimeZoneType
#define ISOCALENDAR_DATE_TYPE(st) st->isocalendar_date_type

#define _PyDate_CAST(op) ((PyDateTime_Date *)(op))
#define PyDate_Check(op) PyObject_TypeCheck(op, DATE_TYPE(NO_STATE))
#define PyDate_CheckExact(op) Py_IS_TYPE(op, DATE_TYPE(NO_STATE))

#define _PyDateTime_CAST(op) ((PyDateTime_DateTime *)(op))
#define PyDateTime_Check(op) PyObject_TypeCheck(op, DATETIME_TYPE(NO_STATE))
#define PyDateTime_CheckExact(op) Py_IS_TYPE(op, DATETIME_TYPE(NO_STATE))

#define _PyTime_CAST(op) ((PyDateTime_Time *)(op))
#define PyTime_Check(op) PyObject_TypeCheck(op, TIME_TYPE(NO_STATE))
#define PyTime_CheckExact(op) Py_IS_TYPE(op, TIME_TYPE(NO_STATE))

#define _PyDelta_CAST(op) ((PyDateTime_Delta *)(op))
#define PyDelta_Check(op) PyObject_TypeCheck(op, DELTA_TYPE(NO_STATE))
#define PyDelta_CheckExact(op) Py_IS_TYPE(op, DELTA_TYPE(NO_STATE))

#define _PyTZInfo_CAST(op) ((PyDateTime_TZInfo *)(op))
#define PyTZInfo_Check(op) PyObject_TypeCheck(op, TZINFO_TYPE(NO_STATE))
#define PyTZInfo_CheckExact(op) Py_IS_TYPE(op, TZINFO_TYPE(NO_STATE))

#define _PyTimezone_CAST(op) ((PyDateTime_TimeZone *)(op))
#define PyTimezone_Check(op) PyObject_TypeCheck(op, TIMEZONE_TYPE(NO_STATE))

#define _PyIsoCalendarDate_CAST(op) ((PyDateTime_IsoCalendarDate *)(op))

#define CONST_US_PER_MS(st) st->us_per_ms
#define CONST_US_PER_SECOND(st) st->us_per_second
#define CONST_US_PER_MINUTE(st) st->us_per_minute
Expand Down
0