Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion mrbgems/mruby-regexp/src/regexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,10 @@ matchdata_aref(mrb_state *mrb, mrb_value self)
if (!mrb_nil_p(md->regexp)) {
pat = DATA_GET_PTR(mrb, md->regexp, &regexp_type, mrb_regexp_pattern);
}
if (pat) {
/* A stored name never exceeds UINT16_MAX, so a longer request can name no
group. Rejecting it here keeps the cast in the loop lossless; without it
the length test truncates while the memcmp() next to it does not. */
if (pat && name_len <= UINT16_MAX) {
for (uint16_t i = 0; i < pat->num_named; i++) {
if (pat->named_captures[i].name_len == (uint16_t)name_len &&
memcmp(pat->named_captures[i].name, name, name_len) == 0) {
Expand Down
10 changes: 10 additions & 0 deletions mrbgems/mruby-regexp/test/regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,16 @@ def limit.to_int; 1.5; end
assert_raise(IndexError) { /(a)/.match("a")[:zz] }
end

assert("MatchData#[] - group name longer than a uint16 length") do
# Regression: the length test truncated the requested length to uint16_t
# while the memcmp() next to it did not, so (uint16_t)65539 == 3 ==
# "abc".length let a 65539-byte read run off a 3-byte arena. The result is
# unchanged either way; only a sanitizer build fails on it.
md = /(?<abc>x)/.match("x")
assert_raise(IndexError) { md["abc" + "A" * 65536] }
assert_equal "x", md[:abc]
end

assert("Regexp - named backreference \\k") do
assert_equal "aa", "aa".match(/(?<n>\w)\k<n>/)[0]
assert_equal "abba", "abba".match(/(?<a>.)(?<b>.)\k<b>\k<a>/)[0]
Expand Down
Loading