Skip to content

Commit 0205bd1

Browse files
committedMar 13, 2024
Define StringIO::MAX_LENGTH
·
v3.1.7v3.1.1
1 parent 31a9d42 commit 0205bd1

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed
 

‎ext/java/org/jruby/ext/stringio/StringIO.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ public static RubyClass createStringIOClass(final Ruby runtime) {
100100
RubyString version = RubyString.newString(runtime, STRINGIO_VERSION);
101101
stringIOClass.defineConstant("VERSION", version);
102102

103+
stringIOClass.defineConstant("MAX_LENGTH", RubyNumeric.int2fix(runtime, Integer.MAX_VALUE));
104+
103105
stringIOClass.defineAnnotatedMethods(StringIO.class);
104106
stringIOClass.includeModule(runtime.getEnumerable());
105107

‎ext/stringio/stringio.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,6 +1861,10 @@ Init_stringio(void)
18611861

18621862
rb_include_module(StringIO, rb_mEnumerable);
18631863
rb_define_alloc_func(StringIO, strio_s_allocate);
1864+
1865+
/* Maximum length that a StringIO instance can hold */
1866+
rb_define_const(StringIO, "MAX_LENGTH", LONG2NUM(LONG_MAX));
1867+
18641868
rb_define_singleton_method(StringIO, "new", strio_s_new, -1);
18651869
rb_define_singleton_method(StringIO, "open", strio_s_open, -1);
18661870
rb_define_method(StringIO, "initialize", strio_initialize, -1);

‎test/stringio/test_stringio.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,8 @@ def test_write_encoding_conversion
237237

238238
def test_write_integer_overflow
239239
f = StringIO.new
240+
f.pos = StringIO::MAX_LENGTH
240241
assert_raise(ArgumentError) {
241-
# JRuby errors when setting pos to an out-of-range value
242-
f.pos = RbConfig::LIMITS["LONG_MAX"]
243242
f.write("pos + len overflows")
244243
}
245244
end
@@ -900,8 +899,9 @@ def test_new_block_warning
900899
end
901900

902901
def test_overflow
903-
return if RbConfig::SIZEOF["void*"] > RbConfig::SIZEOF["long"]
904-
limit = RbConfig::LIMITS["INTPTR_MAX"] - 0x10
902+
intptr_max = RbConfig::LIMITS["INTPTR_MAX"]
903+
return if intptr_max > StringIO::MAX_LENGTH
904+
limit = intptr_max - 0x10
905905
assert_separately(%w[-rstringio], "#{<<-"begin;"}\n#{<<-"end;"}")
906906
begin;
907907
limit = #{limit}

0 commit comments

Comments
 (0)
Please sign in to comment.