From: Christian Hofstaedtler Date: Wed, 24 Feb 2016 21:06:29 +0000 (+0100) Subject: Handle return codes from OpenSSL BIO_* X-Git-Tag: auth-4.0.0-alpha2~5^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F3279%2Fhead;p=thirdparty%2Fpdns.git Handle return codes from OpenSSL BIO_* --- diff --git a/pdns/base64.cc b/pdns/base64.cc index 17c8c8d74a..6a0ca12f68 100644 --- a/pdns/base64.cc +++ b/pdns/base64.cc @@ -2,6 +2,7 @@ #include "config.h" #endif #include "base64.hh" +#include #include #include #include @@ -12,8 +13,8 @@ int B64Decode(const std::string& src, std::string& dst) dst.clear(); return 0; } - size_t dlen = ( src.length() * 6 + 7 ) / 8 ; - size_t olen = 0; + int dlen = ( src.length() * 6 + 7 ) / 8 ; + ssize_t olen = 0; boost::scoped_array d( new unsigned char[dlen] ); BIO *bio, *b64; bio = BIO_new(BIO_s_mem()); @@ -22,15 +23,19 @@ int B64Decode(const std::string& src, std::string& dst) bio = BIO_push(b64, bio); BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); olen = BIO_read(b64, d.get(), dlen); + if ((olen == 0 || olen == -1) && BIO_should_retry(bio)) { + BIO_free_all(bio); + throw std::runtime_error("BIO_read failed to read all data from memory buffer"); + } BIO_free_all(bio); if (olen > 0) { - dst = std::string( (const char*) d.get(), olen ); + dst = std::string( reinterpret_cast(d.get()), olen ); return 0; } return -1; } -std::string Base64Encode (const std::string& src) +std::string Base64Encode(const std::string& src) { if (!src.empty()) { size_t olen = 0; @@ -39,8 +44,11 @@ std::string Base64Encode (const std::string& src) bio = BIO_new(BIO_s_mem()); bio = BIO_push(b64, bio); BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); - BIO_write(bio, src.c_str(), src.length()); - BIO_flush(bio); + if (BIO_write(bio, src.c_str(), src.length()) != src.length()) { + BIO_free_all(bio); + throw std::runtime_error("BIO_write failed to write all data to memory buffer"); + } + (void)BIO_flush(bio); char* pp; std::string out; olen = BIO_get_mem_data(bio, &pp);