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

Skip to content

gh-111178: fix UBSan failures in Objects/exceptions.c #128154

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 25 commits into from
Feb 17, 2025
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
2683a25
fix UBSan failures for `PyBaseExceptionObject`
picnixz Dec 21, 2024
3f1196b
fix UBSan failures for `PyStopIterationObject`
picnixz Dec 21, 2024
1886b07
fix UBSan failures for `PySystemExitObject`
picnixz Dec 21, 2024
3df93a5
fix UBSan failures for `PyImportErrorObject`
picnixz Dec 21, 2024
96f97a5
fix UBSan failures for `PyOSErrorObject`
picnixz Dec 21, 2024
09514a9
fix UBSan failures for `PyNameErrorObject`
picnixz Dec 21, 2024
9d3c8b8
fix UBSan failures for `PyAttributeErrorObject`
picnixz Dec 21, 2024
2640a34
fix UBSan failures for `PySyntaxErrorObject`
picnixz Dec 21, 2024
150f34e
fix UBSan failures for `KeyError`
picnixz Dec 21, 2024
11d9e31
remove un-necessary casts for `UnicodeError*`
picnixz Dec 21, 2024
96430d9
remove un-necessary casts for `MemoryError`
picnixz Dec 21, 2024
0437291
fix UBSan failures for `PyBaseExceptionGroupObject`
picnixz Dec 21, 2024
d17a9b4
unify naming for cast functions
picnixz Dec 21, 2024
0d2434e
Merge remote-tracking branch 'upstream/main' into fix/ubsan/exception…
picnixz Jan 14, 2025
793196d
fixup
picnixz Jan 14, 2025
6abf1d8
fixup
picnixz Jan 14, 2025
6c2ef08
align naming convention
picnixz Jan 14, 2025
96f2d2d
remove redundant casts
picnixz Jan 16, 2025
0cd116b
Merge branch 'main' into fix/ubsan/exceptions-111178
picnixz Jan 18, 2025
248f195
Merge remote-tracking branch 'upstream/main' into fix/ubsan/exception…
picnixz Jan 25, 2025
e4daf11
remove un-necessary cast
picnixz Feb 6, 2025
565edab
Merge branch 'main' into fix/ubsan/exceptions-111178
picnixz Feb 7, 2025
122c914
Do not use `_` + capital letter in cast macros as it is also UB.
picnixz Feb 8, 2025
7906e3b
Do not use `_` + capital letter in cast macros as it is also UB.
picnixz Feb 8, 2025
8862b83
Merge branch 'main' into fix/ubsan/exceptions-111178
picnixz Feb 8, 2025
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 PyOSErrorObject
  • Loading branch information
picnixz committed Dec 21, 2024
commit 96f97a5a9a53bf493dd547bfd1366b5981f821f6
51 changes: 32 additions & 19 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1705,7 +1705,7 @@ ImportError_getstate(PyObject *op)

/* Pickling support */
static PyObject *
ImportError_reduce(PyObject *self, PyObject *Py_UNUSED(igno+red))
ImportError_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject *res;
PyObject *state = ImportError_getstate(self);
Expand Down Expand Up @@ -1755,6 +1755,13 @@ MiddlingExtendsException(PyExc_ImportError, ModuleNotFoundError, ImportError,
* OSError extends Exception
*/

static inline PyOSErrorObject *
_PyOSError_CAST(PyObject *self)
{
assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_OSError));
return (PyOSErrorObject *)self;
}

#ifdef MS_WINDOWS
#include "errmap.h"
#endif
Expand Down Expand Up @@ -1891,7 +1898,7 @@ oserror_init(PyOSErrorObject *self, PyObject **p_args,
static PyObject *
OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static int
OSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds);
OSError_init(PyObject *self, PyObject *args, PyObject *kwds);

static int
oserror_use_init(PyTypeObject *type)
Expand Down Expand Up @@ -1983,8 +1990,9 @@ OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}

static int
OSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds)
OSError_init(PyObject *op, PyObject *args, PyObject *kwds)
{
PyOSErrorObject *self = _PyOSError_CAST(op);
PyObject *myerrno = NULL, *strerror = NULL;
PyObject *filename = NULL, *filename2 = NULL;
#ifdef MS_WINDOWS
Expand Down Expand Up @@ -2021,43 +2029,45 @@ OSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds)
}

static int
OSError_clear(PyOSErrorObject *self)
OSError_clear(PyObject *op)
{
PyOSErrorObject *self = _PyOSError_CAST(op);
Py_CLEAR(self->myerrno);
Py_CLEAR(self->strerror);
Py_CLEAR(self->filename);
Py_CLEAR(self->filename2);
#ifdef MS_WINDOWS
Py_CLEAR(self->winerror);
#endif
return BaseException_clear((PyBaseExceptionObject *)self);
return BaseException_clear(op);
}

static void
OSError_dealloc(PyOSErrorObject *self)
OSError_dealloc(PyObject *self)
{
_PyObject_GC_UNTRACK(self);
OSError_clear(self);
Py_TYPE(self)->tp_free((PyObject *)self);
(void)OSError_clear(self);
Py_TYPE(self)->tp_free(self);
}

static int
OSError_traverse(PyOSErrorObject *self, visitproc visit,
void *arg)
OSError_traverse(PyObject *op, visitproc visit, void *arg)
{
PyOSErrorObject *self = _PyOSError_CAST(op);
Py_VISIT(self->myerrno);
Py_VISIT(self->strerror);
Py_VISIT(self->filename);
Py_VISIT(self->filename2);
#ifdef MS_WINDOWS
Py_VISIT(self->winerror);
#endif
return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
return BaseException_traverse(op, visit, arg);
}

static PyObject *
OSError_str(PyOSErrorObject *self)
OSError_str(PyObject *op)
{
PyOSErrorObject *self = _PyOSError_CAST(op);
#define OR_NONE(x) ((x)?(x):Py_None)
#ifdef MS_WINDOWS
/* If available, winerror has the priority over myerrno */
Expand Down Expand Up @@ -2097,12 +2107,13 @@ OSError_str(PyOSErrorObject *self)
if (self->myerrno && self->strerror)
return PyUnicode_FromFormat("[Errno %S] %S",
self->myerrno, self->strerror);
return BaseException_str((PyBaseExceptionObject *)self);
return BaseException_str(op);
}

static PyObject *
OSError_reduce(PyOSErrorObject *self, PyObject *Py_UNUSED(ignored))
OSError_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
{
PyOSErrorObject *self = _PyOSError_CAST(op);
PyObject *args = self->args;
PyObject *res = NULL;

Expand Down Expand Up @@ -2141,8 +2152,9 @@ OSError_reduce(PyOSErrorObject *self, PyObject *Py_UNUSED(ignored))
}

static PyObject *
OSError_written_get(PyOSErrorObject *self, void *context)
OSError_written_get(PyObject *op, void *context)
{
PyOSErrorObject *self = _PyOSError_CAST(op);
if (self->written == -1) {
PyErr_SetString(PyExc_AttributeError, "characters_written");
return NULL;
Expand All @@ -2151,8 +2163,9 @@ OSError_written_get(PyOSErrorObject *self, void *context)
}

static int
OSError_written_set(PyOSErrorObject *self, PyObject *arg, void *context)
OSError_written_set(PyObject *op, PyObject *arg, void *context)
{
PyOSErrorObject *self = _PyOSError_CAST(op);
if (arg == NULL) {
if (self->written == -1) {
PyErr_SetString(PyExc_AttributeError, "characters_written");
Expand Down Expand Up @@ -2186,13 +2199,13 @@ static PyMemberDef OSError_members[] = {
};

static PyMethodDef OSError_methods[] = {
{"__reduce__", (PyCFunction)OSError_reduce, METH_NOARGS},
{"__reduce__", OSError_reduce, METH_NOARGS},
{NULL}
};

static PyGetSetDef OSError_getset[] = {
{"characters_written", (getter) OSError_written_get,
(setter) OSError_written_set, NULL},
{"characters_written", OSError_written_get,
OSError_written_set, NULL},
{NULL}
};

Expand Down
0