From 16a29fab3e57d791468924f0f64dec728b78d6e9 Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Mon, 26 Nov 2012 15:03:09 -0300 Subject: [PATCH] Fixed memory leak in Packet's copy assignment operator. --- include/packet.h | 14 +++++++++----- src/pdu.cpp | 2 +- tests/configure | 14 ++++++++++++++ tests/configure.ac | 6 ++++++ 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/include/packet.h b/include/packet.h index 339ce95..bec79e2 100644 --- a/include/packet.h +++ b/include/packet.h @@ -156,9 +156,11 @@ public: * This calls PDU::clone on the rhs's PDU* member. */ Packet& operator=(const Packet &rhs) { - ts = rhs.timestamp(); - if(rhs.pdu()) - pdu_ = rhs.pdu()->clone(); + if(this != &rhs) { + delete pdu_; + ts = rhs.timestamp(); + pdu_ = rhs.pdu() ? rhs.pdu()->clone() : 0; + } return *this; } @@ -174,8 +176,10 @@ public: * Move assignment operator. */ Packet& operator=(Packet &&rhs){ - std::swap(pdu_, rhs.pdu_); - ts = rhs.timestamp(); + if(this != &rhs) { + std::swap(pdu_, rhs.pdu_); + ts = rhs.timestamp(); + } return *this; } #endif diff --git a/src/pdu.cpp b/src/pdu.cpp index 55c2d98..068bb41 100644 --- a/src/pdu.cpp +++ b/src/pdu.cpp @@ -71,7 +71,7 @@ void PDU::send(PacketSender &) { } PDU *PDU::recv_response(PacketSender &) { - return false; + return 0; } void PDU::inner_pdu(PDU *next_pdu) { diff --git a/tests/configure b/tests/configure index 2761978..ea27acd 100755 --- a/tests/configure +++ b/tests/configure @@ -652,6 +652,7 @@ SHELL' ac_subst_files='' ac_user_opts=' enable_option_checking +enable_c__11 ' ac_precious_vars='build_alias host_alias @@ -1270,6 +1271,12 @@ if test -n "$ac_init_help"; then esac cat <<\_ACEOF +Optional Features: + --disable-option-checking ignore unrecognized --enable/--with options + --disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no) + --enable-FEATURE[=ARG] include FEATURE [ARG=yes] + --enable-c++11 enable C++11 features + Some influential environment variables: CXX C++ compiler command CXXFLAGS C++ compiler flags @@ -2509,6 +2516,13 @@ ac_compiler_gnu=$ac_cv_cxx_compiler_gnu CFLAGS="-DTINS_DEBUG -g" +# Check whether --enable-c++11 was given. +if test "${enable_c__11+set}" = set; then : + enableval=$enable_c__11; CFLAGS="$CFLAGS -std=c++0x" + +fi + + ac_ext=cpp ac_cpp='$CXXCPP $CPPFLAGS' diff --git a/tests/configure.ac b/tests/configure.ac index 26a49ea..f8863e6 100644 --- a/tests/configure.ac +++ b/tests/configure.ac @@ -5,6 +5,12 @@ AC_LANG(C++) CFLAGS="-DTINS_DEBUG -g" +AC_ARG_ENABLE( + c++11, + [ --enable-c++11 enable C++11 features], + [CFLAGS="$CFLAGS -std=c++0x"] +) + AC_CHECK_HEADERS([pcap.h gtest/gtest.h]) AC_CHECK_LIB(pcap, pcap_loop, [], [AC_MSG_ERROR([pcap library is needed!])]) #AC_CHECK_LIB([gtest_main], [main], [], [AC_MSG_ERROR([gtest library is needed!])])