Skip to content

Commit 1094891

Browse files
authored
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 9b7cf75)
1 parent 688325e commit 1094891

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
@@ -652,11 +652,15 @@ _bz2_BZ2Decompressor___init___impl(BZ2Decompressor *self)
652652
int bzerror;
653653

654654
#ifdef WITH_THREAD
655-
self->lock = PyThread_allocate_lock();
656-
if (self->lock == NULL) {
655+
PyThread_type_lock lock = PyThread_allocate_lock();
656+
if (lock == NULL) {
657657
PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
658658
return -1;
659659
}
660+
if (self->lock != NULL) {
661+
PyThread_free_lock(self->lock);
662+
}
663+
self->lock = lock;
660664
#endif
661665

662666
self->needs_input = 1;

Modules/_lzmamodule.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,11 +1181,15 @@ _lzma_LZMADecompressor___init___impl(Decompressor *self, int format,
11811181
self->lzs.next_in = NULL;
11821182

11831183
#ifdef WITH_THREAD
1184-
self->lock = PyThread_allocate_lock();
1185-
if (self->lock == NULL) {
1184+
PyThread_type_lock lock = PyThread_allocate_lock();
1185+
if (lock == NULL) {
11861186
PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
11871187
return -1;
11881188
}
1189+
if (self->lock != NULL) {
1190+
PyThread_free_lock(self->lock);
1191+
}
1192+
self->lock = lock;
11891193
#endif
11901194

11911195
self->check = LZMA_CHECK_UNKNOWN;

0 commit comments

Comments
 (0)