Repository: randombit/botan
Pull request #2694 (2021-04-04 15:02:54)

Avoid returning raw pointers in mode padding impl


Patch:
diff --git a/src/lib/modes/cipher_mode.cpp b/src/lib/modes/cipher_mode.cpp
index 01ebc61bc..ee41ff1ad 100644
--- a/src/lib/modes/cipher_mode.cpp
+++ b/src/lib/modes/cipher_mode.cpp
@@ -147,7 +147,7 @@ std::unique_ptr<Cipher_Mode> Cipher_Mode::create(const std::string& algo,
          }
       else
          {
-         std::unique_ptr<BlockCipherModePaddingMethod> pad(get_bc_pad(padding));
+         auto pad = BlockCipherModePaddingMethod::create(padding);
 
          if(pad)
             {
diff --git a/src/lib/modes/mode_pad/mode_pad.cpp b/src/lib/modes/mode_pad/mode_pad.cpp
index 30a11eb91..4607d7317 100644
--- a/src/lib/modes/mode_pad/mode_pad.cpp
+++ b/src/lib/modes/mode_pad/mode_pad.cpp
@@ -15,22 +15,23 @@ namespace Botan {
 /**
 * Get a block cipher padding method by name
 */
-BlockCipherModePaddingMethod* get_bc_pad(const std::string& algo_spec)
+std::unique_ptr<BlockCipherModePaddingMethod>
+BlockCipherModePaddingMethod::create(const std::string& algo_spec)
    {
    if(algo_spec == "NoPadding")
-      return new Null_Padding;
+      return std::make_unique<Null_Padding>();
 
    if(algo_spec == "PKCS7")
-      return new PKCS7_Padding;
+      return std::make_unique<PKCS7_Padding>();
 
    if(algo_spec == "OneAndZeros")
-      return new OneAndZeros_Padding;
+      return std::make_unique<OneAndZeros_Padding>();
 
    if(algo_spec == "X9.23")
-      return new ANSI_X923_Padding;
+      return std::make_unique<ANSI_X923_Padding>();
 
    if(algo_spec == "ESP")
-      return new ESP_Padding;
+      return std::make_unique<ESP_Padding>();
 
    return nullptr;
    }
diff --git a/src/lib/modes/mode_pad/mode_pad.h b/src/lib/modes/mode_pad/mode_pad.h
index 2eeb6a97a..156c6b6f3 100644
--- a/src/lib/modes/mode_pad/mode_pad.h
+++ b/src/lib/modes/mode_pad/mode_pad.h
@@ -26,6 +26,12 @@ namespace Botan {
 class BOTAN_TEST_API BlockCipherModePaddingMethod
    {
    public:
+      /**
+      * Get a block cipher padding mode by name (eg "NoPadding" or "PKCS7")
+      * @param algo_spec block cipher padding mode name
+      */
+      static std::unique_ptr<BlockCipherModePaddingMethod> create(const std::string& algo_spec);
+
       /**
       * Add padding bytes to buffer.
       * @param buffer data to pad
@@ -147,12 +153,6 @@ class Null_Padding final : public BlockCipherModePaddingMethod
       std::string name() const override { return "NoPadding"; }
    };
 
-/**
-* Get a block cipher padding mode by name (eg "NoPadding" or "PKCS7")
-* @param algo_spec block cipher padding mode name
-*/
-BOTAN_TEST_API BlockCipherModePaddingMethod* get_bc_pad(const std::string& algo_spec);
-
 }
 
 #endif


Test patch:
diff --git a/src/tests/test_pad.cpp b/src/tests/test_pad.cpp
index 7b379ef2e..398c37885 100644
--- a/src/tests/test_pad.cpp
+++ b/src/tests/test_pad.cpp
@@ -39,7 +39,7 @@ class Cipher_Mode_Padding_Tests final : public Text_Based_Test
 
          Test::Result result(algo);
 
-         std::unique_ptr<Botan::BlockCipherModePaddingMethod> pad(Botan::get_bc_pad(algo));
+         auto pad = Botan::BlockCipherModePaddingMethod::create(algo);
 
          if(!pad)
             {