diff --git a/CHANGELOG.md b/CHANGELOG.md index aa13362..e6b2105 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ * raise Vips::Error if `copy_memory` fails [VitaliiLazebnyi] * `Source.new_from_memory` keeps a reference to the memory area [jeremy] * release the GVL during `copy_memory` and `write_to_memory` [andrewfader] +* exceptions in custom source and target callbacks are reported to libvips as errors rather than as end of file, and printed to stderr rather than stdout [janko] +* fix `SourceCustom#on_read` printing "read: unexpected return" at end of file, failing on frozen strings, and overflowing the buffer when the block returns more bytes than requested [janko] ## Version 2.3.0 (2025-12-10) diff --git a/lib/vips/object.rb b/lib/vips/object.rb index 4ac23ab..f6864ec 100644 --- a/lib/vips/object.rb +++ b/lib/vips/object.rb @@ -57,7 +57,10 @@ class Progress < FFI::Struct # ruby-ffi makes it hard to use the g_signal_connect user data param # to pass the function pointer through, unfortunately. # - # We can't throw exceptions across C, so we must catch everything. + # We can't throw exceptions across C, so we must catch everything. Read and + # write callbacks then report an error to libvips, rather than EOF or a + # zero-length write, so that the operation can fail instead of silently + # producing a truncated image. MARSHAL_PROGRESS = proc do |handler| FFI::Function.new(:void, [:pointer, :pointer, :pointer]) do |vi, prog, cb| @@ -71,8 +74,8 @@ class Progress < FFI::Struct begin result = handler.call(p, len) rescue Exception => e - puts "read: #{e}" - result = 0 + warn "read: #{e}" + result = -1 end result @@ -84,7 +87,7 @@ class Progress < FFI::Struct begin result = handler.call(off, whence) rescue Exception => e - puts "seek: #{e}" + warn "seek: #{e}" result = -1 end @@ -97,8 +100,8 @@ class Progress < FFI::Struct begin result = handler.call(p, len) rescue Exception => e - puts "write: #{e}" - result = 0 + warn "write: #{e}" + result = -1 end result diff --git a/lib/vips/sourcecustom.rb b/lib/vips/sourcecustom.rb index 9e1c5e7..0dccff7 100644 --- a/lib/vips/sourcecustom.rb +++ b/lib/vips/sourcecustom.rb @@ -60,10 +60,13 @@ def initialize def on_read &block signal_connect "read" do |buf, len| chunk = block.call len - return 0 if chunk.nil? + next 0 if chunk.nil? bytes_read = chunk.bytesize + if bytes_read > len + raise Vips::Error, "on_read returned #{bytes_read} bytes, but only #{len} were requested" + end buf.put_bytes(0, chunk, 0, bytes_read) - chunk.clear + chunk.clear unless chunk.frozen? bytes_read end diff --git a/spec/connection_spec.rb b/spec/connection_spec.rb index 35f3c25..94a67c2 100644 --- a/spec/connection_spec.rb +++ b/spec/connection_spec.rb @@ -185,3 +185,78 @@ expect(image2.avg).to eq(image.avg) end end + +RSpec.describe Vips::SourceCustom, version: [8, 9] do + it "doesn't print anything when on_read reaches the end" do + file = File.open simg("wagon.jpg"), "rb" + source = Vips::SourceCustom.new + source.on_read { |length| file.read length } + + expect { + image = Vips::Image.new_from_source source, "" + image.avg + }.not_to output.to_stdout + end + + it "accepts frozen strings from on_read" do + file = File.open simg("wagon.jpg"), "rb" + source = Vips::SourceCustom.new + source.on_read do |length| + chunk = file.read length + chunk.freeze if chunk + end + image = Vips::Image.new_from_source source, "" + + expect(image.avg).to be_within(0.001).of(109.789) + end + + # with the default fail_on, the jpeg and png loaders treat read errors as a + # truncated file, so use a loader that doesn't + it "fails when on_read raises" do + tiff = Vips::Image.new_from_file(simg("wagon.jpg")).write_to_buffer(".tif") + file = StringIO.new(tiff) + source = Vips::SourceCustom.new + source.on_read do |length| + raise IOError, "connection reset" if file.pos > 10_000 + file.read length + end + + expect { + image = Vips::Image.new_from_source source, "", access: :sequential + image.avg + }.to raise_error(Vips::Error).and output(/read: connection reset/).to_stderr + end + + it "fails when on_read raises and fail_on is set", version: [8, 12] do + file = File.open simg("wagon.jpg"), "rb" + source = Vips::SourceCustom.new + source.on_read do |length| + raise IOError, "connection reset" if file.pos > 10_000 + file.read length + end + + expect { + image = Vips::Image.new_from_source source, "", access: :sequential, fail_on: :truncated + image.avg + }.to raise_error(Vips::Error).and output(/read: connection reset/).to_stderr + end + + it "fails when on_read returns more bytes than requested" do + source = Vips::SourceCustom.new + source.on_read { |length| "x" * (length + 1) } + + expect { + Vips::Image.new_from_source source, "" + }.to raise_error(Vips::Error).and output(/only \d+ were requested/).to_stderr + end + + it "fails with the exception raised in on_write" do + image = Vips::Image.new_from_file simg("wagon.jpg") + target = Vips::TargetCustom.new + target.on_write { |chunk| raise IOError, "disk full" } + + expect { + image.write_to_target target, ".png" + }.to raise_error(Vips::Error).and output(/write: disk full/).to_stderr + end +end