Skip to content

Commit 9b7cf75

Browse files
authored
bpo-33916: Fix bz2 and lzma init when called twice (GH-7843)
bz2, lzma: When Decompressor.__init__() is called twice, free the old lock to not leak memory.
1 parent 44742e9 commit 9b7cf75

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bz2 and lzma: When Decompressor.__init__() is called twice, free the old
2+
lock to not leak memory.

Modules/_bz2module.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,11 +634,15 @@ _bz2_BZ2Decompressor___init___impl(BZ2Decompressor *self)
634634
{
635635
int bzerror;
636636

637-
self->lock = PyThread_allocate_lock();
638-
if (self->lock == NULL) {
637+
PyThread_type_lock lock = PyThread_allocate_lock();
638+
if (lock == NULL) {
639639
PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
640640
return -1;
641641
}
642+
if (self->lock != NULL) {
643+
PyThread_free_lock(self->lock);
644+
}
645+
self->lock = lock;
642646

643647
self->needs_input = 1;
644648
self->bzs_avail_in_real = 0;

Modules/_lzmamodule.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,11 +1163,15 @@ _lzma_LZMADecompressor___init___impl(Decompressor *self, int format,
11631163
self->lzs.allocator = &self->alloc;
11641164
self->lzs.next_in = NULL;
11651165

1166-
self->lock = PyThread_allocate_lock();
1167-
if (self->lock == NULL) {
1166+
PyThread_type_lock lock = PyThread_allocate_lock();
1167+
if (lock == NULL) {
11681168
PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
11691169
return -1;
11701170
}
1171+
if (self->lock != NULL) {
1172+
PyThread_free_lock(self->lock);
1173+
}
1174+
self->lock = lock;
11711175

11721176
self->check = LZMA_CHECK_UNKNOWN;
11731177
self->needs_input = 1;

0 commit comments

Comments
 (0)