From ee45da7befa4b5dcc28105df13b6248029dbf036 Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Fri, 22 Sep 2017 19:28:25 -0400 Subject: RAII for Power_Mod class --- src/lib/math/numbertheory/pow_mod.cpp | 33 +++++++++------------------------ src/lib/math/numbertheory/pow_mod.h | 4 ++-- 2 files changed, 11 insertions(+), 26 deletions(-) (limited to 'src/lib/math') diff --git a/src/lib/math/numbertheory/pow_mod.cpp b/src/lib/math/numbertheory/pow_mod.cpp index e4fed9925..1a03767b8 100644 --- a/src/lib/math/numbertheory/pow_mod.cpp +++ b/src/lib/math/numbertheory/pow_mod.cpp @@ -15,7 +15,6 @@ namespace Botan { */ Power_Mod::Power_Mod(const BigInt& n, Usage_Hints hints, bool disable_monty) { - m_core = nullptr; set_modulus(n, hints, disable_monty); } @@ -24,9 +23,8 @@ Power_Mod::Power_Mod(const BigInt& n, Usage_Hints hints, bool disable_monty) */ Power_Mod::Power_Mod(const Power_Mod& other) { - m_core = nullptr; - if(other.m_core) - m_core = other.m_core->copy(); + if(other.m_core.get()) + m_core.reset(other.m_core->copy()); } /* @@ -36,25 +34,14 @@ Power_Mod& Power_Mod::operator=(const Power_Mod& other) { if(this != &other) { - delete m_core; - m_core = nullptr; if(other.m_core) - { - m_core = other.m_core->copy(); - } + m_core.reset(other.m_core->copy()); + else + m_core.reset(); } return (*this); } -/* -* Power_Mod Destructor -*/ -Power_Mod::~Power_Mod() - { - delete m_core; - m_core = nullptr; - } - /* * Set the modulus */ @@ -62,16 +49,14 @@ void Power_Mod::set_modulus(const BigInt& n, Usage_Hints hints, bool disable_mon { // Allow set_modulus(0) to mean "drop old state" - delete m_core; - m_core = nullptr; + m_core.reset(); if(n != 0) { if(n.is_odd() && disable_monty == false) - m_core = new Montgomery_Exponentiator(n, hints); - - if(!m_core) - m_core = new Fixed_Window_Exponentiator(n, hints); + m_core.reset(new Montgomery_Exponentiator(n, hints)); + else + m_core.reset(new Fixed_Window_Exponentiator(n, hints)); } } diff --git a/src/lib/math/numbertheory/pow_mod.h b/src/lib/math/numbertheory/pow_mod.h index 7613c2ddf..cca99b714 100644 --- a/src/lib/math/numbertheory/pow_mod.h +++ b/src/lib/math/numbertheory/pow_mod.h @@ -93,9 +93,9 @@ class BOTAN_PUBLIC_API(2,0) Power_Mod Usage_Hints hints = NO_HINTS, bool disable_montgomery_arith = false); Power_Mod(const Power_Mod&); - virtual ~Power_Mod(); + virtual ~Power_Mod() {} private: - mutable Modular_Exponentiator* m_core; + mutable std::unique_ptr m_core; }; /** -- cgit v1.2.3