From a9b158192f2f0d4c57b4f26aa7256c17ec185e8e Mon Sep 17 00:00:00 2001 From: rdb Date: Tue, 9 Jun 2020 18:15:36 +0200 Subject: [PATCH] putil: fix crash in BitArray::has_any_of() --- panda/src/putil/bitArray.cxx | 10 +++++----- tests/putil/test_bitarray.py | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 tests/putil/test_bitarray.py diff --git a/panda/src/putil/bitArray.cxx b/panda/src/putil/bitArray.cxx index 2ba077e0c1..aab43ee99e 100644 --- a/panda/src/putil/bitArray.cxx +++ b/panda/src/putil/bitArray.cxx @@ -114,6 +114,11 @@ has_any_of(int low_bit, int size) const { ++w; while (size > 0) { + if ((size_t)w >= get_num_words()) { + // Now we're up to the highest bits. + return (_highest_bits != 0); + } + if (size <= num_bits_per_word) { // The remainder fits within one word of the array. return _array[w].has_any_of(0, size); @@ -125,11 +130,6 @@ has_any_of(int low_bit, int size) const { } size -= num_bits_per_word; ++w; - - if (w >= (int)get_num_words()) { - // Now we're up to the highest bits. - return (_highest_bits != 0); - } } return false; diff --git a/tests/putil/test_bitarray.py b/tests/putil/test_bitarray.py new file mode 100644 index 0000000000..66e4292e76 --- /dev/null +++ b/tests/putil/test_bitarray.py @@ -0,0 +1,18 @@ +from panda3d.core import BitArray + + +def test_bitarray_has_any_of(): + ba = BitArray() + assert not ba.has_any_of(100, 200) + + ba = BitArray() + ba.set_range(0, 53) + assert ba.has_any_of(52, 1) + assert ba.has_any_of(52, 100) + assert not ba.has_any_of(53, 45) + + ba = BitArray() + ba.invert_in_place() + assert ba.has_any_of(0, 1) + assert ba.has_any_of(53, 45) + assert ba.has_any_of(0, 100)