Skip to content

use .null? to test NULL pointer returns #420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master

* improve NULL pointer handling [dloebl]

## Version 2.2.4 (2025-06-05)

* fix write to target test with libvips 8.17 [jcupitt]
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.4
2.2.5
8 changes: 4 additions & 4 deletions lib/vips/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,10 @@ def self.new_from_source source, option_string, **opts
def self.matrix_from_array width, height, array
ptr = FFI::MemoryPointer.new :double, array.length
ptr.write_array_of_double array
image = Vips.vips_image_new_matrix_from_array width, height,
img_ptr = Vips.vips_image_new_matrix_from_array width, height,
ptr, array.length
Vips::Image.new image
raise Vips::Error if img_ptr.null?
Vips::Image.new img_ptr
end

# Create a new Image from a 1D or 2D array. A 1D array becomes an
Expand Down Expand Up @@ -534,7 +535,6 @@ def self.new_from_array array, scale = 1, offset = 0
end

image = Vips::Image.matrix_from_array width, height, array
raise Vips::Error if image.nil?

image.mutate do |mutable|
# be careful to set them as double
Expand Down Expand Up @@ -700,7 +700,7 @@ def write_to_target target, format_string, **opts
def write_to_memory
len = Vips::SizeStruct.new
ptr = Vips.vips_image_write_to_memory self, len
raise Vips::Error if ptr.nil?
raise Vips::Error if ptr.null?

# wrap up as an autopointer
ptr = FFI::AutoPointer.new(ptr, GLib::G_FREE)
Expand Down
2 changes: 1 addition & 1 deletion lib/vips/interpolate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ManagedStruct < Vips::Object::ManagedStruct
def initialize name
name = name.to_s if name.is_a? Symbol
pointer = Vips.vips_interpolate_new name
raise Vips::Error if pointer.nil?
raise Vips::Error if pointer.null?

super(pointer)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/vips/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Vips
VERSION = "2.2.4"
VERSION = "2.2.5"
end
5 changes: 5 additions & 0 deletions spec/image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@
expect(x.avg).to eq(128)
end

it "will raise VipsError for invalid image files" do
img = Vips::Image.new_from_file simg("coffee.gif")
expect { img.write_to_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)
Expand Down
Binary file added spec/samples/coffee.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions spec/vips_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,9 @@

expect { black.crop(10, 10, 1, 1) }.to raise_exception(Vips::Error)
end

it "can throw errors for bad interpolations" do
expect { Vips::Interpolate.new "banana" }.to raise_exception(Vips::Error)
end
end
end