From d7330e586aee1912838952c6583d9449060d776a Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Fri, 22 Sep 2017 19:53:20 -0400 Subject: Refactor to avoid explicit delete in BER_Decoder --- src/lib/asn1/ber_dec.cpp | 32 +++++++++----------------------- src/lib/asn1/ber_dec.h | 6 +++--- 2 files changed, 12 insertions(+), 26 deletions(-) (limited to 'src/lib') diff --git a/src/lib/asn1/ber_dec.cpp b/src/lib/asn1/ber_dec.cpp index 67a2c9d8e..dd83e2446 100644 --- a/src/lib/asn1/ber_dec.cpp +++ b/src/lib/asn1/ber_dec.cpp @@ -289,7 +289,6 @@ BER_Decoder& BER_Decoder::end_cons() BER_Decoder::BER_Decoder(DataSource& src) { m_source = &src; - m_owns = false; m_pushed.type_tag = m_pushed.class_tag = NO_OBJECT; m_parent = nullptr; } @@ -299,8 +298,8 @@ BER_Decoder::BER_Decoder(DataSource& src) */ BER_Decoder::BER_Decoder(const uint8_t data[], size_t length) { - m_source = new DataSource_Memory(data, length); - m_owns = true; + m_data_src.reset(new DataSource_Memory(data, length)); + m_source = m_data_src.get(); m_pushed.type_tag = m_pushed.class_tag = NO_OBJECT; m_parent = nullptr; } @@ -310,8 +309,8 @@ BER_Decoder::BER_Decoder(const uint8_t data[], size_t length) */ BER_Decoder::BER_Decoder(const secure_vector& data) { - m_source = new DataSource_Memory(data); - m_owns = true; + m_data_src.reset(new DataSource_Memory(data)); + m_source = m_data_src.get(); m_pushed.type_tag = m_pushed.class_tag = NO_OBJECT; m_parent = nullptr; } @@ -321,8 +320,8 @@ BER_Decoder::BER_Decoder(const secure_vector& data) */ BER_Decoder::BER_Decoder(const std::vector& data) { - m_source = new DataSource_Memory(data.data(), data.size()); - m_owns = true; + m_data_src.reset(new DataSource_Memory(data.data(), data.size())); + m_source = m_data_src.get(); m_pushed.type_tag = m_pushed.class_tag = NO_OBJECT; m_parent = nullptr; } @@ -333,26 +332,13 @@ BER_Decoder::BER_Decoder(const std::vector& data) BER_Decoder::BER_Decoder(const BER_Decoder& other) { m_source = other.m_source; - m_owns = false; - if(other.m_owns) - { - other.m_owns = false; - m_owns = true; - } + + // take ownership + std::swap(m_data_src, other.m_data_src); m_pushed.type_tag = m_pushed.class_tag = NO_OBJECT; m_parent = other.m_parent; } -/* -* BER_Decoder Destructor -*/ -BER_Decoder::~BER_Decoder() - { - if(m_owns) - delete m_source; - m_source = nullptr; - } - /* * Request for an object to decode itself */ diff --git a/src/lib/asn1/ber_dec.h b/src/lib/asn1/ber_dec.h index ab39d985d..fbe62e464 100644 --- a/src/lib/asn1/ber_dec.h +++ b/src/lib/asn1/ber_dec.h @@ -198,12 +198,12 @@ class BOTAN_PUBLIC_API(2,0) BER_Decoder final explicit BER_Decoder(const std::vector& vec); BER_Decoder(const BER_Decoder&); - ~BER_Decoder(); private: BER_Decoder* m_parent; - DataSource* m_source; BER_Object m_pushed; - mutable bool m_owns; + // either m_data_src.get() or an unowned pointer + DataSource* m_source; + mutable std::unique_ptr m_data_src; }; /* -- cgit v1.2.3