summaryrefslogtreecommitdiff
path: root/io.c
diff options
context:
space:
mode:
authorusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-01-08 07:47:12 +0000
committerusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-01-08 07:47:12 +0000
commitef6e53635ab580214694a72f60133452f66a8827 (patch)
treeab26f22ef6775d50ecce4e4667d9833265a3e298 /io.c
parent902b1094655145946e6432f24d0f275123d59c26 (diff)
* io.c (rb_io_s_read): close the IO if an exception is raised on
seeking. [ruby-core:27429] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26250 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'io.c')
-rw-r--r--io.c25
1 files changed, 23 insertions, 2 deletions
diff --git a/io.c b/io.c
index 3b8bd2566f..5d4752a177 100644
--- a/io.c
+++ b/io.c
@@ -7816,6 +7816,19 @@ io_s_read(struct foreach_arg *arg)
return io_read(arg->argc, arg->argv, arg->io);
}
+struct seek_arg {
+ VALUE io;
+ VALUE offset;
+ int mode;
+};
+
+static VALUE
+seek_before_read(struct seek_arg *arg)
+{
+ rb_io_binmode(arg->io);
+ return rb_io_seek(arg->io, arg->offset, arg->mode);
+}
+
/*
* call-seq:
* IO.read(name, [length [, offset]] ) => string
@@ -7858,8 +7871,16 @@ rb_io_s_read(int argc, VALUE *argv, VALUE io)
open_key_args(argc, argv, &arg);
if (NIL_P(arg.io)) return Qnil;
if (!NIL_P(offset)) {
- rb_io_binmode(arg.io);
- rb_io_seek(arg.io, offset, SEEK_SET);
+ struct seek_arg sarg;
+ int state = 0;
+ sarg.io = arg.io;
+ sarg.offset = offset;
+ sarg.mode = SEEK_SET;
+ rb_protect((VALUE (*)(VALUE))seek_before_read, (VALUE)&sarg, &state);
+ if (state) {
+ rb_io_close(arg.io);
+ rb_jump_tag(state);
+ }
if (arg.argc == 2) arg.argc = 1;
}
return rb_ensure(io_s_read, (VALUE)&arg, rb_io_close, arg.io);