Skip to content

Commit

Permalink
Use smart pointer for digest context
Browse files Browse the repository at this point in the history
  • Loading branch information
omoerbeek committed Dec 15, 2021
1 parent 01498d8 commit b0b6e70
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
15 changes: 6 additions & 9 deletions pdns/sha.hh
Expand Up @@ -58,9 +58,9 @@ namespace pdns
class SHADigest
{
public:
SHADigest(unsigned int bits)
SHADigest(unsigned int bits) :
mdctx(std::unique_ptr<EVP_MD_CTX, void (*)(EVP_MD_CTX *)>(EVP_MD_CTX_new(), EVP_MD_CTX_free))
{
mdctx = EVP_MD_CTX_new();
if (mdctx == nullptr) {
throw std::runtime_error("SHADigest: EVP_MD_CTX_new failed");
}
Expand All @@ -77,22 +77,19 @@ public:
default:
throw std::runtime_error("SHADigest: unsupported size");
}
if (EVP_DigestInit_ex(mdctx, md, NULL) == 0) {
if (EVP_DigestInit_ex(mdctx.get(), md, NULL) == 0) {
throw std::runtime_error("SHADigest: init error");
}
}

~SHADigest()
{
// No free of md needed afaik
if (mdctx != nullptr) {
EVP_MD_CTX_free(mdctx);
}
}

void process(const std::string& msg)
{
if (EVP_DigestUpdate(mdctx, msg.data(), msg.size()) == 0) {
if (EVP_DigestUpdate(mdctx.get(), msg.data(), msg.size()) == 0) {
throw std::runtime_error("SHADigest: update error");
}
}
Expand All @@ -102,7 +99,7 @@ public:
std::string md_value;
md_value.resize(EVP_MD_size(md));
unsigned int md_len;
if (EVP_DigestFinal_ex(mdctx, reinterpret_cast<unsigned char*>(md_value.data()), &md_len) == 0) {
if (EVP_DigestFinal_ex(mdctx.get(), reinterpret_cast<unsigned char*>(md_value.data()), &md_len) == 0) {
throw std::runtime_error("SHADigest: finalize error");
}
if (md_len != md_value.size()) {
Expand All @@ -112,7 +109,7 @@ public:
}

private:
EVP_MD_CTX* mdctx{nullptr};
std::unique_ptr<EVP_MD_CTX, void (*)(EVP_MD_CTX *)> mdctx;
const EVP_MD* md;
};
}
6 changes: 2 additions & 4 deletions pdns/test-zonemd_cc.cc
Expand Up @@ -25,12 +25,10 @@ static void testZoneMD(const std::string& zone, const std::string& file, bool ex
pdns::zonemdVerify(z, zpt, validationDone, validationOK);
}
catch (const PDNSException& e) {
cerr << e.reason << endl;
BOOST_CHECK(ex);
BOOST_CHECK(ex);
}
catch (const std::exception& e) {
cerr << e.what() << endl;
BOOST_CHECK(ex);
BOOST_CHECK(ex);
}

BOOST_CHECK(validationDone == done);
Expand Down

0 comments on commit b0b6e70

Please sign in to comment.