From 8c18baf10ec89dc0b207f7c1f5c5a0fd5fe2b664 Mon Sep 17 00:00:00 2001 From: arvidn Date: Fri, 16 Nov 2018 02:44:11 +0100 Subject: [PATCH] check for self-assignment in bitfield operator= --- include/libtorrent/bitfield.hpp | 1 + test/test_bitfield.cpp | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/include/libtorrent/bitfield.hpp b/include/libtorrent/bitfield.hpp index 1b54d9f2a..2aad3738c 100644 --- a/include/libtorrent/bitfield.hpp +++ b/include/libtorrent/bitfield.hpp @@ -145,6 +145,7 @@ namespace libtorrent { // assignment operator bitfield& operator=(bitfield const& rhs) { + if (&rhs == this) return *this; assign(rhs.data(), rhs.size()); return *this; } diff --git a/test/test_bitfield.cpp b/test/test_bitfield.cpp index a1906b8ad..e52eda752 100644 --- a/test/test_bitfield.cpp +++ b/test/test_bitfield.cpp @@ -387,6 +387,14 @@ TORRENT_TEST(not_initialized) // 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) { // check a not initialized empty bitfield