1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/strscan] Fix segmentation fault of StringScanner#charpos when String#byteslice returns non string value [Bug #17756] (#20)

https://github.com/ruby/strscan/commit/92961cde2b
This commit is contained in:
Kenichi Kamiya 2021-03-31 14:56:28 +09:00 committed by Hiroshi SHIBATA
parent 822eb94563
commit 564ccd095a
2 changed files with 18 additions and 4 deletions

View file

@ -445,13 +445,10 @@ static VALUE
strscan_get_charpos(VALUE self)
{
struct strscanner *p;
VALUE substr;
GET_SCANNER(self, p);
substr = rb_funcall(p->str, id_byteslice, 2, INT2FIX(0), LONG2NUM(p->curr));
return rb_str_length(substr);
return LONG2NUM(rb_enc_strlen(S_PBEG(p), CURPTR(p), rb_enc_get(p->str)));
}
/*

View file

@ -206,6 +206,23 @@ class TestStringScanner < Test::Unit::TestCase
assert_equal 11, s.charpos
end
def test_charpos_not_use_string_methods
string = +'abcädeföghi'
scanner = create_string_scanner(string)
class << string
EnvUtil.suppress_warning do
undef_method(*instance_methods)
end
end
assert_equal 0, scanner.charpos
assert_equal "abcä", scanner.scan_until(/ä/)
assert_equal 4, scanner.charpos
assert_equal "defö", scanner.scan_until(/ö/)
assert_equal 8, scanner.charpos
end
def test_concat
s = create_string_scanner('a'.dup)
s.scan(/a/)