Skip to content

Commit

Permalink
[ruby/strscan] Fix segmentation fault of StringScanner#charpos when…
Browse files Browse the repository at this point in the history
… `String#byteslice` returns non string value [Bug #17756] (#20)

ruby/strscan@92961cde2b
  • Loading branch information
kachick authored and hsbt committed May 6, 2021
1 parent 822eb94 commit 564ccd0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
5 changes: 1 addition & 4 deletions ext/strscan/strscan.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}

/*
Expand Down
17 changes: 17 additions & 0 deletions test/strscan/test_stringscanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,23 @@ def test_pos_unicode
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/)
Expand Down

0 comments on commit 564ccd0

Please sign in to comment.