From 07b4ce988b3468ae56bdc24a35c647a51baf3c16 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Wed, 20 Jul 2016 11:47:40 +0200 Subject: [PATCH] eap::credentials_ttls::m_inner is std::unique_ptr managed now (Also fixes a memory leak caused by missing destructor pointer delete) --- lib/TTLS/include/Credentials.h | 37 ++++++++++++++++++++-------------- lib/TTLS/src/Credentials.cpp | 18 +++++++---------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/lib/TTLS/include/Credentials.h b/lib/TTLS/include/Credentials.h index 4654eaa..bc03a36 100644 --- a/lib/TTLS/include/Credentials.h +++ b/lib/TTLS/include/Credentials.h @@ -59,6 +59,8 @@ namespace eapserial #include "../../TLS/include/Credentials.h" #include "../../PAP/include/Credentials.h" +#include + namespace eap { @@ -181,7 +183,7 @@ namespace eap /// @} public: - credentials *m_inner; ///< Inner credentials + std::unique_ptr m_inner; ///< Inner credentials }; } @@ -192,27 +194,32 @@ namespace eapserial { pack(cursor, (const eap::credentials_tls&)val); if (val.m_inner) { - if (dynamic_cast(val.m_inner)) { - pack(cursor, (unsigned char)eap::type_pap); + if (dynamic_cast(val.m_inner.get())) { + pack(cursor, eap::type_pap); pack(cursor, (const eap::credentials_pap&)*val.m_inner); } else { assert(0); // Unsupported inner authentication method type. - pack(cursor, (unsigned char)0); + pack(cursor, eap::type_undefined); } } else - pack(cursor, (unsigned char)0); + pack(cursor, eap::type_undefined); } inline size_t get_pk_size(const eap::credentials_ttls &val) { - size_t size_inner = sizeof(unsigned char); + size_t size_inner; if (val.m_inner) { - if (dynamic_cast(val.m_inner)) - size_inner += get_pk_size((const eap::credentials_pap&)*val.m_inner); - else + if (dynamic_cast(val.m_inner.get())) { + size_inner = + get_pk_size(eap::type_pap) + + get_pk_size((const eap::credentials_pap&)*val.m_inner); + } else { assert(0); // Unsupported inner authentication method type. - } + size_inner = get_pk_size(eap::type_undefined); + } + } else + size_inner = get_pk_size(eap::type_undefined); return get_pk_size((const eap::credentials_tls&)val) + @@ -224,16 +231,16 @@ namespace eapserial { unpack(cursor, (eap::credentials_tls&)val); - assert(!val.m_inner); - unsigned char eap_type; + eap::type_t eap_type; unpack(cursor, eap_type); switch (eap_type) { case eap::type_pap: - val.m_inner = new eap::credentials_pap(val.m_module); + val.m_inner.reset(new eap::credentials_pap(val.m_module)); unpack(cursor, (eap::credentials_pap&)*val.m_inner); break; - case 0 : break; - default : assert(0); // Unsupported inner authentication method type. + default: + assert(0); // Unsupported inner authentication method type. + val.m_inner.reset(nullptr); } } } diff --git a/lib/TTLS/src/Credentials.cpp b/lib/TTLS/src/Credentials.cpp index 3dc7102..44b21bf 100644 --- a/lib/TTLS/src/Credentials.cpp +++ b/lib/TTLS/src/Credentials.cpp @@ -28,23 +28,23 @@ using namespace winstd; // eap::credentials_ttls ////////////////////////////////////////////////////////////////////// -eap::credentials_ttls::credentials_ttls(_In_ module &mod) : credentials_tls(mod) +eap::credentials_ttls::credentials_ttls(_In_ module &mod) : + credentials_tls(mod) { } eap::credentials_ttls::credentials_ttls(_In_ const credentials_ttls &other) : - m_inner(other.m_inner ? (credentials*)other.m_inner->clone() : NULL), + m_inner(other.m_inner ? (credentials*)other.m_inner->clone() : nullptr), credentials_tls(other) { } eap::credentials_ttls::credentials_ttls(_Inout_ credentials_ttls &&other) : - m_inner(other.m_inner), + m_inner(std::move(other.m_inner)), credentials_tls(std::move(other)) { - other.m_inner = NULL; } @@ -52,9 +52,7 @@ eap::credentials_ttls& eap::credentials_ttls::operator=(_In_ const credentials_t { if (this != &other) { (credentials_tls&)*this = other; - - if (m_inner) delete m_inner; - m_inner = other.m_inner ? (credentials*)other.m_inner->clone() : NULL; + m_inner.reset(other.m_inner ? (credentials*)other.m_inner->clone() : nullptr); } return *this; @@ -65,10 +63,7 @@ eap::credentials_ttls& eap::credentials_ttls::operator=(_Inout_ credentials_ttls { if (this != &other) { (credentials_tls&)*this = std::move(other); - - if (m_inner) delete m_inner; - m_inner = other.m_inner; - other.m_inner = NULL; + m_inner = std::move(other.m_inner); } return *this; @@ -138,6 +133,7 @@ bool eap::credentials_ttls::load(_In_ IXMLDOMNode *pConfigRoot, _Out_ EAP_ERROR if (!credentials_tls::load(pConfigRoot, ppEapError)) return false; + // TODO: For the time being, there is no detection what type is inner method. Introduce one! if (m_inner) { com_obj pXmlElInnerAuthenticationMethod; if ((dwResult = eapxml::select_node(pConfigRoot, bstr(L"eap-metadata:InnerAuthenticationMethod"), &pXmlElInnerAuthenticationMethod)) != ERROR_SUCCESS) {