diff --git a/internal.h b/internal.h index e9f8cb0..e4f15bf 100644 --- a/internal.h +++ b/internal.h @@ -442,6 +442,7 @@ VALUE rb_id_quote_unprintable(ID); #define QUOTE(str) rb_str_quote_unprintable(str) #define QUOTE_ID(id) rb_id_quote_unprintable(id) void rb_str_fill_terminator(VALUE str, const int termlen); +VALUE rb_str_locktmp_ensure(VALUE str, VALUE (*func)(VALUE), VALUE arg); /* struct.c */ VALUE rb_struct_init_copy(VALUE copy, VALUE s); diff --git a/io.c b/io.c index 6038609..b502f31 100644 --- a/io.c +++ b/io.c @@ -2047,15 +2047,32 @@ io_bufread(char *ptr, long len, rb_io_t *fptr) static void io_setstrbuf(VALUE *str, long len); +struct bufread_arg { + char *str_ptr; + long len; + rb_io_t *fptr; +}; + +static VALUE +bufread_call(VALUE arg) +{ + struct bufread_arg *p = (struct bufread_arg *)arg; + p->len = io_bufread(p->str_ptr, p->len, p->fptr); + return Qundef; +} + static long io_fread(VALUE str, long offset, long size, rb_io_t *fptr) { long len; + struct bufread_arg arg; io_setstrbuf(&str, offset + size); - rb_str_locktmp(str); - len = io_bufread(RSTRING_PTR(str) + offset, size, fptr); - rb_str_unlocktmp(str); + arg.str_ptr = RSTRING_PTR(str) + offset; + arg.len = size; + arg.fptr = fptr; + rb_str_locktmp_ensure(str, bufread_call, (VALUE)&arg); + len = arg.len; if (len < 0) rb_sys_fail_path(fptr->pathv); return len; } diff --git a/string.c b/string.c index f101a94..e7f9b44 100644 --- a/string.c +++ b/string.c @@ -1871,6 +1871,13 @@ rb_str_unlocktmp(VALUE str) return str; } +VALUE +rb_str_locktmp_ensure(VALUE str, VALUE (*func)(VALUE), VALUE arg) +{ + rb_str_locktmp(str); + return rb_ensure(func, arg, rb_str_unlocktmp, str); +} + void rb_str_set_len(VALUE str, long len) {