Skip to content

Commit

Permalink
Bugfix: Fix memory errors due to smart pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
dr8co committed Feb 24, 2024
1 parent f71fa70 commit 92d9764
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 33 deletions.
32 changes: 13 additions & 19 deletions src/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,27 @@ import secureAllocator;
/// \param encodedData Base64 encoded string.
/// \return a vector of the decoded binary data.
std::vector<unsigned char> base64Decode(const std::string &encodedData) {
// Allocate a buffer for the decoded data (the size of the decoded data is always less than the size of the encoded data)
std::vector<unsigned char> decodedData(encodedData.size());

// Custom deleter for BIO objects
auto bioDeleter = [](BIO *bio) -> void { BIO_free_all(bio); };

// Create a BIO object to decode the data
const std::unique_ptr<BIO, decltype(bioDeleter)> b64(BIO_new(BIO_f_base64()), bioDeleter);
std::unique_ptr<BIO, decltype(&BIO_free_all)> bio(
BIO_new_mem_buf(encodedData.data(), static_cast<int>(encodedData.size())), &BIO_free_all);
if (bio == nullptr)
throw std::bad_alloc(); // Memory allocation failed

// Don't use newlines to flush buffer
BIO_set_flags(b64.get(), BIO_FLAGS_BASE64_NO_NL);
// Create a base64 BIO
BIO *b64 = BIO_new(BIO_f_base64());
if (b64 == nullptr)
throw std::bad_alloc(); // Memory allocation failed

// Create a memory BIO to store the encoded data
std::unique_ptr<BIO, decltype(bioDeleter)> bio(
BIO_new_mem_buf(encodedData.data(), static_cast<int>(encodedData.size())), bioDeleter);
// Don't use newlines to flush buffer
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);

// Check if memory allocation failed
if (b64 == nullptr || bio == nullptr)
throw std::bad_alloc();
// Push the base64 BIO to the memory BIO
bio.reset(BIO_push(b64, bio.release())); // Transfer ownership to bio

// Push the memory BIO to the base64 BIO
bio.reset(BIO_push(b64.get(), bio.get()));
std::vector<unsigned char> decodedData(encodedData.size());

// Decode the data
const int len = BIO_read(bio.get(), decodedData.data(), static_cast<int>(decodedData.size()));

// Check if the decoding failed
if (len < 0)
throw std::runtime_error("BIO_read() failed.");

Expand Down
29 changes: 15 additions & 14 deletions src/utils/utils.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -78,33 +78,34 @@ export {
/// \param input a vector of the binary data to be encoded.
/// \return Base64-encoded string.
std::string base64Encode(const uCharVector auto &input) {
// Custom deleter for BIO objects
auto bioDeleter = [](BIO *bio) { BIO_free_all(bio); };

// Create a BIO object to encode the data
std::unique_ptr<BIO, decltype(bioDeleter)> b64(BIO_new(BIO_f_base64()), bioDeleter);

// Don't use newlines to flush buffer
BIO_set_flags(b64.get(), BIO_FLAGS_BASE64_NO_NL);
const std::unique_ptr<BIO, decltype(&BIO_free_all)> b64(BIO_new(BIO_f_base64()), &BIO_free_all);
if (b64 == nullptr)
throw std::bad_alloc(); // Memory allocation failed

// Create a memory BIO to store the encoded data
std::unique_ptr<BIO, decltype(bioDeleter)> bio(BIO_new(BIO_s_mem()), bioDeleter);
BIO *bio = BIO_new(BIO_s_mem());
if (bio == nullptr)
throw std::bad_alloc(); // Memory allocation failed

// Check if memory allocation failed
if (b64 == nullptr || bio == nullptr)
throw std::bad_alloc();
// Don't use newlines to flush buffer
BIO_set_flags(b64.get(), BIO_FLAGS_BASE64_NO_NL);

// Push the memory BIO to the base64 BIO
bio.reset(BIO_push(b64.get(), bio.release()));
bio = BIO_push(b64.get(), bio); // Transfer ownership to b64

if (BIO_write(bio.get(), input.data(), static_cast<int>(input.size())) < 0)
// Write the data to the BIO
if (BIO_write(bio, input.data(), static_cast<int>(input.size())) < 0)
throw std::runtime_error("BIO_write() failed.");

BIO_flush(bio.get());
// Flush the BIO
BIO_flush(bio);

// Get the pointer to the BIO's data
BUF_MEM *bufferPtr;
BIO_get_mem_ptr(b64.get(), &bufferPtr);

// Create a string from the data
std::string encodedData(bufferPtr->data, bufferPtr->length);

return encodedData;
Expand Down

0 comments on commit 92d9764

Please sign in to comment.