From 083694ea3b418acfdc11e9849bee485707748b6e Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Thu, 17 Jul 2025 12:44:27 +0100 Subject: [PATCH 1/4] use `.null?` to test NULL pointer returns we were using `.nil?` in a couple of cases see https://github.com/libvips/ruby-vips/issues/419 thanks dloebl --- CHANGELOG.md | 2 ++ lib/vips/image.rb | 3 +-- lib/vips/interpolate.rb | 2 +- spec/image_spec.rb | 5 +++++ spec/samples/coffee.gif | Bin 0 -> 2100 bytes spec/vips_spec.rb | 5 +++++ 6 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 spec/samples/coffee.gif diff --git a/CHANGELOG.md b/CHANGELOG.md index 1eb1e61d..686cbe92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/lib/vips/image.rb b/lib/vips/image.rb index ea8b1af5..3283ab68 100644 --- a/lib/vips/image.rb +++ b/lib/vips/image.rb @@ -534,7 +534,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 @@ -700,7 +699,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) diff --git a/lib/vips/interpolate.rb b/lib/vips/interpolate.rb index e31450a1..891b1e59 100644 --- a/lib/vips/interpolate.rb +++ b/lib/vips/interpolate.rb @@ -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 diff --git a/spec/image_spec.rb b/spec/image_spec.rb index 38fdb29a..69ff2c32 100644 --- a/spec/image_spec.rb +++ b/spec/image_spec.rb @@ -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) diff --git a/spec/samples/coffee.gif b/spec/samples/coffee.gif new file mode 100644 index 0000000000000000000000000000000000000000..bf794f617a8124d3a9daaa0e1a5c80580e385b78 GIT binary patch literal 2100 zcma)+c{mde1IM=+3iT6ONE&j4km2Dym$- zf16T!V6Ym115o}Q_+1MCbF644we6a#{jWKW3TRi)LXR4v&*~dLWqRPFnP4j^xLrU0chChypZ#->@?tAc81_9DIMTO>oL5tP0dV`s?uIH`qfds@HK|p{ym<;`+p6@(&5Ss#*G6JRCqG*vg$v z%aAjQ34|}Z&@x?aXQZ4LruN3i+V`f|1mH6Yr5{7`%hYA*c?w~CvY+u#<6JJDM037x z^2jQ91>Qb%79d`E(c(sdsqm6{y4`zU4Z(WvoJfZQT??~O!kq?BH}b!dxi^56^TWrh z5E|5JLMX8Hoe(VA1Oyg*Vd(WtZ4!tP{xLPQ`^1%i4YnmqXaty|QvQJI_`z$J26fDY zmnMRqWD{!^@DpiH%LqFd@VfW#$!P6~Su~oxw@vrTX1N`o4%uEB zDybtLUi%p|F=3sDP5s&YW0C&L681GbXng1GaM&9GO2kh3R)_pqPT)80BaDd2^X`sM zAZr>-LH;IzfX$}?S=n$8K<=v;i>g~lWu;m3!XKqI)?t;J-K*$Fq{$YYwtSRu$Own(%b zbh`Ks#B{L4=G;C$PA;-y=>aao#53J%O|}}Ddr!gX#Ra!r&+w8PqLS5lny%7iFB(^p zYNa~G?`M*pg)Uc@CK%}gsXY7yaMyl`w+k&nVXm%|$IQnTnB@2Ed|Ak)Rf5rwoY{I2 z2#)Z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bd(<<~k05b>)(ho#-w)ft%+aE}29KSi1h7PNctZ}S^^K)R3J@0~u3q`Nezqa( z>~TqtAKUGHC?h)Jm+yC5UHhm%igusxt*+i&hhA%mJdpSXV}wK<81kNp0LwirM()Yd zhQQo%b5K741CPnLa>E3tgyct65~n5gc${M-(~VJ)9mQ~gP!TGmP)b?PV;Tj!BQFz+ zwi=(0PVdf1NgF-VoQ65D@wk6SXs~4fWh`#T=GN4vK!b1FdM;$cA6745?Vm0$;2aY| Jbisfte*>0#6fFP% literal 0 HcmV?d00001 diff --git a/spec/vips_spec.rb b/spec/vips_spec.rb index c569071b..39ff9cd1 100644 --- a/spec/vips_spec.rb +++ b/spec/vips_spec.rb @@ -193,5 +193,10 @@ 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 From 85c7c5a2ead2293c2ef075bf81a89d779a06b5ad Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Thu, 17 Jul 2025 12:48:25 +0100 Subject: [PATCH 2/4] fix a rcop fail --- spec/vips_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/vips_spec.rb b/spec/vips_spec.rb index 39ff9cd1..3b8c407d 100644 --- a/spec/vips_spec.rb +++ b/spec/vips_spec.rb @@ -197,6 +197,5 @@ it "can throw errors for bad interpolations" do expect { Vips::Interpolate.new "banana" }.to raise_exception(Vips::Error) end - end end From 923706f12ecfc20fbf2989d23bcaa2d14f43660d Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Thu, 17 Jul 2025 13:37:12 +0100 Subject: [PATCH 3/4] add a null check to matrix_from_array for safety --- lib/vips/image.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/vips/image.rb b/lib/vips/image.rb index 3283ab68..9ee303ac 100644 --- a/lib/vips/image.rb +++ b/lib/vips/image.rb @@ -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 From fc45f5d0b50f4e892c84450c7e8719fc9325e971 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Thu, 17 Jul 2025 13:48:56 +0100 Subject: [PATCH 4/4] and bump version --- VERSION | 2 +- lib/vips/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/VERSION b/VERSION index 530cdd91..21bb5e15 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.2.4 +2.2.5 diff --git a/lib/vips/version.rb b/lib/vips/version.rb index e00f5c8f..c7cc34f8 100644 --- a/lib/vips/version.rb +++ b/lib/vips/version.rb @@ -1,3 +1,3 @@ module Vips - VERSION = "2.2.4" + VERSION = "2.2.5" end