aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/asn1
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-09-22 19:53:20 -0400
committerJack Lloyd <[email protected]>2017-09-22 19:53:20 -0400
commitd7330e586aee1912838952c6583d9449060d776a (patch)
tree1374bd3d400ec35055285ed24ea44623ca0db426 /src/lib/asn1
parentde581bbf6dfc1fadc0755925300d047f392bf986 (diff)
Refactor to avoid explicit delete in BER_Decoder
Diffstat (limited to 'src/lib/asn1')
-rw-r--r--src/lib/asn1/ber_dec.cpp32
-rw-r--r--src/lib/asn1/ber_dec.h6
2 files changed, 12 insertions, 26 deletions
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<uint8_t>& 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<uint8_t>& data)
*/
BER_Decoder::BER_Decoder(const std::vector<uint8_t>& 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,27 +332,14 @@ BER_Decoder::BER_Decoder(const std::vector<uint8_t>& 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
*/
BER_Decoder& BER_Decoder::decode(ASN1_Object& obj,
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<uint8_t>& 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<DataSource> m_data_src;
};
/*