From 017abaf6948aa3b0d1d0cabcfa6a529694a8cafa Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 29 Sep 2016 16:25:12 +1300 Subject: [PATCH] Assert that new OpenSSL allocators succeed --- src/ecwrapper.cpp | 8 +++++--- src/test/bignum.h | 6 +++++- src/wallet/crypter.cpp | 2 ++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/ecwrapper.cpp b/src/ecwrapper.cpp index 73e8a8395..fcc7fb65b 100644 --- a/src/ecwrapper.cpp +++ b/src/ecwrapper.cpp @@ -178,11 +178,13 @@ bool CECKey::Recover(const uint256 &hash, const unsigned char *p64, int rec) { if (rec<0 || rec>=3) return false; - ECDSA_SIG *sig = ECDSA_SIG_new(); - BIGNUM *sig_r, *sig_s; - if (!(sig_r = BN_bin2bn(&p64[0], 32, nullptr)) || + ECDSA_SIG *sig = nullptr; + BIGNUM *sig_r = nullptr, *sig_s = nullptr; + if (!(sig = ECDSA_SIG_new()) || + !(sig_r = BN_bin2bn(&p64[0], 32, nullptr)) || !(sig_s = BN_bin2bn(&p64[32], 32, nullptr)) || !ECDSA_SIG_set0(sig, sig_r, sig_s)) { + ECDSA_SIG_free(sig); BN_free(sig_r); BN_free(sig_s); return false; diff --git a/src/test/bignum.h b/src/test/bignum.h index 94307985b..a7fb18953 100644 --- a/src/test/bignum.h +++ b/src/test/bignum.h @@ -7,6 +7,7 @@ #define BITCOIN_TEST_BIGNUM_H #include +#include #include #include #include @@ -30,11 +31,13 @@ public: CBigNum() { bn = BN_new(); + assert(bn); } CBigNum(const CBigNum& b) { bn = BN_new(); + assert(bn); if (!BN_copy(bn, b.bn)) { BN_clear_free(bn); @@ -54,11 +57,12 @@ public: BN_clear_free(bn); } - CBigNum(long long n) { bn = BN_new(); setint64(n); } + CBigNum(long long n) { bn = BN_new(); assert(bn); setint64(n); } explicit CBigNum(const std::vector& vch) { bn = BN_new(); + assert(bn); setvch(vch); } diff --git a/src/wallet/crypter.cpp b/src/wallet/crypter.cpp index 7c57550b7..71242258c 100644 --- a/src/wallet/crypter.cpp +++ b/src/wallet/crypter.cpp @@ -61,6 +61,7 @@ bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector& vchCiphertext, CKeyingM bool fOk = true; EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); + assert(ctx); if (fOk) fOk = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0; if (fOk) fOk = EVP_DecryptUpdate(ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0; if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0;