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

Skip to content

gh-111178: fix UBSan failures in Modules/posixmodule.c #129788

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 8 commits into from
Feb 24, 2025
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
10000
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -15584,9 +15584,12 @@ typedef struct {
#endif
} DirEntry;

#define DirEntry_CAST(op) ((DirEntry *)(op))

static void
DirEntry_dealloc(DirEntry *entry)
DirEntry_dealloc(PyObject *op)
{
DirEntry *entry = DirEntry_CAST(op);
PyTypeObject *tp = Py_TYPE(entry);
Py_XDECREF(entry->name);
Py_XDECREF(entry->path);
Expand Down Expand Up @@ -15915,8 +15918,9 @@ os_DirEntry_inode_impl(DirEntry *self)
}

static PyObject *
DirEntry_repr(DirEntry *self)
DirEntry_repr(PyObject *op)
{
DirEntry *self = DirEntry_CAST(op);
return PyUnicode_FromFormat("<DirEntry %R>", self->name);
}

Expand Down Expand Up @@ -16179,6 +16183,8 @@ typedef struct {
#endif
} ScandirIterator;

#define ScandirIterator_CAST(op) ((ScandirIterator *)(op))

#ifdef MS_WINDOWS

static int
Expand All @@ -16202,8 +16208,9 @@ ScandirIterator_closedir(ScandirIterator *iterator)
}

static PyObject *
ScandirIterator_iternext(ScandirIterator *iterator)
ScandirIterator_iternext(PyObject *op)
{
ScandirIterator *iterator = ScandirIterator_CAST(op);
WIN32_FIND_DATAW *file_data = &iterator->file_data;
BOOL success;
PyObject *entry;
Expand Down Expand Up @@ -16273,8 +16280,9 @@ ScandirIterator_closedir(ScandirIterator *iterator)
}

static PyObject *
ScandirIterator_iternext(ScandirIterator *iterator)
ScandirIterator_iternext(PyObject *op)
{
ScandirIterator *iterator = ScandirIterator_CAST(op);
struct dirent *direntp;
Py_ssize_t name_len;
int is_dot;
Expand Down Expand Up @@ -16326,37 +16334,40 @@ ScandirIterator_iternext(ScandirIterator *iterator)
#endif

static PyObject *
ScandirIterator_close(ScandirIterator *self, PyObject *args)
ScandirIterator_close(PyObject *op, PyObject *Py_UNUSED(ignored))
{
ScandirIterator *self = ScandirIterator_CAST(op);
ScandirIterator_closedir(self);
Py_RETURN_NONE;
}

static PyObject *
ScandirIterator_enter(PyObject *self, PyObject *args)
ScandirIterator_enter(PyObject *self, PyObject *Py_UNUSED(ignored))
{
return Py_NewRef(self);
}

static PyObject *
ScandirIterator_exit(ScandirIterator *self, PyObject *args)
ScandirIterator_exit(PyObject *op, PyObject *Py_UNUSED(args))
{
ScandirIterator *self = ScandirIterator_CAST(op);
ScandirIterator_closedir(self);
Py_RETURN_NONE;
}

static void
ScandirIterator_finalize(ScandirIterator *iterator)
ScandirIterator_finalize(PyObject *op)
{

ScandirIterator *iterator = ScandirIterator_CAST(op);
/* Save the current exception, if any. */
PyObject *exc = PyErr_GetRaisedException();

if (!ScandirIterator_is_closed(iterator)) {
ScandirIterator_closedir(iterator);

if (PyErr_ResourceWarning((PyObject *)iterator, 1,
"unclosed scandir iterator %R", iterator)) {
if (PyErr_ResourceWarning(op, 1,
"unclosed scandir iterator %R", iterator))
{
/* Spurious errors can appear at shutdown */
if (PyErr_ExceptionMatches(PyExc_Warning)) {
PyErr_FormatUnraisable("Exception ignored while finalizing "
Expand All @@ -16372,10 +16383,11 @@ ScandirIterator_finalize(ScandirIterator *iterator)
}

static void
ScandirIterator_dealloc(ScandirIterator *iterator)
ScandirIterator_dealloc(PyObject *op)
{
ScandirIterator *iterator = ScandirIterator_CAST(op);
PyTypeObject *tp = Py_TYPE(iterator);
if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
if (PyObject_CallFinalizerFromDealloc(op) < 0)
return;

freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
Expand All @@ -16384,9 +16396,9 @@ ScandirIterator_dealloc(ScandirIterator *iterator)
}

static PyMethodDef ScandirIterator_methods[] = {
{"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
{"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
{"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
{"__enter__", ScandirIterator_enter, METH_NOARGS},
{"__exit__", ScandirIterator_exit, METH_VARARGS},
{"close", ScandirIterator_close, METH_NOARGS},
{NULL}
};

Expand Down
Loading
0