diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index 975a308..e6b17a8 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -728,6 +728,7 @@ strio_ungetc(VALUE self, VALUE c) rb_encoding *enc, *enc2; if (NIL_P(c)) return Qnil; + check_modifiable(ptr); if (FIXNUM_P(c)) { int cc = FIX2INT(c); char buf[16]; @@ -792,6 +793,7 @@ strio_ungetbyte(VALUE self, VALUE c) cl = RSTRING_LEN(c); if (cl == 0) return Qnil; } + check_modifiable(ptr); rb_str_modify(str); if (cl > pos) { char *s; diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb index 417a53b..0ea3e4f 100644 --- a/test/stringio/test_stringio.rb +++ b/test/stringio/test_stringio.rb @@ -504,6 +504,14 @@ class TestStringIO < Test::Unit::TestCase assert_raise(RuntimeError, bug) {s.reopen("")} end + def test_frozen_string + s = StringIO.new("".freeze) + bug = '[ruby-core:48530]' + assert_raise(IOError, bug) {s.write("foo")} + assert_raise(IOError, bug) {s.ungetc("a")} + assert_raise(IOError, bug) {s.ungetbyte("a")} + end + def test_readlines_limit_0 assert_raise(ArgumentError, "[ruby-dev:43392]") { StringIO.new.readlines(0) } end