[Python-checkins] bpo-33916: Fix bz2 and lzma init when called twice (GH-7843) (GH-7872)

Victor Stinner webhook-mailer at python.org
Sat Jun 23 09:06:17 EDT 2018


https://github.com/python/cpython/commit/1094891b2e3eafef464819acd4cd04cfd2b59661
commit: 1094891b2e3eafef464819acd4cd04cfd2b59661
branch: 3.6
author: Victor Stinner <vstinner at redhat.com>
committer: GitHub <noreply at github.com>
date: 2018-06-23T15:06:11+02:00
summary:

bpo-33916: Fix bz2 and lzma init when called twice (GH-7843) (GH-7872)

bz2, lzma: When Decompressor.__init__() is called twice, free the old
lock to not leak memory.

(cherry picked from commit 9b7cf757213cf4d7ae1d436d86ad53f5ba362d55)

files:
A Misc/NEWS.d/next/Library/2018-06-21-11-35-47.bpo-33916.cZgPCD.rst
M Modules/_bz2module.c
M Modules/_lzmamodule.c

diff --git a/Misc/NEWS.d/next/Library/2018-06-21-11-35-47.bpo-33916.cZgPCD.rst b/Misc/NEWS.d/next/Library/2018-06-21-11-35-47.bpo-33916.cZgPCD.rst
new file mode 100644
index 000000000000..65b9d2bf89a5
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-06-21-11-35-47.bpo-33916.cZgPCD.rst
@@ -0,0 +1,2 @@
+bz2 and lzma: When Decompressor.__init__() is called twice, free the old
+lock to not leak memory.
diff --git a/Modules/_bz2module.c b/Modules/_bz2module.c
index 9c5a631eff45..3f555992316d 100644
--- a/Modules/_bz2module.c
+++ b/Modules/_bz2module.c
@@ -652,11 +652,15 @@ _bz2_BZ2Decompressor___init___impl(BZ2Decompressor *self)
     int bzerror;
 
 #ifdef WITH_THREAD
-    self->lock = PyThread_allocate_lock();
-    if (self->lock == NULL) {
+    PyThread_type_lock lock = PyThread_allocate_lock();
+    if (lock == NULL) {
         PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
         return -1;
     }
+    if (self->lock != NULL) {
+        PyThread_free_lock(self->lock);
+    }
+    self->lock = lock;
 #endif
 
     self->needs_input = 1;
diff --git a/Modules/_lzmamodule.c b/Modules/_lzmamodule.c
index b25faff5ac49..c265507ff58e 100644
--- a/Modules/_lzmamodule.c
+++ b/Modules/_lzmamodule.c
@@ -1181,11 +1181,15 @@ _lzma_LZMADecompressor___init___impl(Decompressor *self, int format,
     self->lzs.next_in = NULL;
 
 #ifdef WITH_THREAD
-    self->lock = PyThread_allocate_lock();
-    if (self->lock == NULL) {
+    PyThread_type_lock lock = PyThread_allocate_lock();
+    if (lock == NULL) {
         PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
         return -1;
     }
+    if (self->lock != NULL) {
+        PyThread_free_lock(self->lock);
+    }
+    self->lock = lock;
 #endif
 
     self->check = LZMA_CHECK_UNKNOWN;



More information about the Python-checkins mailing list