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

Skip to content

gh-111178: fix UBSan failures in Modules/_lzmamodule.c #129783

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 5 commits into from
Feb 18, 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
10000
Diff view
Diff view
Next Next commit
fix UBSan failures for LZMA objects
  • Loading branch information
picnixz committed Jan 25, 2025
commit d47be9dea453b824aa16992eab34edc2e7921f51
17 changes: 11 additions & 6 deletions Modules/_lzmamodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ typedef struct {
PyThread_type_lock lock;
} Decompressor;

#define _Compressor_CAST(op) ((Compressor *)(op))
#define _Decompressor_CAST(op) ((Decompressor *)(op))

/* Helper functions. */

static int
Expand Down Expand Up @@ -857,14 +860,15 @@ Compressor_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
}

static void
Compressor_dealloc(Compressor *self)
Compressor_dealloc(PyObject *op)
{
Compressor *self = _Compressor_CAST(op);
lzma_end(&self->lzs);
if (self->lock != NULL) {
PyThread_free_lock(self->lock);
}
PyTypeObject *tp = Py_TYPE(self);
tp->tp_free((PyObject *)self);
tp->tp_free(self);
Py_DECREF(tp);
}

Expand All @@ -875,7 +879,7 @@ static PyMethodDef Compressor_methods[] = {
};

static int
Compressor_traverse(Compressor *self, visitproc visit, void *arg)
Compressor_traverse(PyObject *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
return 0;
Expand Down Expand Up @@ -1304,8 +1308,9 @@ _lzma_LZMADecompressor_impl(PyTypeObject *type, int format,
}

static void
Decompressor_dealloc(Decompressor *self)
Decompressor_dealloc(PyObject *op)
{
Decompressor *self = _Decompressor_CAST(op);
if(self->input_buffer != NULL)
PyMem_Free(self->input_buffer);

Expand All @@ -1315,12 +1320,12 @@ Decompressor_dealloc(Decompressor *self)
PyThread_free_lock(self->lock);
}
PyTypeObject *tp = Py_TYPE(self);
tp->tp_free((PyObject *)self);
tp->tp_free(self);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be op?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tp_free takes a void * as input, so it doesn't matter. I've chosen the one that reduces the diff (but honestly, I don't know whether I've always been consistent)

Py_DECREF(tp);
}

static int
Decompressor_traverse(Decompressor *self, visitproc visit, void *arg)
Decompressor_traverse(PyObject *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
return 0;
Expand Down
0