Skip to content

Commit

Permalink
Fixes #3766. IO.copy_streams are not closing filename argument IO
Browse files Browse the repository at this point in the history
instances.

Solution is to close any open IO instances which were not passed in
as actual IO instances.
  • Loading branch information
enebo committed Mar 29, 2016
1 parent 2a7d6a9 commit 04b8191
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core/src/main/java/org/jruby/RubyIO.java
Expand Up @@ -4639,7 +4639,9 @@ public static IRubyObject copy_stream(ThreadContext context, IRubyObject recv,
RubyInteger length = null;
RubyInteger offset = null;

boolean io1FromIO = false;
RubyIO io1 = null;
boolean io2FromIO = false;
RubyIO io2 = null;

RubyString read = null;
Expand All @@ -4655,6 +4657,7 @@ public static IRubyObject copy_stream(ThreadContext context, IRubyObject recv,
if (arg1 instanceof RubyString) {
io1 = (RubyIO) RubyFile.open(context, runtime.getFile(), new IRubyObject[] {arg1}, Block.NULL_BLOCK);
} else if (arg1 instanceof RubyIO) {
io1FromIO = true;
io1 = (RubyIO) arg1;
} else if (arg1.respondsTo("to_path")) {
RubyString path = (RubyString) TypeConverter.convertToType19(arg1, runtime.getString(), "to_path");
Expand All @@ -4672,6 +4675,7 @@ public static IRubyObject copy_stream(ThreadContext context, IRubyObject recv,
if (arg2 instanceof RubyString) {
io2 = (RubyIO) RubyFile.open(context, runtime.getFile(), new IRubyObject[] {arg2, runtime.newString("w")}, Block.NULL_BLOCK);
} else if (arg2 instanceof RubyIO) {
io2FromIO = true;
io2 = (RubyIO) arg2;
} else if (arg2.respondsTo("to_path")) {
RubyString path = (RubyString) TypeConverter.convertToType19(arg2, runtime.getString(), "to_path");
Expand Down Expand Up @@ -4732,6 +4736,9 @@ public static IRubyObject copy_stream(ThreadContext context, IRubyObject recv,
}
} catch (BadDescriptorException e) {
throw runtime.newErrnoEBADFError();
} finally {
if (!io1FromIO && io1 != null && !io1.isClosed()) io1.ioClose(runtime);
if (!io2FromIO && io2 != null && !io2.isClosed()) io2.ioClose(runtime);
}
}

Expand Down

0 comments on commit 04b8191

Please sign in to comment.