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

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 0789b6179e52be7fda174e1be53932e92a9d6998..3890b60b1b87b3145ca8a27136242d7389d65c25 100644 (file)
@@ -634,11 +634,15 @@ _bz2_BZ2Decompressor___init___impl(BZ2Decompressor *self)
 {
     int bzerror;
 
-    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;
 
     self->needs_input = 1;
     self->bzs_avail_in_real = 0;
index 5bcd088d772188391ad40734c6b92804d0ee9001..7b501d8202d8b99e927f42708ae86349dc279b0d 100644 (file)
@@ -1163,11 +1163,15 @@ _lzma_LZMADecompressor___init___impl(Decompressor *self, int format,
     self->lzs.allocator = &self->alloc;
     self->lzs.next_in = NULL;
 
-    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;
 
     self->check = LZMA_CHECK_UNKNOWN;
     self->needs_input = 1;