Skip to content

Commit fee9200

Browse files
committed
merge revision(s) fa85d23: [Backport #21380]
[Bug #21380] Prohibit modification in String#split block Reported at https://hackerone.com/reports/3163876
1 parent 3a06b3d commit fee9200

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

string.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9247,11 +9247,15 @@ rb_str_split_m(int argc, VALUE *argv, VALUE str)
92479247
}
92489248
}
92499249

9250-
#define SPLIT_STR(beg, len) (empty_count = split_string(result, str, beg, len, empty_count))
9250+
#define SPLIT_STR(beg, len) ( \
9251+
empty_count = split_string(result, str, beg, len, empty_count), \
9252+
str_mod_check(str, str_start, str_len))
92519253

92529254
beg = 0;
92539255
char *ptr = RSTRING_PTR(str);
9254-
char *eptr = RSTRING_END(str);
9256+
char *const str_start = ptr;
9257+
const long str_len = RSTRING_LEN(str);
9258+
char *const eptr = str_start + str_len;
92559259
if (split_type == SPLIT_TYPE_AWK) {
92569260
char *bptr = ptr;
92579261
int skip = 1;
@@ -9312,7 +9316,6 @@ rb_str_split_m(int argc, VALUE *argv, VALUE str)
93129316
}
93139317
}
93149318
else if (split_type == SPLIT_TYPE_STRING) {
9315-
char *str_start = ptr;
93169319
char *substr_start = ptr;
93179320
char *sptr = RSTRING_PTR(spat);
93189321
long slen = RSTRING_LEN(spat);
@@ -9329,14 +9332,14 @@ rb_str_split_m(int argc, VALUE *argv, VALUE str)
93299332
continue;
93309333
}
93319334
SPLIT_STR(substr_start - str_start, (ptr+end) - substr_start);
9335+
str_mod_check(spat, sptr, slen);
93329336
ptr += end + slen;
93339337
substr_start = ptr;
93349338
if (!NIL_P(limit) && lim <= ++i) break;
93359339
}
93369340
beg = ptr - str_start;
93379341
}
93389342
else if (split_type == SPLIT_TYPE_CHARS) {
9339-
char *str_start = ptr;
93409343
int n;
93419344

93429345
if (result) result = rb_ary_new_capa(RSTRING_LEN(str));

test/ruby/test_string.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,6 +1869,13 @@ def test_split_with_block
18691869

18701870
result = []; S("aaa,bbb,ccc,ddd").split(/,/) {|s| result << s.gsub(/./, "A")}
18711871
assert_equal(["AAA"]*4, result)
1872+
1873+
s = S("abc ") * 20
1874+
assert_raise(RuntimeError) {
1875+
10.times do
1876+
s.split {s.prepend("xxx" * 100)}
1877+
end
1878+
}
18721879
ensure
18731880
EnvUtil.suppress_warning {$; = fs}
18741881
end

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 4
1313
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
14-
#define RUBY_PATCHLEVEL 44
14+
#define RUBY_PATCHLEVEL 45
1515

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

0 commit comments

Comments
 (0)