Skip to content

Commit 1e48631

Browse files
committed
merge revision(s) 02b7025, 6b4f894: [Backport #20909]
Check negative integer underflow Many of Oniguruma functions need valid encoding strings
1 parent 8506fdf commit 1e48631

3 files changed

Lines changed: 15 additions & 4 deletions

File tree

string.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2891,11 +2891,12 @@ rb_str_subpos(VALUE str, long beg, long *lenp)
28912891
{
28922892
long len = *lenp;
28932893
long slen = -1L;
2894-
long blen = RSTRING_LEN(str);
2894+
const long blen = RSTRING_LEN(str);
28952895
rb_encoding *enc = STR_ENC_GET(str);
28962896
char *p, *s = RSTRING_PTR(str), *e = s + blen;
28972897

28982898
if (len < 0) return 0;
2899+
if (beg < 0 && -beg < 0) return 0;
28992900
if (!blen) {
29002901
len = 0;
29012902
}
@@ -2913,7 +2914,8 @@ rb_str_subpos(VALUE str, long beg, long *lenp)
29132914
}
29142915
if (beg < 0) {
29152916
if (len > -beg) len = -beg;
2916-
if (-beg * rb_enc_mbmaxlen(enc) < RSTRING_LEN(str) / 8) {
2917+
if ((ENC_CODERANGE(str) == ENC_CODERANGE_VALID) &&
2918+
(-beg * rb_enc_mbmaxlen(enc) < blen / 8)) {
29172919
beg = -beg;
29182920
while (beg-- > len && (e = rb_enc_prev_char(s, e, e, enc)) != 0);
29192921
p = e;
@@ -2931,7 +2933,7 @@ rb_str_subpos(VALUE str, long beg, long *lenp)
29312933
if (len == 0) goto end;
29322934
}
29332935
}
2934-
else if (beg > 0 && beg > RSTRING_LEN(str)) {
2936+
else if (beg > 0 && beg > blen) {
29352937
return 0;
29362938
}
29372939
if (len == 0) {

test/ruby/test_string.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,15 @@ def o.to_int; 2; end
169169
assert_raise(ArgumentError) { "foo"[] }
170170
end
171171

172+
def test_AREF_underflow
173+
require "rbconfig/sizeof"
174+
assert_equal(nil, S("\u{3042 3044 3046}")[RbConfig::LIMITS["LONG_MIN"], 1])
175+
end
176+
177+
def test_AREF_invalid_encoding
178+
assert_equal(S("\x80"), S("A"*39+"\x80")[-1, 1])
179+
end
180+
172181
def test_ASET # '[]='
173182
s = S("FooBar")
174183
s[0] = S('A')

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
1212
#define RUBY_VERSION_TEENY 6
1313
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
14-
#define RUBY_PATCHLEVEL 113
14+
#define RUBY_PATCHLEVEL 114
1515

1616
#include "ruby/version.h"
1717
#include "ruby/internal/abi.h"

0 commit comments

Comments
 (0)