Skip to content

Commit 249ec3e

Browse files
committed
fix UBSan failures for winconsoleio
1 parent 19ed68d commit 249ec3e

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

Modules/_io/winconsoleio.c

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ typedef struct {
221221
wchar_t wbuf;
222222
} winconsoleio;
223223

224+
#define _winconsoleio_CAST(op) ((winconsoleio *)(op))
225+
224226
int
225227
_PyWindowsConsoleIO_closed(PyObject *self)
226228
{
@@ -492,32 +494,35 @@ _io__WindowsConsoleIO___init___impl(winconsoleio *self, PyObject *nameobj,
492494
}
493495

494496
static int
495-
winconsoleio_traverse(winconsoleio *self, visitproc visit, void *arg)
497+
winconsoleio_traverse(PyObject *op, visitproc visit, void *arg)
496498
{
499+
winconsoleio *self = _winconsoleio_CAST(op);
497500
Py_VISIT(Py_TYPE(self));
498501
Py_VISIT(self->dict);
499502
return 0;
500503
}
501504

502505
static int
503-
winconsoleio_clear(winconsoleio *self)
506+
winconsoleio_clear(PyObject *op)
504507
{
508+
winconsoleio *self = _winconsoleio_CAST(op);
505509
Py_CLEAR(self->dict);
506510
return 0;
507511
}
508512

509513
static void
510-
winconsoleio_dealloc(winconsoleio *self)
514+
winconsoleio_dealloc(PyObject *op)
511515
{
516+
winconsoleio *self = _winconsoleio_CAST(op);
512517
PyTypeObject *tp = Py_TYPE(self);
513518
self->finalizing = 1;
514-
if (_PyIOBase_finalize((PyObject *) self) < 0)
519+
if (_PyIOBase_finalize(op) < 0)
515520
return;
516521
_PyObject_GC_UNTRACK(self);
517522
if (self->weakreflist != NULL)
518-
PyObject_ClearWeakRefs((PyObject *) self);
523+
PyObject_ClearWeakRefs(op);
519524
Py_CLEAR(self->dict);
520-
tp->tp_free((PyObject *)self);
525+
tp->tp_free(self);
521526
Py_DECREF(tp);
522527
}
523528

@@ -1137,9 +1142,10 @@ _io__WindowsConsoleIO_write_impl(winconsoleio *self, PyTypeObject *cls,
11371142
}
11381143

11391144
static PyObject *
1140-
winconsoleio_repr(winconsoleio *self)
1145+
winconsoleio_repr(PyObject *op)
11411146
{
1142-
const char *type_name = (Py_TYPE((PyObject *)self)->tp_name);
1147+
winconsoleio *self = _winconsoleio_CAST(op);
1148+
const char *type_name = Py_TYPE(self)->tp_name;
11431149

11441150
if (self->fd == -1) {
11451151
return PyUnicode_FromFormat("<%.100s [closed]>", type_name);
@@ -1197,28 +1203,31 @@ static PyMethodDef winconsoleio_methods[] = {
11971203
/* 'closed' and 'mode' are attributes for compatibility with FileIO. */
11981204

11991205
static PyObject *
1200-
get_closed(winconsoleio *self, void *closure)
1206+
get_closed(PyObject *op, void *Py_UNUSED(closure))
12011207
{
1208+
winconsoleio *self = _winconsoleio_CAST(op);
12021209
return PyBool_FromLong((long)(self->fd == -1));
12031210
}
12041211

12051212
static PyObject *
1206-
get_closefd(winconsoleio *self, void *closure)
1213+
get_closefd(PyObject *op, void *Py_UNUSED(closure))
12071214
{
1215+
winconsoleio *self = _winconsoleio_CAST(op);
12081216
return PyBool_FromLong((long)(self->closefd));
12091217
}
12101218

12111219
static PyObject *
1212-
get_mode(winconsoleio *self, void *closure)
1220+
get_mode(PyObject *op, void *Py_UNUSED(closure))
12131221
{
1222+
winconsoleio *self = _winconsoleio_CAST(op);
12141223
return PyUnicode_FromString(self->readable ? "rb" : "wb");
12151224
}
12161225

12171226
static PyGetSetDef winconsoleio_getsetlist[] = {
1218-
{"closed", (getter)get_closed, NULL, "True if the file is closed"},
1219-
{"closefd", (getter)get_closefd, NULL,
1227+
{"closed", get_closed, NULL, "True if the file is closed"},
1228+
{"closefd", get_closefd, NULL,
12201229
"True if the file descriptor will be closed by close()."},
1221-
{"mode", (getter)get_mode, NULL, "String giving the file mode"},
1230+
{"mode", get_mode, NULL, "String giving the file mode"},
12221231
{NULL},
12231232
};
12241233

0 commit comments

Comments
 (0)