ilanschnell / bitarray

efficient arrays of booleans for Python
Other
716 stars 98 forks source link

segfault when on bitwise operation on int and bitarray #209

Closed tneele closed 1 year ago

tneele commented 1 year ago

In the interactive shell of python, I ran the following and obtained a segfault:

Python 3.11.4 (main, Jun  9 2023, 07:59:55) [GCC 12.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from bitarray import bitarray
>>> 3 & bitarray('111')
Segmentation fault (core dumped)

According to gdb, this takes place somewhere in the cpython implementation of bitarray:

#0  __memcpy_avx_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:318
#1  0x00007ffff7bf4867 in ?? () from /usr/lib/python3/dist-packages/bitarray/_bitarray.cpython-311-x86_64-linux-gnu.so
...

By the way, swapping the argument just yields a type error:

>>> bitarray('111') | 3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bitarray expected for bitwise operation
ilanschnell commented 1 year ago

Thank you for reporting this issue. What version of bitarray are you using? While the segfault was a problem, it should have been fixed in a few years ago. Using the latest version 2.8.1 of bitarray, I get:

>>> from bitarray import bitarray
>>> 3 & bitarray('111')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for &: 'int' and 'bitarray.bitarray'
>>> bitarray('111') | 3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'bitarray.bitarray' and 'int'

Note that for the second operation error message if different.

ilanschnell commented 1 year ago

I was able to verify that this bug was introduced in version 1.5.0 (Aug 2020) and fixed in version 1.8.2 (March 2021). This exact issue has actually been raised in #116 (and was fixed in the 1.8.2 release).

tneele commented 1 year ago

Thanks @ilanschnell. For the record, I'm running version 1.6.3, obtained from the Ubuntu package python3-bitarray. The next Ubuntu release distributes bitarray version 2.8.1, so it should be fixed there.