diff options
Diffstat (limited to 'io.c')
-rw-r--r-- | io.c | 23 |
1 files changed, 22 insertions, 1 deletions
@@ -5462,6 +5462,18 @@ io_s_read(arg) return io_read(arg->argc, &arg->sep, arg->io); } +struct seek_arg { + VALUE io; + VALUE offset; + int mode; +}; + +static VALUE +seek_before_read(struct seek_arg *arg) +{ + return rb_io_seek(arg->io, arg->offset, arg->mode); +} + /* * call-seq: * IO.read(name, [length [, offset]] ) => string @@ -5491,7 +5503,16 @@ rb_io_s_read(argc, argv, io) arg.io = rb_io_open(StringValueCStr(fname), "r"); if (NIL_P(arg.io)) return Qnil; if (!NIL_P(offset)) { - 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); + } } return rb_ensure(io_s_read, (VALUE)&arg, rb_io_close, arg.io); } |