Skip to content

Commit

Permalink
Fix specs for rb_io_open_descriptor() to prevent file descriptors lea…
Browse files Browse the repository at this point in the history
…king
  • Loading branch information
andrykonchin committed Dec 9, 2024
1 parent e10ee00 commit 8ff5cfd
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions optional/capi/io_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -579,11 +579,13 @@
end

it "does not apply the specified encoding flags" do
File.write("a.txt", "123\r\n456\n89")
file = File.open("a.txt", "r")
File.write(@name, "123\r\n456\n89")
file = File.open(@name, "r")

io = @o.rb_io_open_descriptor(File, file.fileno, CApiIOSpecs::FMODE_READABLE, "a.txt", 60, "US-ASCII", "UTF-8", CApiIOSpecs::ECONV_UNIVERSAL_NEWLINE_DECORATOR, {})
io.read_nonblock(20).should == "123\r\n456\n89"
ensure
file.close
end

it "ignores the IO open options" do
Expand Down Expand Up @@ -623,7 +625,13 @@
io = @o.rb_io_open_descriptor(File, @w_io.fileno, CApiIOSpecs::FMODE_READABLE, "a.txt", 60, "US-ASCII", "UTF-8", 0, {})
io.should.is_a?(File)

-> { io.read_nonblock(1) }.should raise_error(Errno::EBADF)
platform_is_not :windows do
-> { io.read_nonblock(1) }.should raise_error(Errno::EBADF)
end

platform_is :windows do
-> { io.read_nonblock(1) }.should raise_error(IO::EWOULDBLOCKWaitReadable)
end
end

it "tolerates NULL as rb_io_encoding *encoding parameter" do
Expand Down

0 comments on commit 8ff5cfd

Please sign in to comment.