Repository: randombit/botan
Pull request #2685 (2021-04-03 14:53:09)

Remove use of explicit new operator in cli


Patch:
diff --git a/src/build-data/arch/riscv32.txt b/src/build-data/arch/riscv32.txt
index c8c258a00..ba88b42e9 100644
--- a/src/build-data/arch/riscv32.txt
+++ b/src/build-data/arch/riscv32.txt
@@ -1,2 +1,2 @@
-family riscv
+family riscv32
 endian little
diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp
index 35e6464cf..cdfe6226c 100644
--- a/src/cli/cli.cpp
+++ b/src/cli/cli.cpp
@@ -339,16 +339,15 @@ std::vector<std::string> Command::registered_cmds()
 //static
 std::unique_ptr<Command> Command::get_cmd(const std::string& name)
    {
-   const std::map<std::string, Command::cmd_maker_fn>& reg = Command::global_registry();
+   const auto& reg = Command::global_registry();
 
-   std::unique_ptr<Command> r;
    auto i = reg.find(name);
    if(i != reg.end())
       {
-      r.reset(i->second());
+      return i->second();
       }
 
-   return r;
+   return nullptr;
    }
 
 }
diff --git a/src/cli/cli.h b/src/cli/cli.h
index bf17e5d5b..c8aac22e3 100644
--- a/src/cli/cli.h
+++ b/src/cli/cli.h
@@ -184,7 +184,7 @@ class Command
       Botan::RandomNumberGenerator& rng();
 
    private:
-      typedef std::function<Command* ()> cmd_maker_fn;
+      typedef std::function<std::unique_ptr<Command> ()> cmd_maker_fn;
       static std::map<std::string, cmd_maker_fn>& global_registry();
 
       void parse_spec();
@@ -213,7 +213,7 @@ class Command
 
 #define BOTAN_REGISTER_COMMAND(name, CLI_Class)                          \
    Botan_CLI::Command::Registration reg_cmd_ ## CLI_Class(name,          \
-               []() -> Botan_CLI::Command* { return new CLI_Class; })
+     []() -> std::unique_ptr<Botan_CLI::Command> { return std::make_unique<CLI_Class>(); })
 
 }
 
diff --git a/src/cli/cli_rng.cpp b/src/cli/cli_rng.cpp
index 426bcff8b..064286d55 100644
--- a/src/cli/cli_rng.cpp
+++ b/src/cli/cli_rng.cpp
@@ -34,7 +34,7 @@ cli_make_rng(const std::string& rng_type, const std::string& hex_drbg_seed)
 #if defined(BOTAN_HAS_SYSTEM_RNG)
    if(rng_type == "system" || rng_type.empty())
       {
-      return std::unique_ptr<Botan::RandomNumberGenerator>(new Botan::System_RNG);
+      return std::make_unique<Botan::System_RNG>();
       }
 #endif
 
@@ -46,9 +46,9 @@ cli_make_rng(const std::string& rng_type, const std::string& hex_drbg_seed)
       std::unique_ptr<Botan::RandomNumberGenerator> rng;
 
       if(rng_type == "entropy")
-         rng.reset(new Botan::AutoSeeded_RNG(Botan::Entropy_Sources::global_sources()));
+         rng = std::make_unique<Botan::AutoSeeded_RNG>(Botan::Entropy_Sources::global_sources());
       else
-         rng.reset(new Botan::AutoSeeded_RNG);
+         rng = std::make_unique<Botan::AutoSeeded_RNG>();
 
       if(drbg_seed.size() > 0)
          rng->add_entropy(drbg_seed.data(), drbg_seed.size());
@@ -59,9 +59,8 @@ cli_make_rng(const std::string& rng_type, const std::string& hex_drbg_seed)
 #if defined(BOTAN_HAS_HMAC_DRBG) && defined(BOTAN_HAS_SHA2_32)
    if(rng_type == "drbg" || (rng_type.empty() && drbg_seed.empty() == false))
       {
-      std::unique_ptr<Botan::MessageAuthenticationCode> mac =
-         Botan::MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
-      std::unique_ptr<Botan::Stateful_RNG> rng(new Botan::HMAC_DRBG(std::move(mac)));
+      auto mac = Botan::MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
+      auto rng = std::make_unique<Botan::HMAC_DRBG>(std::move(mac));
       rng->add_entropy(drbg_seed.data(), drbg_seed.size());
 
       if(rng->is_seeded() == false)
@@ -69,7 +68,7 @@ cli_make_rng(const std::string& rng_type, const std::string& hex_drbg_seed)
                          std::to_string(rng->security_level()/8) +
                          " bytes must be provided");
 
-      return std::unique_ptr<Botan::RandomNumberGenerator>(rng.release());
+      return rng;
       }
 #endif
 
@@ -77,7 +76,7 @@ cli_make_rng(const std::string& rng_type, const std::string& hex_drbg_seed)
    if(rng_type == "rdrand" || rng_type == "cpu" || rng_type.empty())
       {
       if(Botan::Processor_RNG::available())
-         return std::unique_ptr<Botan::RandomNumberGenerator>(new Botan::Processor_RNG);
+         return std::make_unique<Botan::Processor_RNG>();
       else if(rng_type.empty() == false)
          throw CLI_Error("RNG instruction not supported on this processor");
       }
diff --git a/src/cli/roughtime.cpp b/src/cli/roughtime.cpp
index 11ac6990f..739b4a31b 100644
--- a/src/cli/roughtime.cpp
+++ b/src/cli/roughtime.cpp
@@ -156,11 +156,12 @@ class Roughtime final : public Command
             {
             try
                {
-               chain.reset(new Botan::Roughtime::Chain(slurp_file_as_str(chain_file)));
+               chain = std::make_unique<Botan::Roughtime::Chain>(slurp_file_as_str(chain_file));
                }
             catch(const CLI_IO_Error&)
                {
-               chain.reset(new Botan::Roughtime::Chain()); //file is to still be created
+               // file is to still be created
+               chain = std::make_unique<Botan::Roughtime::Chain>();
                }
             }
 
diff --git a/src/cli/speed.cpp b/src/cli/speed.cpp
index 4a22fb57f..6dbe82aba 100644
--- a/src/cli/speed.cpp
+++ b/src/cli/speed.cpp
@@ -450,9 +450,9 @@ class Speed final : public Command
             }
 
          if(format == "table")
-            m_summary.reset(new Summary);
+            m_summary = std::make_unique<Summary>();
          else if(format == "json")
-            m_json.reset(new JSON_Output);
+            m_json = std::make_unique<JSON_Output>();
          else if(format != "default")
             throw CLI_Usage_Error("Unknown --format type '" + format + "'");
 
@@ -876,9 +876,9 @@ class Speed final : public Command
                                         const std::string& provider = "",
                                         size_t buf_size = 0)
          {
-         return std::unique_ptr<Timer>(
-            new Timer(name, provider, what, event_mult, buf_size,
-                      m_clock_cycle_ratio, m_clock_speed));
+         return std::make_unique<Timer>(
+            name, provider, what, event_mult, buf_size,
+            m_clock_cycle_ratio, m_clock_speed);
          }
 
       std::unique_ptr<Timer> make_timer(const std::string& algo,
@@ -2103,10 +2103,10 @@ class Speed final : public Command
 
             std::unique_ptr<Timer> keygen_timer = make_timer(nm, provider, "keygen");
 
-            std::unique_ptr<Botan::Private_Key> key(keygen_timer->run([&]
+            std::unique_ptr<Botan::Private_Key> key = keygen_timer->run([&]
                {
-               return new Botan::McEliece_PrivateKey(rng(), n, t);
-               }));
+               return std::make_unique<Botan::McEliece_PrivateKey>(rng(), n, t);
+               });;
 
             record_result(keygen_timer);
             bench_pk_kem(*key, nm, provider, "KDF2(SHA-256)", msec);
diff --git a/src/cli/tls_helpers.h b/src/cli/tls_helpers.h
index 48a856c1a..f93c14719 100644
--- a/src/cli/tls_helpers.h
+++ b/src/cli/tls_helpers.h
@@ -195,48 +195,42 @@ class TLS_All_Policy final : public Botan::TLS::Policy
 
 inline std::unique_ptr<Botan::TLS::Policy> load_tls_policy(const std::string policy_type)
    {
-   std::unique_ptr<Botan::TLS::Policy> policy;
-
    if(policy_type == "default" || policy_type == "")
       {
-      policy.reset(new Botan::TLS::Policy);
+      return std::make_unique<Botan::TLS::Policy>();
       }
    else if(policy_type == "suiteb_128")
       {
-      policy.reset(new Botan::TLS::NSA_Suite_B_128);
+      return std::make_unique<Botan::TLS::NSA_Suite_B_128>();
       }
    else if(policy_type == "suiteb_192" || policy_type == "suiteb")
       {
-      policy.reset(new Botan::TLS::NSA_Suite_B_192);
+      return std::make_unique<Botan::TLS::NSA_Suite_B_192>();
       }
    else if(policy_type == "strict")
       {
-      policy.reset(new Botan::TLS::Strict_Policy);
+      return std::make_unique<Botan::TLS::Strict_Policy>();
       }
    else if(policy_type == "bsi")
       {
-      policy.reset(new Botan::TLS::BSI_TR_02102_2);
+      return std::make_unique<Botan::TLS::BSI_TR_02102_2>();
       }
    else if(policy_type == "datagram")
       {
-      policy.reset(new Botan::TLS::Strict_Policy);
+      return std::make_unique<Botan::TLS::Strict_Policy>();
       }
    else if(policy_type == "all" || policy_type == "everything")
       {
-      policy.reset(new TLS_All_Policy);
+      return std::make_unique<TLS_All_Policy>();
       }
-   else
+
+   // if something we don't recognize, assume it's a file
+   std::ifstream policy_stream(policy_type);
+   if(!policy_stream.good())
       {
-      // assume it's a file
-      std::ifstream policy_stream(policy_type);
-      if(!policy_stream.good())
-         {
-         throw Botan_CLI::CLI_Usage_Error("Unknown TLS policy: not a file or known short name");
-         }
-      policy.reset(new Botan::TLS::Text_Policy(policy_stream));
+      throw Botan_CLI::CLI_Usage_Error("Unknown TLS policy: not a file or known short name");
       }
-
-   return policy;
+   return std::make_unique<Botan::TLS::Text_Policy>(policy_stream);
    }
 
 #endif
diff --git a/src/cli/tls_http_server.cpp b/src/cli/tls_http_server.cpp
index 6ce30312f..2d5bfa75e 100644
--- a/src/cli/tls_http_server.cpp
+++ b/src/cli/tls_http_server.cpp
@@ -181,7 +181,7 @@ class TLS_Asio_HTTP_Session final : public std::enable_shared_from_this<TLS_Asio
          Botan::Credentials_Manager& credentials,
          Botan::TLS::Policy& policy)
          {
-         return pointer(new TLS_Asio_HTTP_Session(io, session_manager, credentials, policy));
+         return std::make_shared<TLS_Asio_HTTP_Session>(io, session_manager, credentials, policy);
          }
 
       tcp::socket& client_socket()
@@ -200,7 +200,6 @@ class TLS_Asio_HTTP_Session final : public std::enable_shared_from_this<TLS_Asio
          m_tls.close();
          }
 
-   private:
       TLS_Asio_HTTP_Session(boost::asio::io_service& io,
                             Botan::TLS::Session_Manager& session_manager,
                             Botan::Credentials_Manager& credentials,
@@ -210,6 +209,7 @@ class TLS_Asio_HTTP_Session final : public std::enable_shared_from_this<TLS_Asio
          , m_rng(cli_make_rng())
          , m_tls(*this, session_manager, credentials, policy, *m_rng) {}
 
+   private:
       void client_read(const boost::system::error_code& error,
                        size_t bytes_transferred)
          {
diff --git a/src/cli/tls_proxy.cpp b/src/cli/tls_proxy.cpp
index 686c01ba8..c1d1136cb 100644
--- a/src/cli/tls_proxy.cpp
+++ b/src/cli/tls_proxy.cpp
@@ -104,14 +104,13 @@ class tls_proxy_session final : public std::enable_shared_from_this<tls_proxy_se
          Botan::TLS::Policy& policy,
          tcp::resolver::iterator endpoints)
          {
-         return pointer(
-                   new tls_proxy_session(
-                      io,
-                      session_manager,
-                      credentials,
-                      policy,
-                      endpoints)
-                );
+         return std::make_shared<tls_proxy_session>(
+            io,
+            session_manager,
+            credentials,
+            policy,
+            endpoints
+            );
          }
 
       tcp::socket& client_socket()
@@ -139,7 +138,6 @@ class tls_proxy_session final : public std::enable_shared_from_this<tls_proxy_se
             }
          }
 
-   private:
       tls_proxy_session(
          boost::asio::io_service& io,
          Botan::TLS::Session_Manager& session_manager,
@@ -157,6 +155,7 @@ class tls_proxy_session final : public std::enable_shared_from_this<tls_proxy_se
                  policy,
                  *m_rng) {}
 
+   private:
       void client_read(const boost::system::error_code& error,
                        size_t bytes_transferred)
          {


Test patch:
diff --git a/src/cli/timing_tests.cpp b/src/cli/timing_tests.cpp
index 0e3cac4f5..e3c28a633 100644
--- a/src/cli/timing_tests.cpp
+++ b/src/cli/timing_tests.cpp
@@ -554,57 +554,57 @@ std::unique_ptr<Timing_Test> Timing_Test_Command::lookup_timing_test(const std::
 #if defined(BOTAN_HAS_RSA) && defined(BOTAN_HAS_EME_PKCS1) && defined(BOTAN_HAS_EME_RAW)
    if(test_type == "bleichenbacher")
       {
-      return std::unique_ptr<Timing_Test>(new Bleichenbacker_Timing_Test(2048));
+      return std::make_unique<Bleichenbacker_Timing_Test>(2048);
       }
 #endif
 
 #if defined(BOTAN_HAS_RSA) && defined(BOTAN_HAS_EME_OAEP) && defined(BOTAN_HAS_EME_RAW)
    if(test_type == "manger")
       {
-      return std::unique_ptr<Timing_Test>(new Manger_Timing_Test(2048));
+      return std::make_unique<Manger_Timing_Test>(2048);
       }
 #endif
 
 #if defined(BOTAN_HAS_ECDSA)
    if(test_type == "ecdsa")
       {
-      return std::unique_ptr<Timing_Test>(new ECDSA_Timing_Test("secp384r1"));
+      return std::make_unique<ECDSA_Timing_Test>("secp384r1");
       }
 #endif
 
 #if defined(BOTAN_HAS_ECC_GROUP)
    if(test_type == "ecc_mul")
       {
-      return std::unique_ptr<Timing_Test>(new ECC_Mul_Timing_Test("brainpool512r1"));
+      return std::make_unique<ECC_Mul_Timing_Test>("brainpool512r1");
       }
 #endif
 
 #if defined(BOTAN_HAS_NUMBERTHEORY)
    if(test_type == "inverse_mod")
       {
-      return std::unique_ptr<Timing_Test>(new Invmod_Timing_Test(512));
+      return std::make_unique<Invmod_Timing_Test>(512);
       }
 #endif
 
 #if defined(BOTAN_HAS_DL_GROUP)
    if(test_type == "pow_mod")
       {
-      return std::unique_ptr<Timing_Test>(new Powmod_Timing_Test("modp/ietf/1024"));
+      return std::make_unique<Powmod_Timing_Test>("modp/ietf/1024");
       }
 #endif
 
 #if defined(BOTAN_HAS_TLS_CBC)
    if(test_type == "lucky13sec3" || test_type == "lucky13sec4sha1")
       {
-      return std::unique_ptr<Timing_Test>(new Lucky13_Timing_Test("SHA-1", 20));
+      return std::make_unique<Lucky13_Timing_Test>("SHA-1", 20);
       }
    if(test_type == "lucky13sec4sha256")
       {
-      return std::unique_ptr<Timing_Test>(new Lucky13_Timing_Test("SHA-256", 32));
+      return std::make_unique<Lucky13_Timing_Test>("SHA-256", 32);
       }
    if(test_type == "lucky13sec4sha384")
       {
-      return std::unique_ptr<Timing_Test>(new Lucky13_Timing_Test("SHA-384", 48));
+      return std::make_unique<Lucky13_Timing_Test>("SHA-384", 48);
       }
 #endif