Commit bdbf404a5 for imagemagick.org
commit bdbf404a5a6f0fe7ef520e5744140a4bf5123796
Author: Cristy <urban-warrior@imagemagick.org>
Date: Mon Nov 23 14:32:32 2026 -0500
https://github.com/ImageMagick/ImageMagick/issues/9144
diff --git a/Magick++/lib/Blob.cpp b/Magick++/lib/Blob.cpp
index d5474f49a..1b39f24fc 100644
--- a/Magick++/lib/Blob.cpp
+++ b/Magick++/lib/Blob.cpp
@@ -112,21 +112,39 @@ size_t Magick::Blob::length(void) const
void Magick::Blob::update(const void* data_,size_t length_)
{
- if (_blobRef->decrease() == 0)
- delete _blobRef;
-
- _blobRef=new Magick::BlobRef(data_,length_);
+ BlobRef
+ *newRef,
+ *oldRef;
+
+ // Build the replacement first. data_ may alias the buffer owned by the
+ // current BlobRef (e.g. blob.update(blob.data(),blob.length())), so the
+ // old buffer must stay alive until the copy is complete. If the
+ // allocation throws, _blobRef is untouched and the Blob remains valid.
+ newRef=new Magick::BlobRef(data_,length_);
+ oldRef=_blobRef;
+ _blobRef=newRef;
+
+ if (oldRef->decrease() == 0)
+ delete oldRef;
}
void Magick::Blob::updateNoCopy(void* data_,size_t length_,
Magick::Blob::Allocator allocator_)
{
- if (_blobRef->decrease() == 0)
- delete _blobRef;
-
- _blobRef=new Magick::BlobRef((const void*) NULL,0);
- _blobRef->data=data_;
- _blobRef->length=length_;
- _blobRef->allocator=allocator_;
+ BlobRef
+ *newRef,
+ *oldRef;
+
+ // Allocate the replacement before releasing the current reference so
+ // that a failed allocation leaves the Blob in its previous valid state.
+ newRef=new Magick::BlobRef((const void*) NULL,0);
+ newRef->data=data_;
+ newRef->length=length_;
+ newRef->allocator=allocator_;
+
+ oldRef=_blobRef;
+ _blobRef=newRef;
+
+ if (oldRef->decrease() == 0)
+ delete oldRef;
}
-