Skip to content

Commit

Permalink
Fix memory leak in PDU's move assignment operator
Browse files Browse the repository at this point in the history
  • Loading branch information
mfontanini committed Dec 14, 2017
1 parent 8f85a6e commit f44b253
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion include/tins/pdu.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,9 @@ class TINS_API PDU {
* \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);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/src/pdu_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f44b253

Please sign in to comment.