check for self-assignment in bitfield operator=

This commit is contained in:
arvidn 2018-11-16 02:44:11 +01:00 committed by Arvid Norberg
parent 1dc8c1c127
commit 8c18baf10e
2 changed files with 9 additions and 0 deletions

View File

@ -145,6 +145,7 @@ namespace libtorrent {
// assignment operator // assignment operator
bitfield& operator=(bitfield const& rhs) bitfield& operator=(bitfield const& rhs)
{ {
if (&rhs == this) return *this;
assign(rhs.data(), rhs.size()); assign(rhs.data(), rhs.size());
return *this; return *this;
} }

View File

@ -387,6 +387,14 @@ TORRENT_TEST(not_initialized)
// get_bit, clear_bit, set_bit // get_bit, clear_bit, set_bit
} }
TORRENT_TEST(self_assign)
{
bitfield test1(123, false);
test1 = test1;
TEST_EQUAL(test1.size(), 123);
TEST_EQUAL(test1.count(), 0);
}
TORRENT_TEST(not_initialized_assign) TORRENT_TEST(not_initialized_assign)
{ {
// check a not initialized empty bitfield // check a not initialized empty bitfield