代码之家  ›  专栏  ›  技术社区  ›  Dark Sorrow

CryptoPP::StreamTransformationFilter中可能存在内存泄漏

  •  0
  • Dark Sorrow  · 技术社区  · 4 年前

    https://www.cryptopp.com/wiki/StreamTransformationFilter https://www.cryptopp.com/wiki/CBC_Mode

    排队 StringSource string_source(encypted_data, true, new StreamTransformationFilter(decryption, new StringSink(plain_data))); new 运算符;但是我们不会删除它们。它不应该导致内存泄漏,因为他们没有删除操作。

    我要换吗

    StringSource string_source(plain_data, true, new StreamTransformationFilter(encryption, new StringSink(encypted_data)));
    

    使用以下代码

    try
    {
        CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption   encryption(this->aes_key.data(), this->aes_key.size(), this->aes_iv.data(), this->aes_iv.size())
        CryptoPP::StringSink                            string_sink(encypted_data);
        CryptoPP::StreamTransformationFilter            stf_encryptor(encryption, string_sink);
        CryptoPP::StringSource                          string_source(plain_data, true, stf_encryptor);
    }
    

    CryptoPP::StringSink , CryptoPP::StreamTransformationFilter CryptoPP::StringSource

    课程:

    std::optional<std::string> Cryptography::decrypt_data(const std::string& encypted_data)
    {
        std::optional<std::string> plain_data { std::nullopt };
        try
        {
            CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption   decryption(this->aes_key.data(), this->aes_key.size(), this->aes_iv.data(), this->aes_iv.size())
            StringSource string_source(encypted_data, true, new StreamTransformationFilter(decryption, new StringSink(plain_data)));
        }
        catch(const CryptoPP::Exception& e)
        {
    #ifdef _DEBUG
            spdlog::error("CryptoPP Exception in Cryptography::decrypt_data : {}", ex.what());
    #endif
            PLOG_ERROR << ex.what();
        }
        catch(const std::exception& ex)
        {
    #ifdef _DEBUG
            spdlog::error("Exception in Cryptography::decrypt_data : {}", ex.what());
    #endif
            PLOG_ERROR << ex.what();
        }
        return plain_data;
    }
    
    0 回复  |  直到 4 年前
        1
  •  1
  •   Alan Birtles    4 年前

    当您将筛选器/接收器传递给文件服务器/源的构造函数时,它将获得指针的所有权,并在销毁时将其删除。

    在你的例子中 StringSource 删除 StreamTransformationFilter StringSink