Skip to content

Commit

Permalink
Ensure buffer release before return
Browse files Browse the repository at this point in the history
After reading data into a buffer the reading_generator_next function
calls invokes the underlying yajl parsing routine, which might return
successfully or not. There was a memory leak in the latter case, since
the "view" Py_buffer object was not being released before checking for
NULL returns and returning NULL ourselves.

This commit fixes the bug by ensuring we release the "view" object
before any returns, ensuring memory isn't leaked.

This was originally reported in #97.

Signed-off-by: Rodrigo Tobar <rtobar@icrar.org>
  • Loading branch information
rtobar committed Jun 12, 2023
1 parent 92a851e commit d8e398a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
## Development

* Added support for Python 3.12.
* Fixed a memory leak in the ``yajl2_c`` backend
triggered only when the underlying ``yajl`` functions
reported a failure (#97).

## [3.2.0.post0]

Expand Down
6 changes: 4 additions & 2 deletions ijson/backends/yajl2_c/reading_generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ PyObject *reading_generator_next(reading_generator_t *self)
length = view.len;
PyObject *send_res = ijson_yajl_parse(basic_parse_basecoro->h, view.buf, view.len);
Py_DECREF(pbuffer);
PyBuffer_Release(&view);
N_N(send_res);
}
else {
Expand All @@ -84,9 +85,10 @@ PyObject *reading_generator_next(reading_generator_t *self)
N_M1(length);
Py_DECREF(plength);
N_M1(PyObject_GetBuffer(self->buffer, &view, PyBUF_SIMPLE));
N_N(ijson_yajl_parse(basic_parse_basecoro->h, view.buf, length));
PyObject *send_res = ijson_yajl_parse(basic_parse_basecoro->h, view.buf, length);
PyBuffer_Release(&view);
N_N(send_res);
}
PyBuffer_Release(&view);
nevents = PyList_Size(events);

if (length == 0) {
Expand Down

0 comments on commit d8e398a

Please sign in to comment.