From f44b253a42506763f1fe066503f0802e4862274b Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Thu, 14 Dec 2017 14:42:46 -0300 Subject: [PATCH] Fix memory leak in PDU's move assignment operator Fixes #272 --- include/tins/pdu.h | 3 ++- tests/src/pdu_test.cpp | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/tins/pdu.h b/include/tins/pdu.h index 6f6d5c0..62dc0f8 100644 --- a/include/tins/pdu.h +++ b/include/tins/pdu.h @@ -244,8 +244,9 @@ public: * \param rhs The PDU to be moved. */ PDU& operator=(PDU &&rhs) TINS_NOEXCEPT { + delete inner_pdu_; + inner_pdu_ = 0; std::swap(inner_pdu_, rhs.inner_pdu_); - rhs.inner_pdu_ = 0; if (inner_pdu_) { inner_pdu_->parent_pdu(this); } diff --git a/tests/src/pdu_test.cpp b/tests/src/pdu_test.cpp index 483dc3e..faee7e6 100644 --- a/tests/src/pdu_test.cpp +++ b/tests/src/pdu_test.cpp @@ -83,6 +83,14 @@ TEST_F(PDUTest, OperatorConcatOnPacket) { EXPECT_TRUE(std::equal(raw->payload().begin(), raw->payload().end(), raw_payload.begin())); } +#if TINS_IS_CXX11 +TEST_F(PDUTest, MoveAssignment) { + IP packet = IP("192.168.0.1") / TCP(22, 52); + packet = IP("1.2.3.4"); + EXPECT_TRUE(packet.inner_pdu() == 0); +} +#endif // TINS_IS_CXX11 + TEST_F(PDUTest, TinsCast) { PDU* null_pdu = 0; TCP tcp;