Skip to content

Commit

Permalink
Add spec and fix exception for Fiddle::Handle.new with a missing library
Browse files Browse the repository at this point in the history
* Fixes #2714
  • Loading branch information
eregon committed Sep 2, 2022
1 parent 7b23568 commit 3fd4d3a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Compatibility:
* Implement `rb_eval_cmd_kw` to support the `tk` gem (#2556, @aardvark179).
* Fix `rb_class2name` to call `inspect` on anonymous classes like in CRuby (#2701, @aardvark179).
* Implement `rb_ivar_foreach` to iterate over instance and class variables like in CRuby (#2701, @aardvark179).
* Fix exception for `Fiddle::Handle.new` with a missing library (#2714, @eregon).

Performance:

Expand Down
7 changes: 4 additions & 3 deletions lib/truffle/truffle/fiddle_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,16 @@ def self.[](*args)
RTLD_GLOBAL = Truffle::Config['platform.dlopen.RTLD_GLOBAL']

def initialize(library = nil, flags = RTLD_LAZY | RTLD_GLOBAL)
raise DLError, 'unsupported dlopen flags' if flags != RTLD_LAZY | RTLD_GLOBAL
raise DLError, 'unsupported dlopen flags' if flags != (RTLD_LAZY | RTLD_GLOBAL)

if library == Truffle::FiddleBackend::RTLD_NEXT
@handle = :rtld_next
else
library = nil if library == Truffle::FiddleBackend::RTLD_DEFAULT
begin
@handle = Primitive.interop_eval_nfi(library ? "load '#{library}'" : 'default')
rescue RuntimeError
raise DLError, "#{library}: cannot open shared object file: No such file or directory"
rescue Polyglot::ForeignException => e
raise DLError, "#{e.message}"
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/ruby/library/fiddle/handle/initialize_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require_relative '../../../spec_helper'
require 'fiddle'

describe "Fiddle::Handle#initialize" do
it "raises Fiddle::DLError if the library cannot be found" do
-> {
Fiddle::Handle.new("doesnotexist.doesnotexist")
}.should raise_error(Fiddle::DLError)
end
end

0 comments on commit 3fd4d3a

Please sign in to comment.