From 3f8ef7ff7c09e67a48eff33804060803b9f11119 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Wed, 13 Mar 2024 19:13:37 +0900 Subject: [PATCH] [ruby/stringio] Define `StringIO::MAX_LENGTH` https://github.com/ruby/stringio/commit/0205bd1c86 --- ext/stringio/stringio.c | 4 ++++ test/stringio/test_stringio.rb | 8 ++++---- 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}