summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2024-03-13 19:13:37 +0900
committergit <svn-admin@ruby-lang.org>2024-03-13 13:20:23 +0000
commit3f8ef7ff7c09e67a48eff33804060803b9f11119 (patch)
treebea4f20c489b40f66ab80e3289c3086453c48342
parent2fc551e34e3d1310986e0e2727aadcd39ba06925 (diff)
[ruby/stringio] Define `StringIO::MAX_LENGTH`
https://github.com/ruby/stringio/commit/0205bd1c86
-rw-r--r--ext/stringio/stringio.c4
-rw-r--r--test/stringio/test_stringio.rb8
2 files changed, 8 insertions, 4 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c
index 27c7f65408..8cb792d27b 100644
--- a/ext/stringio/stringio.c
+++ b/ext/stringio/stringio.c
@@ -1861,6 +1861,10 @@ Init_stringio(void)
rb_include_module(StringIO, rb_mEnumerable);
rb_define_alloc_func(StringIO, strio_s_allocate);
+
+ /* Maximum length that a StringIO instance can hold */
+ rb_define_const(StringIO, "MAX_LENGTH", LONG2NUM(LONG_MAX));
+
rb_define_singleton_method(StringIO, "new", strio_s_new, -1);
rb_define_singleton_method(StringIO, "open", strio_s_open, -1);
rb_define_method(StringIO, "initialize", strio_initialize, -1);
diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb
index ae8e845497..b0eff57c77 100644
--- a/test/stringio/test_stringio.rb
+++ b/test/stringio/test_stringio.rb
@@ -237,9 +237,8 @@ class TestStringIO < Test::Unit::TestCase
def test_write_integer_overflow
f = StringIO.new
+ f.pos = StringIO::MAX_LENGTH
assert_raise(ArgumentError) {
- # JRuby errors when setting pos to an out-of-range value
- f.pos = RbConfig::LIMITS["LONG_MAX"]
f.write("pos + len overflows")
}
end
@@ -900,8 +899,9 @@ class TestStringIO < Test::Unit::TestCase
end
def test_overflow
- return if RbConfig::SIZEOF["void*"] > RbConfig::SIZEOF["long"]
- limit = RbConfig::LIMITS["INTPTR_MAX"] - 0x10
+ intptr_max = RbConfig::LIMITS["INTPTR_MAX"]
+ return if intptr_max > StringIO::MAX_LENGTH
+ limit = intptr_max - 0x10
assert_separately(%w[-rstringio], "#{<<-"begin;"}\n#{<<-"end;"}")
begin;
limit = #{limit}