summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-07-21 13:30:46 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-07-21 13:30:46 +0000
commit90311f37e47dd62ffad20c90db68a0705c050061 (patch)
treea729a6e8af5377ffad933e25d2975b0bb140aba3
parent5f534b7d4684f5bf826ab9ad4dac166f5b938f1d (diff)
strscan.c: fix segfault in aref
* ext/strscan/strscan.c (strscan_aref): fix segfault after get_byte or getch which do not apply regexp. [ruby-core:82116] [Bug #13759] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59384 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ext/strscan/strscan.c1
-rw-r--r--test/strscan/test_stringscanner.rb10
2 files changed, 11 insertions, 0 deletions
diff --git a/ext/strscan/strscan.c b/ext/strscan/strscan.c
index 838f0e7..75cff37 100644
--- a/ext/strscan/strscan.c
+++ b/ext/strscan/strscan.c
@@ -1034,6 +1034,7 @@ strscan_aref(VALUE self, VALUE idx)
idx = rb_sym2str(idx);
/* fall through */
case T_STRING:
+ if (!p->regex) return Qnil;
RSTRING_GETMEM(idx, name, i);
i = name_to_backref_number(&(p->regs), p->regex, name, name + i, rb_enc_get(idx));
break;
diff --git a/test/strscan/test_stringscanner.rb b/test/strscan/test_stringscanner.rb
index bc5db79..9fe6ad7 100644
--- a/test/strscan/test_stringscanner.rb
+++ b/test/strscan/test_stringscanner.rb
@@ -718,4 +718,14 @@ class TestStringScanner < Test::Unit::TestCase
s.scan(/test strin/)
assert_equal('#<StringScanner 10/16 "...strin" @ "g tes...">', s.inspect)
end
+
+ def test_aref_without_regex
+ s = StringScanner.new('abc')
+ s.get_byte
+ assert_nil(s[:c])
+ assert_nil(s["c"])
+ s.getch
+ assert_nil(s[:c])
+ assert_nil(s["c"])
+ end
end