Skip to content

Commit cf89448

Browse files
raise Vips::Error if copy_memory fails (#443)
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
1 parent 88bdc7c commit cf89448

3 files changed

Lines changed: 17 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## master
44

5+
* raise Vips::Error if `copy_memory` fails [VitaliiLazebnyi]
6+
57
## Version 2.3.0 (2025-12-10)
68

79
* move library_name out of the global namespace and into FFI [jcupitt]

‎lib/vips/image.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,8 @@ def add_alpha
10181018
# @return [Image] new memory image
10191019
def copy_memory
10201020
new_image = Vips.vips_image_copy_memory self
1021+
raise Vips::Error if new_image.null?
1022+
10211023
Vips::Image.new new_image
10221024
end
10231025

‎spec/image_spec.rb‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@
8181
expect { img.write_to_memory }.to raise_exception(Vips::Error)
8282
end
8383

84+
it "can copy an image to memory" do
85+
image = (Vips::Image.black(16, 16) + 128).copy_memory
86+
87+
expect(image.width).to eq(16)
88+
expect(image.height).to eq(16)
89+
expect(image.avg).to eq(128)
90+
end
91+
92+
it "will raise VipsError if copy_memory fails" do
93+
img = Vips::Image.new_from_file simg("coffee.gif")
94+
expect { img.copy_memory }.to raise_exception(Vips::Error)
95+
end
96+
8497
it "throws an error when trying to load an image from memory with unknown size" do
8598
data = FFI::Pointer.new(1)
8699
expect { Vips::Image.new_from_memory(data, 16, 16, 1, :uchar) }.to raise_error(Vips::Error)

0 commit comments

Comments
 (0)