[macruby-changes] [4920] MacRuby/trunk/string.c

source_changes at macosforge.org source_changes at macosforge.org
Sat Nov 20 15:38:16 PST 2010


Revision: 4920
          http://trac.macosforge.org/projects/ruby/changeset/4920
Author:   watson1978 at gmail.com
Date:     2010-11-20 15:38:13 -0800 (Sat, 20 Nov 2010)
Log Message:
-----------
String#sub! will throw an exception with frozen string, also when replacing was not performed.

Test Script:
{{{
require 'test/unit/assertions.rb'
include Test::Unit::Assertions

str = 'hello'
str.freeze

assert_raise(RuntimeError) { str.sub!(/0/, "x") }
assert_raise(RuntimeError) { str.sub!(/0/) { "x" } }
assert_raise(RuntimeError) { str.sub!(/h/) { "x" } }
assert_equal("hello", str.sub(/0/, "x"))
assert_equal("hello", str.sub(/0/) { "x" })
assert_equal("xello", str.sub(/h/) { "x" })

puts :ok
}}}

Modified Paths:
--------------
    MacRuby/trunk/string.c

Modified: MacRuby/trunk/string.c
===================================================================
--- MacRuby/trunk/string.c	2010-11-20 12:55:58 UTC (rev 4919)
+++ MacRuby/trunk/string.c	2010-11-20 23:38:13 UTC (rev 4920)
@@ -196,6 +196,17 @@
     return new_encoding;
 }
 
+static void
+str_modifiable(VALUE str)
+{
+    if (OBJ_FROZEN(str)) {
+	rb_error_frozen("string");
+    }
+    if (!OBJ_UNTRUSTED(str) && rb_safe_level() >= 4) {
+	rb_raise(rb_eSecurityError, "Insecure: can't modify string");
+    }
+}
+
 static rb_str_t *
 str_alloc(VALUE klass)
 {
@@ -3759,6 +3770,7 @@
     }
 
     VALUE pat = get_pat(argv[0], true);
+    str_modifiable(str);
     if (rb_reg_search(pat, str, 0, false) >= 0) {
 	VALUE match = rb_backref_get();
 	int count = 0;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101120/3807154d/attachment.html>


More information about the macruby-changes mailing list