[前][次][番号順一覧][スレッド一覧]

ruby-changes:46997

From: nobu <ko1@a...>
Date: Sun, 18 Jun 2017 13:38:07 +0900 (JST)
Subject: [ruby-changes:46997] nobu:r59112 (trunk): string.c: check just before modification

nobu	2017-06-18 13:38:01 +0900 (Sun, 18 Jun 2017)

  New Revision: 59112

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59112

  Log:
    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.

  Modified files:
    trunk/string.c
    trunk/test/ruby/test_string.rb
Index: string.c
===================================================================
--- string.c	(revision 59111)
+++ string.c	(revision 59112)
@@ -8255,6 +8255,7 @@ rb_str_chomp_string(VALUE str, VALUE rs) https://github.com/ruby/ruby/blob/trunk/string.c#L8255
     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 https://github.com/ruby/ruby/blob/trunk/string.c#L8276
 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;
Index: test/ruby/test_string.rb
===================================================================
--- test/ruby/test_string.rb	(revision 59111)
+++ test/ruby/test_string.rb	(revision 59112)
@@ -444,6 +444,14 @@ CODE https://github.com/ruby/ruby/blob/trunk/test/ruby/test_string.rb#L444
 
     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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]