Commit 132d15c4c for imagemagick.org

commit 132d15c4c4442f0d44020939489e41e3383c34b9
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Wed Nov 25 20:14:42 2026 -0500

    base64() getter no longer leaks the encoded buffer on bad alloc

diff --git a/Magick++/lib/Blob.cpp b/Magick++/lib/Blob.cpp
index 1b39f24fc..63829df0e 100644
--- a/Magick++/lib/Blob.cpp
+++ b/Magick++/lib/Blob.cpp
@@ -35,12 +35,13 @@ Magick::Blob::Blob(const Magick::Blob& blob_)

 Magick::Blob::~Blob()
 {
+  // A destructor must never let an exception escape.
   try
   {
     if (_blobRef->decrease() == 0)
       delete _blobRef;
   }
-  catch(Magick::Exception&)
+  catch(...)
   {
   }

@@ -51,16 +52,23 @@ Magick::Blob& Magick::Blob::operator=(const Magick::Blob& blob_)
 {
   if (this != &blob_)
     {
+      BlobRef
+        *oldRef;
+
+      // Take the new reference and publish it before releasing the old
+      // one, so this object always points at a live BlobRef even if the
+      // release throws.
       blob_._blobRef->increase();
-      if (_blobRef->decrease() == 0)
-        delete _blobRef;
-
+      oldRef=_blobRef;
       _blobRef=blob_._blobRef;
+
+      if (oldRef->decrease() == 0)
+        delete oldRef;
     }
   return(*this);
 }

-void Magick::Blob::base64(const std::string base64_)
+void Magick::Blob::base64(const std::string& base64_)
 {
   size_t
     length;
@@ -68,11 +76,24 @@ void Magick::Blob::base64(const std::string base64_)
   unsigned char
     *decoded;

+  length=0;
   decoded=Base64Decode(base64_.c_str(),&length);

-  if(decoded)
+  if (decoded == (unsigned char *) NULL)
+    return;
+
+  // updateNoCopy() only throws before it takes ownership, so release the
+  // decoded buffer here if that happens.
+  try
+  {
     updateNoCopy(static_cast<void*>(decoded),length,
       Magick::Blob::MallocAllocator);
+  }
+  catch(...)
+  {
+    decoded=(unsigned char *) RelinquishMagickMemory(decoded);
+    throw;
+  }
 }

 std::string Magick::Blob::base64(void) const
@@ -86,18 +107,30 @@ std::string Magick::Blob::base64(void) const
   std::string
     result;

+  // Base64Encode() asserts on an empty or NULL buffer.
+  if ((data() == (const void *) NULL) || (length() == 0))
+    return(std::string());
+
   encoded_length=0;
   encoded=Base64Encode(static_cast<const unsigned char*>(data()),length(),
     &encoded_length);

-  if(encoded)
-    {
-      result=std::string(encoded,encoded_length);
-      encoded=(char *) RelinquishMagickMemory(encoded);
-      return result;
-    }
+  if (encoded == (char *) NULL)
+    return(std::string());
+
+  // Constructing the string can throw (bad_alloc); do not leak the buffer.
+  try
+  {
+    result.assign(encoded,encoded_length);
+  }
+  catch(...)
+  {
+    encoded=(char *) RelinquishMagickMemory(encoded);
+    throw;
+  }

-  return(std::string());
+  encoded=(char *) RelinquishMagickMemory(encoded);
+  return(result);
 }

 const void* Magick::Blob::data(void) const
@@ -137,6 +170,8 @@ void Magick::Blob::updateNoCopy(void* data_,size_t length_,

   // Allocate the replacement before releasing the current reference so
   // that a failed allocation leaves the Blob in its previous valid state.
+  // Ownership of data_ transfers only once newRef holds it, so on a throw
+  // here the caller still owns data_.
   newRef=new Magick::BlobRef((const void*) NULL,0);
   newRef->data=data_;
   newRef->length=length_;
diff --git a/Magick++/lib/Magick++/Blob.h b/Magick++/lib/Magick++/Blob.h
index f7ac4ad3a..852363d94 100644
--- a/Magick++/lib/Magick++/Blob.h
+++ b/Magick++/lib/Magick++/Blob.h
@@ -45,7 +45,8 @@ namespace Magick
     Blob& operator=(const Blob& blob_);

     // Update object contents from Base64-encoded string representation.
-    void base64(const std::string base64_);
+    // If the string cannot be decoded the object is left unchanged.
+    void base64(const std::string& base64_);
     // Return Base64-encoded string representation.
     std::string base64(void) const;

@@ -69,7 +70,8 @@ namespace Magick
     // otherwise modified after it has been supplied to this method.
     // Specify allocator_ as "MallocAllocator" if memory is allocated
     // via the C language malloc() function, or "NewAllocator" if
-    // memory is allocated via C++ 'new'.
+    // memory is allocated via C++ 'new'. If this method throws, ownership
+    // of the supplied pointer stays with the caller.
     void updateNoCopy(void* data_,size_t length_,
       Allocator allocator_=NewAllocator);