ruby-changes:5258
From: mame <ko1@a...>
Date: Mon, 2 Jun 2008 21:46:04 +0900 (JST)
Subject: [ruby-changes:5258] Ruby:r16757 (trunk): * re.c: fix SEGV by Regexp.allocate.names, Match.allocate.names, etc.
mame 2008-06-02 21:45:42 +0900 (Mon, 02 Jun 2008) New Revision: 16757 Modified files: trunk/ChangeLog trunk/io.c trunk/re.c trunk/test/ruby/test_io.rb trunk/test/ruby/test_regexp.rb Log: * re.c: fix SEGV by Regexp.allocate.names, Match.allocate.names, etc. * test/ruby/test_regexp.rb: add tests for above. * io.c: fix SEGV by IO.allocate.print, etc. * test/ruby/test_io.rb: add tests for above. http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_io.rb?r1=16757&r2=16756&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=16757&r2=16756&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/io.c?r1=16757&r2=16756&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_regexp.rb?r1=16757&r2=16756&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/re.c?r1=16757&r2=16756&diff_format=u Index: re.c =================================================================== --- re.c (revision 16756) +++ re.c (revision 16757) @@ -672,6 +672,7 @@ rb_reg_names(VALUE re) { VALUE ary = rb_ary_new(); + rb_reg_check(re); onig_foreach_name(RREGEXP(re)->ptr, reg_names_iter, (void*)ary); return ary; } @@ -718,6 +719,7 @@ rb_reg_named_captures(VALUE re) { VALUE hash = rb_hash_new(); + rb_reg_check(re); onig_foreach_name(RREGEXP(re)->ptr, reg_named_captures_iter, (void*)hash); return hash; } @@ -866,6 +868,14 @@ rm->char_offset_updated = 1; } +static void +match_check(VALUE match) +{ + if (!RMATCH(match)->regexp) { + rb_raise(rb_eTypeError, "uninitialized Match"); + } +} + /* :nodoc: */ static VALUE match_init_copy(VALUE obj, VALUE orig) @@ -913,6 +923,7 @@ static VALUE match_regexp(VALUE match) { + match_check(match); return RMATCH(match)->regexp; } @@ -933,6 +944,7 @@ static VALUE match_names(VALUE match) { + match_check(match); return rb_reg_names(RMATCH(match)->regexp); } @@ -951,6 +963,7 @@ static VALUE match_size(VALUE match) { + match_check(match); return INT2FIX(RMATCH_REGS(match)->num_regs); } @@ -963,6 +976,7 @@ struct re_registers *regs = RMATCH_REGS(match); VALUE regexp = RMATCH(match)->regexp; + match_check(match); switch(TYPE(backref)) { default: return NUM2INT(backref); @@ -1013,6 +1027,7 @@ int i = match_backref_number(match, n); struct re_registers *regs = RMATCH_REGS(match); + match_check(match); if (i < 0 || regs->num_regs <= i) rb_raise(rb_eIndexError, "index %d out of matches", i); @@ -1048,6 +1063,7 @@ int i = match_backref_number(match, n); struct re_registers *regs = RMATCH_REGS(match); + match_check(match); if (i < 0 || regs->num_regs <= i) rb_raise(rb_eIndexError, "index %d out of matches", i); @@ -1082,6 +1098,7 @@ int i = match_backref_number(match, n); struct re_registers *regs = RMATCH_REGS(match); + match_check(match); if (i < 0 || regs->num_regs <= i) rb_raise(rb_eIndexError, "index %d out of matches", i); @@ -1350,6 +1367,7 @@ { struct re_registers *regs; if (NIL_P(match)) return Qnil; + match_check(match); regs = RMATCH_REGS(match); if (nth >= regs->num_regs) { return Qnil; @@ -1370,6 +1388,7 @@ struct re_registers *regs; if (NIL_P(match)) return Qnil; + match_check(match); regs = RMATCH_REGS(match); if (nth >= regs->num_regs) { return Qnil; @@ -1412,6 +1431,7 @@ struct re_registers *regs; if (NIL_P(match)) return Qnil; + match_check(match); regs = RMATCH_REGS(match); if (BEG(0) == -1) return Qnil; str = rb_str_subseq(RMATCH(match)->str, 0, BEG(0)); @@ -1439,6 +1459,7 @@ struct re_registers *regs; if (NIL_P(match)) return Qnil; + match_check(match); regs = RMATCH_REGS(match); if (BEG(0) == -1) return Qnil; str = RMATCH(match)->str; @@ -1455,6 +1476,7 @@ struct re_registers *regs; if (NIL_P(match)) return Qnil; + match_check(match); regs = RMATCH_REGS(match); if (BEG(0) == -1) return Qnil; @@ -1497,6 +1519,7 @@ int i; int taint = OBJ_TAINTED(match); + match_check(match); for (i=start; i<regs->num_regs; i++) { if (regs->beg[i] == -1) { rb_ary_push(ary, Qnil); @@ -1565,18 +1588,18 @@ static int name_to_backref_number(struct re_registers *regs, VALUE regexp, const char* name, const char* name_end) { - int num; + int num; - num = onig_name_to_backref_number(RREGEXP(regexp)->ptr, - (const unsigned char* )name, (const unsigned char* )name_end, regs); - if (num >= 1) { - return num; - } - else { - VALUE s = rb_str_new(name, (long )(name_end - name)); - rb_raise(rb_eIndexError, "undefined group name reference: %s", - StringValuePtr(s)); - } + num = onig_name_to_backref_number(RREGEXP(regexp)->ptr, + (const unsigned char* )name, (const unsigned char* )name_end, regs); + if (num >= 1) { + return num; + } + else { + VALUE s = rb_str_new(name, (long )(name_end - name)); + rb_raise(rb_eIndexError, "undefined group name reference: %s", + StringValuePtr(s)); + } } /* @@ -1610,6 +1633,7 @@ { VALUE idx, rest; + match_check(match); rb_scan_args(argc, argv, "11", &idx, &rest); if (NIL_P(rest)) { @@ -1669,6 +1693,7 @@ match_values_at(int argc, VALUE *argv, VALUE match) { struct re_registers *regs = RMATCH_REGS(match); + match_check(match); return rb_get_values_at(match, regs->num_regs, argc, argv, match_entry); } @@ -1688,6 +1713,7 @@ { VALUE str = rb_reg_last_match(match); + match_check(match); if (NIL_P(str)) str = rb_str_new(0,0); if (OBJ_TAINTED(match)) OBJ_TAINT(str); if (OBJ_TAINTED(RMATCH(match)->str)) OBJ_TAINT(str); @@ -1708,6 +1734,7 @@ static VALUE match_string(VALUE match) { + match_check(match); return RMATCH(match)->str; /* str is frozen */ } Index: ChangeLog =================================================================== --- ChangeLog (revision 16756) +++ ChangeLog (revision 16757) @@ -1,3 +1,13 @@ +Mon Jun 2 21:44:15 2008 Yusuke Endoh <mame@t...> + + * re.c: fix SEGV by Regexp.allocate.names, Match.allocate.names, etc. + + * test/ruby/test_regexp.rb: add tests for above. + + * io.c: fix SEGV by IO.allocate.print, etc. + + * test/ruby/test_io.rb: add tests for above. + Mon Jun 2 19:17:47 2008 Tanaka Akira <akr@f...> * test/ruby/test_argf.rb (teardown): remove renamed temporary files. Index: io.c =================================================================== --- io.c (revision 16756) +++ io.c (revision 16757) @@ -271,6 +271,7 @@ rb_io_get_write_io(VALUE io) { VALUE write_io; + rb_io_check_initialized(RFILE(io)->fptr); write_io = RFILE(io)->fptr->tied_io_for_writing; if (write_io) { return write_io; Index: test/ruby/test_regexp.rb =================================================================== --- test/ruby/test_regexp.rb (revision 16756) +++ test/ruby/test_regexp.rb (revision 16757) @@ -676,4 +676,45 @@ assert_equal(3, ("foo" + "bar" * 1000).rindex(/#{"bar"*1000}/)) assert_equal(4, ("foo\nbar\nbaz\n").rindex(/bar/i)) end + + def test_uninitialized + assert_raise(TypeError) { Regexp.allocate.hash } + assert_raise(TypeError) { Regexp.allocate.eql? Regexp.allocate } + assert_raise(TypeError) { Regexp.allocate == Regexp.allocate } + assert_raise(TypeError) { Regexp.allocate =~ "" } + assert_equal(false, Regexp.allocate === Regexp.allocate) + assert_nil(~Regexp.allocate) + assert_raise(TypeError) { Regexp.allocate.match("") } + assert_raise(TypeError) { Regexp.allocate.to_s } + assert_raise(TypeError) { Regexp.allocate.inspect } + assert_raise(TypeError) { Regexp.allocate.source } + assert_raise(TypeError) { Regexp.allocate.casefold? } + assert_raise(TypeError) { Regexp.allocate.options } + assert_equal(Encoding.find("ASCII-8BIT"), Regexp.allocate.encoding) + assert_equal(false, Regexp.allocate.fixed_encoding?) + assert_raise(TypeError) { Regexp.allocate.names } + assert_raise(TypeError) { Regexp.allocate.named_captures } + + assert_raise(TypeError) { MatchData.allocate.regexp } + assert_raise(TypeError) { MatchData.allocate.names } + assert_raise(TypeError) { MatchData.allocate.size } + assert_raise(TypeError) { MatchData.allocate.length } + assert_raise(TypeError) { MatchData.allocate.offset(0) } + assert_raise(TypeError) { MatchData.allocate.begin(0) } + assert_raise(TypeError) { MatchData.allocate.end(0) } + assert_raise(TypeError) { MatchData.allocate.to_a } + assert_raise(TypeError) { MatchData.allocate[:foo] } + assert_raise(TypeError) { MatchData.allocate.captures } + assert_raise(TypeError) { MatchData.allocate.values_at } + assert_raise(TypeError) { MatchData.allocate.pre_match } + assert_raise(TypeError) { MatchData.allocate.post_match } + assert_raise(TypeError) { MatchData.allocate.to_s } + assert_match(/^#<MatchData:.*>$/, MatchData.allocate.inspect) + assert_raise(TypeError) { MatchData.allocate.string } + $~ = MatchData.allocate + assert_raise(TypeError) { $& } + assert_raise(TypeError) { $` } + assert_raise(TypeError) { $' } + assert_raise(TypeError) { $+ } + end end Index: test/ruby/test_io.rb =================================================================== --- test/ruby/test_io.rb (revision 16756) +++ test/ruby/test_io.rb (revision 16757) @@ -1219,4 +1219,8 @@ assert_equal("bar\n", File.read(t.path, 4, 4)) end + + def test_uninitialized + assert_raise(IOError) { IO.allocate.print "" } + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/