Skip to content

Commit 9786b90

Browse files
XrXrbyroot
authored andcommitted
Fix regex match cache out-of-bounds access
Previously the following read and wrote 1 byte out-of-bounds: $ valgrind ruby -e 'p /(\W+)[bx]\?/i.match? "aaaaaa aaaaaaaaa aaaa aaaaaaaa aaa aaaaxaaaaaaaaaaa aaaaa aaaaaaaaaaaa a ? aaa aaaa a ?"' 2> >(grep Invalid -A 30) Because of the `match_cache_point_index + 1` in memoize_extended_match_cache_point() and check_extended_match_cache_point(), we need one more byte of space.
1 parent c65bb5a commit 9786b90

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

regexec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4092,7 +4092,7 @@ match_at(regex_t* reg, const UChar* str, const UChar* end,
40924092
if (num_match_cache_points >= LONG_MAX_LIMIT) {
40934093
return ONIGERR_MEMORY;
40944094
}
4095-
size_t match_cache_buf_length = (num_match_cache_points >> 3) + (num_match_cache_points & 7 ? 1 : 0);
4095+
size_t match_cache_buf_length = (num_match_cache_points >> 3) + (num_match_cache_points & 7 ? 1 : 0) + 1;
40964096
uint8_t* match_cache_buf = (uint8_t*)xmalloc(match_cache_buf_length * sizeof(uint8_t));
40974097
if (match_cache_buf == NULL) {
40984098
return ONIGERR_MEMORY;

0 commit comments

Comments
 (0)