Skip to content
Closed
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 @@ -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)

Expand Down
15 changes: 9 additions & 6 deletions lib/vips/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions lib/vips/sourcecustom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
75 changes: 75 additions & 0 deletions spec/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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