Skip to content

Commit d61fcf8

Browse files
authored
gh-148688: Fix _BlocksOutputBuffer_Finish() double free (#148689)
If _BlocksOutputBuffer_Finish() fails (memory allocation failure), PyBytesWriter_Discard() is called on the writer. Then if _BlocksOutputBuffer_OnError() is called, it calls again PyBytesWriter_Discard() causing a double free. Fix _BlocksOutputBuffer_Finish() by setting buffer->writer to NULL, so _BlocksOutputBuffer_OnError() does nothing instead of calling PyBytesWriter_Discard() again.
1 parent e9bbf86 commit d61fcf8

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

Include/internal/pycore_blocks_output_buffer.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,12 @@ static inline PyObject *
242242
_BlocksOutputBuffer_Finish(_BlocksOutputBuffer *buffer,
243243
const Py_ssize_t avail_out)
244244
{
245+
PyObject *obj;
245246
assert(buffer->writer != NULL);
246-
return PyBytesWriter_FinishWithSize(buffer->writer,
247-
buffer->allocated - avail_out);
247+
obj = PyBytesWriter_FinishWithSize(buffer->writer,
248+
buffer->allocated - avail_out);
249+
buffer->writer = NULL;
250+
return obj;
248251
}
249252

250253
/* Clean up the buffer when an error occurred. */
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`bz2`, :mod:`compression.zstd`, :mod:`lzma`, :mod:`zlib`: Fix a double
2+
free on memory allocation failure. Patch by Victor Stinner.

0 commit comments

Comments
 (0)