From f5052d45be1b564a683c347dd72fd2f7b8638fd8 Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 18 Jun 2017 04:38:01 +0000 Subject: [PATCH] string.c: check just before modification * string.c (rb_str_chomp_bang): check if modifiable after checking an argument and just before modification, as it can get frozen during the argument conversion to String. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59112 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- string.c | 3 ++- test/ruby/test_string.rb | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/string.c b/string.c index cd633e1226..052e0197a1 100644 --- a/string.c +++ b/string.c @@ -8255,6 +8255,7 @@ rb_str_chomp_string(VALUE str, VALUE rs) long olen = RSTRING_LEN(str); long len = chompped_length(str, rs); if (len >= olen) return Qnil; + str_modify_keep_cr(str); STR_SET_LEN(str, len); TERM_FILL(&RSTRING_PTR(str)[len], TERM_LEN(str)); if (ENC_CODERANGE(str) != ENC_CODERANGE_7BIT) { @@ -8275,7 +8276,7 @@ static VALUE rb_str_chomp_bang(int argc, VALUE *argv, VALUE str) { VALUE rs; - str_modify_keep_cr(str); + str_modifiable(str); if (RSTRING_LEN(str) == 0) return Qnil; rs = chomp_rs(argc, argv); if (NIL_P(rs)) return Qnil; diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index dc76b53810..0fd0538d00 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -444,6 +444,14 @@ CODE s = S("").freeze assert_raise_with_message(RuntimeError, /frozen/) {s.chomp!} + + s = S("ax") + o = Struct.new(:s).new(s) + def o.to_str + s.freeze + "x" + end + assert_raise_with_message(RuntimeError, /frozen/) {s.chomp!(o)} ensure $/ = save end