ruby-changes:18182
From: usa <ko1@a...>
Date: Tue, 14 Dec 2010 13:36:20 +0900 (JST)
Subject: [ruby-changes:18182] Ruby:r30203 (trunk): * ext/zlib/zlib.c (gzfile_s_open): should close the IO if some error
usa 2010-12-14 13:36:14 +0900 (Tue, 14 Dec 2010) New Revision: 30203 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=30203 Log: * ext/zlib/zlib.c (gzfile_s_open): should close the IO if some error occurs in initilizing. Modified files: trunk/ChangeLog trunk/ext/zlib/zlib.c Index: ChangeLog =================================================================== --- ChangeLog (revision 30202) +++ ChangeLog (revision 30203) @@ -1,3 +1,8 @@ +Tue Dec 14 13:34:33 2010 NAKAMURA Usaku <usa@r...> + + * ext/zlib/zlib.c (gzfile_s_open): should close the IO if some error + occurs in initilizing. + Tue Dec 14 13:04:16 2010 NARUSE, Yui <naruse@r...> * lib/net/http.rb (Net::HTTPRequest#send_request_body_data): Index: ext/zlib/zlib.c =================================================================== --- ext/zlib/zlib.c (revision 30202) +++ ext/zlib/zlib.c (revision 30203) @@ -2456,7 +2456,20 @@ */ +typedef struct { + int argc; + VALUE *argv; + VALUE klass; +} new_wrap_arg_t; + static VALUE +new_wrap(VALUE tmp) +{ + new_wrap_arg_t *arg = (new_wrap_arg_t *)tmp; + return rb_class_new_instance(arg->argc, arg->argv, arg->klass); +} + +static VALUE gzfile_ensure_close(VALUE obj) { struct gzfile *gz; @@ -2468,6 +2481,35 @@ return Qnil; } +static VALUE +gzfile_wrap(int argc, VALUE *argv, VALUE klass, int close_io_on_error) +{ + VALUE obj; + + if (close_io_on_error) { + int state = 0; + new_wrap_arg_t arg; + arg.argc = argc; + arg.argv = argv; + arg.klass = klass; + obj = rb_protect(new_wrap, (VALUE)&arg, &state); + if (state) { + rb_io_close(argv[0]); + rb_jump_tag(state); + } + } + else { + obj = rb_class_new_instance(argc, argv, klass); + } + + if (rb_block_given_p()) { + return rb_ensure(rb_yield, obj, gzfile_ensure_close, obj); + } + else { + return obj; + } +} + /* * call-seq: Zlib::GzipFile.wrap(io) { |gz| ... } * @@ -2481,14 +2523,7 @@ static VALUE rb_gzfile_s_wrap(int argc, VALUE *argv, VALUE klass) { - VALUE obj = rb_class_new_instance(argc, argv, klass); - - if (rb_block_given_p()) { - return rb_ensure(rb_yield, obj, gzfile_ensure_close, obj); - } - else { - return obj; - } + return gzfile_wrap(argc, argv, klass, 0); } /* @@ -2505,7 +2540,7 @@ filename = argv[0]; io = rb_file_open_str(filename, mode); argv[0] = io; - return rb_gzfile_s_wrap(argc, argv, klass); + return gzfile_wrap(argc, argv, klass, 1); } /* -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/