From cde842649d3ed048d687ad8e159c0d97315adca4 Mon Sep 17 00:00:00 2001 From: Vitalii Lazebnyi <18521734+VitaliiLazebnyi@users.noreply.github.com> Date: Thu, 15 Oct 2026 17:20:49 +0900 Subject: [PATCH] raise Vips::Error if copy_memory fails vips_image_copy_memory() returns NULL on error, for example when the source cannot be decoded. copy_memory wrapped that pointer without a check, so the next call on the returned image segfaulted. Fixes #442 --- CHANGELOG.md | 2 ++ lib/vips/image.rb | 2 ++ spec/image_spec.rb | 13 +++++++++++++ 3 files changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54c5b59..dcbe232 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## master +* raise Vips::Error if `copy_memory` fails [VitaliiLazebnyi] + ## Version 2.3.0 (2025-12-10) * move library_name out of the global namespace and into FFI [jcupitt] diff --git a/lib/vips/image.rb b/lib/vips/image.rb index 9e6206e..1a4347b 100644 --- a/lib/vips/image.rb +++ b/lib/vips/image.rb @@ -1018,6 +1018,8 @@ def add_alpha # @return [Image] new memory image def copy_memory new_image = Vips.vips_image_copy_memory self + raise Vips::Error if new_image.null? + Vips::Image.new new_image end diff --git a/spec/image_spec.rb b/spec/image_spec.rb index 7837d4c..f1eeff7 100644 --- a/spec/image_spec.rb +++ b/spec/image_spec.rb @@ -81,6 +81,19 @@ expect { img.write_to_memory }.to raise_exception(Vips::Error) end + it "can copy an image to memory" do + image = (Vips::Image.black(16, 16) + 128).copy_memory + + expect(image.width).to eq(16) + expect(image.height).to eq(16) + expect(image.avg).to eq(128) + end + + it "will raise VipsError if copy_memory fails" do + img = Vips::Image.new_from_file simg("coffee.gif") + expect { img.copy_memory }.to raise_exception(Vips::Error) + end + it "throws an error when trying to load an image from memory with unknown size" do data = FFI::Pointer.new(1) expect { Vips::Image.new_from_memory(data, 16, 16, 1, :uchar) }.to raise_error(Vips::Error)