Skip to content
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
3 changes: 3 additions & 0 deletions lib/fiddle/ffi_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,9 @@ def to_str(len = nil)
if len
ffi_ptr.read_string(len)
else
if @size < 0
raise ArgumentError.new("negative string size (or size too big)")
end
ffi_ptr.read_string(@size)
end
rescue FFI::NullPointerError
Expand Down
7 changes: 7 additions & 0 deletions test/fiddle/test_pointer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ def test_to_str

ptr.size = 0
assert_equal "", ptr.to_str
assert_equal "ello\0", (ptr + 1).to_str(5)
assert_raise(ArgumentError) do
(ptr + 1).to_str
end
end

def test_to_s
Expand All @@ -99,6 +103,8 @@ def test_to_s

ptr.size = 0
assert_equal "hello", ptr.to_s
assert_equal "ello\0", (ptr + 1).to_s(5)
assert_equal "ello", (ptr + 1).to_s
end

def test_minus
Expand Down Expand Up @@ -264,6 +270,7 @@ def test_size
end
assert_equal 0, Pointer.new(0).size
assert_equal 0, Pointer.new(0).ref.size
assert_equal -1, (Pointer.new(0) + 1).size
end

def test_size=
Expand Down
Loading