]> granicus.if.org Git - python/commitdiff
bpo-33916: Fix bz2 and lzma init when called twice (GH-7843) (GH-7872)
authorVictor Stinner <vstinner@redhat.com>
Sat, 23 Jun 2018 13:06:11 +0000 (15:06 +0200)
committerGitHub <noreply@github.com>
Sat, 23 Jun 2018 13:06:11 +0000 (15:06 +0200)
bz2, lzma: When Decompressor.__init__() is called twice, free the old
lock to not leak memory.

(cherry picked from commit 9b7cf757213cf4d7ae1d436d86ad53f5ba362d55)

Misc/NEWS.d/next/Library/2018-06-21-11-35-47.bpo-33916.cZgPCD.rst [new file with mode: 0644]
Modules/_bz2module.c
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 (file)
index 0000000..65b9d2b
--- /dev/null
@@ -0,0 +1,2 @@
+bz2 and lzma: When Decompressor.__init__() is called twice, free the old
+lock to not leak memory.
index 9c5a631eff45f347e2f4c3d8bcb6cff5e908af9d..3f555992316dad3cd8da5daa4a6d7373a8bedf30 100644 (file)
@@ -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;
index b25faff5ac4924a377f6832aa7e4701c1db88e12..c265507ff58ed934d32dc88effb5b13dfa452d00 100644 (file)
@@ -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;