[New-bugs-announce] [issue34729] bz2/lzma: Compressor/decompressor crash if __init__ is not called

Alexey Izbyshev report at bugs.python.org
Tue Sep 18 18:49:05 EDT 2018


New submission from Alexey Izbyshev <izbyshev at ispras.ru>:

The compressor/decompressor classes from bz2 and lzma modules rely on __init__() for initialization, but it is not guaranteed to be called. Method calls on an uninitialized object crash:

>>> from bz2 import BZ2Compressor as C
>>> c = C.__new__(C)
>>> c.compress(b'')
Segmentation fault (core dumped)

I see two ways to fix this:

1) Move some initialization (notably, for "lock" field) to __new__() and add initialization checks to other methods. This should be backwards-compatible.

2) Move all initialization to __new__(). Since compressor/decompressor classes are not subclassable, it'll break only code than repeatedly calls __init__() on the same object. The simplicity of the fix might outweigh the necessity to support such code.
(However, in 2.7, classes in bz2 *are* subclassable; lzma is not present in 2.7).

Which way is more preferable?

----------
components: Extension Modules
messages: 325691
nosy: berker.peksag, izbyshev, serhiy.storchaka
priority: normal
severity: normal
status: open
title: bz2/lzma: Compressor/decompressor crash if __init__ is not called
type: crash
versions: Python 2.7, Python 3.6, Python 3.7, Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34729>
_______________________________________


More information about the New-bugs-announce mailing list