STINNER Victor added the comment:

The leaking unit test was added to fix a bug found by fuzzing: see bpo-19878.

To fix the bug, I suggest to:

1) Add a clear method: extract most of BZ2File_dealloc() code except of code to 
clear the lock and the tp_free. Create a new BZ2File_clear() function which 
clears the file and buffers. Call BZ2File_clear() in BZ2File_dealloc(), but 
*before* destroying the lock (see later for the rationale). Replace 
Py_XDECREF(self->file) with Py_CLEAR(self->file). Add ACQUIRE_LOCK/RELEASE_LOCK 
in BZ2File_clear.

2) Maybe declare BZ2File_clear() as tp_clear. In this case, change 
BZ2File_clear() return type from void to int, and add "return 0" at the end

3) Move the lock initialization before "self->file = 
PyObject_CallFunction(...)" in BZ2File_init(). Modify the code to not create 
the lock twice: if the lock was already created by a previous call to 
BZ2File_init(), do nothing.

4) Call BZ2File_clear() in BZ2File_init() after the initialization of the lock

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue30614>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to