Skip to content

Commit 92961cd

Browse files
authored
Fix segmentation fault of StringScanner#charpos when String#byteslice returns non string value [Bug #17756] (#20)
1 parent 177e316 commit 92961cd

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

ext/strscan/strscan.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -445,13 +445,10 @@ static VALUE
445445
strscan_get_charpos(VALUE self)
446446
{
447447
struct strscanner *p;
448-
VALUE substr;
449448

450449
GET_SCANNER(self, p);
451450

452-
substr = rb_funcall(p->str, id_byteslice, 2, INT2FIX(0), LONG2NUM(p->curr));
453-
454-
return rb_str_length(substr);
451+
return LONG2NUM(rb_enc_strlen(S_PBEG(p), CURPTR(p), rb_enc_get(p->str)));
455452
}
456453

457454
/*

test/strscan/test_stringscanner.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,23 @@ def test_pos_unicode
206206
assert_equal 11, s.charpos
207207
end
208208

209+
def test_charpos_not_use_string_methods
210+
string = +'abcädeföghi'
211+
scanner = create_string_scanner(string)
212+
213+
class << string
214+
EnvUtil.suppress_warning do
215+
undef_method(*instance_methods)
216+
end
217+
end
218+
219+
assert_equal 0, scanner.charpos
220+
assert_equal "abcä", scanner.scan_until(/ä/)
221+
assert_equal 4, scanner.charpos
222+
assert_equal "defö", scanner.scan_until(/ö/)
223+
assert_equal 8, scanner.charpos
224+
end
225+
209226
def test_concat
210227
s = create_string_scanner('a'.dup)
211228
s.scan(/a/)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy