Skip to content

Commit

Permalink
If you call FFI::Pointer#== with an instance of an object that is not…
Browse files Browse the repository at this point in the history
… FFI::Pointer or descendants an exception is thrown - "INTERNAL ERROR!!! TypeError: wrong argument type Module (expected FFI::Pointer). In my case, this then causes the RubyMine debugger (debase) to blow up, making debugging harder.

This PR updates FFI::Pointer#== to return false if a pointer is compared to a non-pointer object, which is the expected behavior.
  • Loading branch information
cfis committed Mar 29, 2024
1 parent 808848d commit 934ad15
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion ext/ffi_c/Pointer.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,12 @@ ptr_equals(VALUE self, VALUE other)
return ptr->memory.address == NULL ? Qtrue : Qfalse;
}

return ptr->memory.address == POINTER(other)->address ? Qtrue : Qfalse;
if (!rb_obj_is_kind_of(other, rbffi_PointerClass)) {
return Qfalse;
}
else {
return ptr->memory.address == POINTER(other)->address ? Qtrue : Qfalse;
}
}

/*
Expand Down
13 changes: 13 additions & 0 deletions spec/ffi/pointer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ def to_ptr
expect(FFI::Pointer::NULL.to_ptr).to eq(FFI::Pointer::NULL)
end

it "equals itself" do
memory = FFI::MemoryPointer.new :pointer
expect(memory == memory).to be true
end

it "does not equal non pointers" do
memory = FFI::MemoryPointer.new :pointer
expect(memory == Hash.new).to be false
end

describe "pointer type methods" do

it "#read_pointer" do
Expand Down Expand Up @@ -150,6 +160,9 @@ def to_ptr
it 'returns true when compared with nil' do
expect((FFI::Pointer::NULL == nil)).to be true
end
it 'returns false when compared with a non-pointer object' do
expect((FFI::Pointer::NULL == Array.new)).to be false
end
it 'should not raise an error when attempting read/write zero length array' do
null_ptr = FFI::Pointer::NULL
expect( null_ptr.read_array_of_uint(0) ).to eq([])
Expand Down

0 comments on commit 934ad15

Please sign in to comment.