8000 fix UBSan failures for `PyOSErrorObject` · python/cpython@96f97a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 96f97a5

Browse files
committed
fix UBSan failures for PyOSErrorObject
1 parent 3df93a5 commit 96f97a5

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

Objects/exceptions.c

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,7 @@ ImportError_getstate(PyObject *op)
17051705

17061706
/* Pickling support */
17071707
static PyObject *
1708-
ImportError_reduce(PyObject *self, PyObject *Py_UNUSED(igno+red))
1708+
ImportError_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
17091709
{
17101710
PyObject *res;
17111711
PyObject *state = ImportError_getstate(self);
@@ -1755,6 +1755,13 @@ MiddlingExtendsException(PyExc_ImportError, ModuleNotFoundError, ImportError,
17551755
* OSError extends Exception
17561756
*/
17571757

1758+
static inline PyOSErrorObject *
1759+
_PyOSError_CAST(PyObject *self)
1760+
{
1761+
assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_OSError));
1762+
return (PyOSErrorObject *)self;
1763+
}
1764+
17581765
#ifdef MS_WINDOWS
17591766
#include "errmap.h"
17601767
#endif
@@ -1891,7 +1898,7 @@ oserror_init(PyOSErrorObject *self, PyObject **p_args,
18911898
static PyObject *
18921899
OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
18931900
static int
1894-
OSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds);
1901+
OSError_init(PyObject *self, PyObject *args, PyObject *kwds);
18951902

18961903
static int
18971904
oserror_use_init(PyTypeObject *type)
@@ -1983,8 +1990,9 @@ OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
19831990
}
19841991

19851992
static int
1986-
OSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds)
1993+
OSError_init(PyObject *op, PyObject *args, PyObject *kwds)
19871994
{
1995+
PyOSErrorObject *self = _PyOSError_CAST(op);
19881996
PyObject *myerrno = NULL, *strerror = NULL;
19891997
PyObject *filename = NULL, *filename2 = NULL;
19901998
#ifdef MS_WINDOWS
@@ -2021,43 +2029,45 @@ OSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds)
20212029
}
20222030

20232031
static int
2024-
OSError_clear(PyOSErrorObject *self)
2032+
OSError_clear(PyObject *op)
20252033
{
2034+
PyOSErrorObject *self = _PyOSError_CAST(op);
20262035
Py_CLEAR(self->myerrno);
20272036
Py_CLEAR(self->strerror);
20282037
Py_CLEAR(self->filename);
20292038
Py_CLEAR(self->filename2);
20302039
#ifdef MS_WINDOWS
20312040
Py_CLEAR(self->winerror);
20322041
#endif
2033-
return BaseException_clear((PyBaseExceptionObject *)self);
2042+
return BaseException_clear(op);
20342043
}
20352044

20362045
static void
2037-
OSError_dealloc(PyOSErrorObject *self)
2046+
OSError_dealloc(PyObject *self)
20382047
{
20392048
_PyObject_GC_UNTRACK(self);
2040-
OSError_clear(self);
2041-
Py_TYPE(self)->tp_free((PyObject *)self);
2049+
(void)OSError_clear(self);
2050+
Py_TYPE(self)->tp_free(self);
20422051
}
20432052

20442053
static int
2045-
OSError_traverse(PyOSErrorObject *self, visitproc visit,
2046-
void *arg)
2054+
OSError_traverse(PyObject *op, visitproc visit, void *arg)
20472055
{
2056+
PyOSErrorObject *self = _PyOSError_CAST(op);
20482057
Py_VISIT(self->myerrno);
20492058
Py_VISIT(self->strerror);
20502059
Py_VISIT(self->filename);
20512060
Py_VISIT(self->filename2);
20522061
#ifdef MS_WINDOWS
20532062
Py_VISIT(self->winerror);
20542063
#endif
2055-
return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
2064+
return BaseException_traverse(op, visit, arg);
20562065
}
20572066

20582067
static PyObject *
2059-
OSError_str(PyOSErrorObject *self)
2068+
OSError_str(PyObject *op)
20602069
{
2070+
PyOSErrorObject *self = _PyOSError_CAST(op);
20612071
#define OR_NONE(x) ((x)?(x):Py_None)
20622072
#ifdef MS_WINDOWS
20632073
/* If available, winerror has the priority over myerrno */
@@ -2097,12 +2107,13 @@ OSError_str(PyOSErrorObject *self)
20972107
if (self->myerrno && self->strerror)
20982108
return PyUnicode_FromFormat("[Errno %S] %S",
20992109
self->myerrno, self->strerror);
2100-
return BaseException_str((PyBaseExceptionObject *)self);
2110+
return BaseException_str(op);
21012111
}
21022112

21032113
static PyObject *
2104-
OSError_reduce(PyOSErrorObject *self, PyObject *Py_UNUSED(ignored))
2114+
OSError_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
21052115
{
2116+
PyOSErrorObject *self = _PyOSError_CAST(op);
21062117
PyObject *args = self->args;
21072118
PyObject *res = NULL;
21082119

@@ -2141,8 +2152,9 @@ OSError_reduce(PyOSErrorObject *self, PyObject *Py_UNUSED(ignored))
21412152
}
21422153

21432154
static PyObject *
2144-
OSError_written_get(PyOSErrorObject *self, void *context)
2155+
OSError_written_get(PyObject *op, void *context)
21452156
{
2157+
PyOSErrorObject *self = _PyOSError_CAST(op);
21462158
if (self->written == -1) {
21472159
PyErr_SetString(PyExc_AttributeError, "characters_written");
21482160
return NULL;
@@ -2151,8 +2163,9 @@ OSError_written_get(PyOSErrorObject *self, void *context)
21512163
}
21522164

21532165
static int
2154-
OSError_written_set(PyOSErrorObject *self, PyObject *arg, void *context)
2166+
OSError_written_set(PyObject *op, PyObject *arg, void *context)
21552167
{
2168+
PyOSErrorObject *self = _PyOSError_CAST(op);
21562169
if (arg == NULL) {
21572170
if (self->written == -1) {
21582171
PyErr_SetString(PyExc_AttributeError, "characters_written");
@@ -2186,13 +2199,13 @@ static PyMemberDef OSError_members[] = {
21862199
};
21872200

21882201
static PyMethodDef OSError_methods[] = {
2189-
{"__reduce__", (PyCFunction)OSError_reduce, METH_NOARGS},
2202+
{"__reduce__", OSError_reduce, METH_NOARGS},
21902203
{NULL}
21912204
};
21922205

21932206
static PyGetSetDef OSError_getset[] = {
2194-
{"characters_written", (getter) OSError_written_get,
2195-
(setter) OSError_written_set, NULL},
2207+
{"characters_written", OSError_written_get,
2208+
OSError_written_set, NULL},
21962209
{NULL}
21972210
};
21982211

0 commit comments

Comments
 (0)
0