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

Skip to content

gh-111178: fix UBSan failures in Modules/_jsonmodule.c #129781

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 17, 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
Diff view
Diff view
Prev Previous commit
Next Next commit
fix UBSan failures for json::PyEncoderObject
  • Loading branch information
picnixz committed Jan 25, 2025
commit 2f4139416ce0c1c6242786e85371fa3cb3600ae9
24 changes: 14 additions & 10 deletions Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ typedef struct _PyEncoderObject {
PyCFunction fast_encode;
} PyEncoderObject;

#define _PyEncoderObject_CAST(op) ((PyEncoderObject *)(op))

static PyMemberDef encoder_members[] = {
{"markers", _Py_T_OBJECT, offsetof(PyEncoderObject, markers), Py_READONLY, "markers"},
{"default", _Py_T_OBJECT, offsetof(PyEncoderObject, defaultfn), Py_READONLY, "default"},
Expand Down Expand Up @@ -88,7 +90,7 @@ encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static void
encoder_dealloc(PyObject *self);
static int
encoder_clear(PyEncoderObject *self);
encoder_clear(PyObject *self);
static int
encoder_listencode_list(PyEncoderObject *s, PyUnicodeWriter *writer, PyObject *seq, Py_ssize_t indent_level, PyObject *indent_cache);
static int
Expand Down Expand Up @@ -1250,8 +1252,7 @@ encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)

if (PyCFunction_Check(s->encoder)) {
PyCFunction f = PyCFunction_GetFunction(s->encoder);
if (f == (PyCFunction)py_encode_basestring_ascii ||
f == (PyCFunction)py_encode_basestring) {
if (f == py_encode_basestring_ascii || f == py_encode_basestring) {
s->fast_encode = f;
}
}
Expand Down Expand Up @@ -1346,12 +1347,13 @@ write_newline_indent(PyUnicodeWriter *writer,


static PyObject *
encoder_call(PyEncoderObject *self, PyObject *args, PyObject *kwds)
encoder_call(PyObject *op, PyObject *args, PyObject *kwds)
{
/* Python callable interface to encode_listencode_obj */
static char *kwlist[] = {"obj", "_current_indent_level", NULL};
PyObject *obj;
Py_ssize_t indent_level;
PyEncoderObject *self = _PyEncoderObject_CAST(op);

if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:_iterencode", kwlist,
&obj, &indent_level))
Expand Down Expand Up @@ -1823,14 +1825,15 @@ encoder_dealloc(PyObject *self)
PyTypeObject *tp = Py_TYPE(self);
/* bpo-31095: UnTrack is needed before calling any callbacks */
PyObject_GC_UnTrack(self);
encoder_clear((PyEncoderObject *)self);
(void)encoder_clear(self);
tp->tp_free(self);
Py_DECREF(tp);
}

static int
encoder_traverse(PyEncoderObject *self, visitproc visit, void *arg)
encoder_traverse(PyObject *op, visitproc visit, void *arg)
{
PyEncoderObject *self = _PyEncoderObject_CAST(op);
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->markers);
Py_VISIT(self->defaultfn);
Expand All @@ -1842,8 +1845,9 @@ encoder_traverse(PyEncoderObject *self, visitproc visit, void *arg)
}

static int
encoder_clear(PyEncoderObject *self)
encoder_clear(PyObject *op)
{
PyEncoderObject *self = _PyEncoderObject_CAST(op);
/* Deallocate Encoder */
Py_CLEAR(self->markers);
Py_CLEAR(self->defaultfn);
Expand Down Expand Up @@ -1877,15 +1881,15 @@ static PyType_Spec PyEncoderType_spec = {

static PyMethodDef speedups_methods[] = {
{"encode_basestring_ascii",
(PyCFunction)py_encode_basestring_ascii,
py_encode_basestring_ascii,
METH_O,
pydoc_encode_basestring_ascii},
{"encode_basestring",
(PyCFunction)py_encode_basestring,
py_encode_basestring,
METH_O,
pydoc_encode_basestring},
{"scanstring",
(PyCFunction)py_scanstring,
py_scanstring,
METH_VARARGS,
pydoc_scanstring},
{NULL, NULL, 0, NULL}
Expand Down
0