Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle extending self correctly #28

Merged
merged 1 commit into from
May 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion bitarray/_bitarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,11 +574,18 @@ extend_bitarray(bitarrayobject *self, bitarrayobject *other)
if (other->nbits == 0)
return 0;

/*
Note: other may be self. Thus we take the size before we change the
size, ensuring we only copy the right parts of the array.
*/

idx_t n_other_bits = other->nbits;

n_sum = self->nbits + other->nbits;
if (resize(self, n_sum) < 0)
return -1;

copy_n(self, n_sum - other->nbits, other, 0, other->nbits);
copy_n(self, n_sum - other->nbits, other, 0, n_other_bits);
return 0;
}

Expand Down
5 changes: 5 additions & 0 deletions bitarray/test_bitarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,11 @@ def test_string01(self):
self.assertEqual(c.tolist(), a + b)
self.check_obj(c)

def test_extend_self(self):
a = bitarray('1')
a.extend(a)
self.assertEqual(a, bitarray('11'))


tests.append(ExtendTests)

Expand Down