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

ruby-changes:37976

From: nobu <ko1@a...>
Date: Mon, 23 Mar 2015 02:03:48 +0900 (JST)
Subject: [ruby-changes:37976] nobu:r50057 (trunk): marshal.c: register symbol strings first

nobu	2015-03-23 02:03:31 +0900 (Mon, 23 Mar 2015)

  New Revision: 50057

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

  Log:
    marshal.c: register symbol strings first
    
    * marshal.c (r_symreal): register symbol names as strings first so
      that r_symlink always returns valid names.
      [ruby-core:68587] [Bug #10991]
    * marshal.c (r_ivar, r_object0): now need to intern symbol names.
    * marshal.c (r_object0): compare with symbol names.

  Modified files:
    trunk/ChangeLog
    trunk/marshal.c
    trunk/test/ruby/test_marshal.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 50056)
+++ ChangeLog	(revision 50057)
@@ -1,3 +1,23 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Mar 23 02:03:28 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* marshal.c (r_symreal): register symbol names as strings first so
+	  that r_symlink always returns valid names.
+	  [ruby-core:68587] [Bug #10991]
+
+	* marshal.c (r_ivar, r_object0): now need to intern symbol names.
+
+	* marshal.c (r_object0): compare with symbol names.
+
+Mon Mar 23 01:44:35 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* marshal.c (r_symreal): register symbol names as strings first so
+	  that r_symlink always returns valid names.
+	  [ruby-core:68587] [Bug #10991]
+
+	* marshal.c (r_ivar, r_object0): now need to intern symbol names.
+
+	* marshal.c (r_object0): compare with symbol names.
+
 Sun Mar 22 22:07:40 2015  Kouhei Sutou  <kou@c...>
 
 	* doc/etc.rd.ja: Fix wrong coding for Emacs.
Index: marshal.c
===================================================================
--- marshal.c	(revision 50056)
+++ marshal.c	(revision 50057)
@@ -1295,11 +1295,18 @@ r_bytes0(long len, struct load_arg *arg) https://github.com/ruby/ruby/blob/trunk/marshal.c#L1295
 static int
 sym2encidx(VALUE sym, VALUE val)
 {
-    if (sym == ID2SYM(rb_id_encoding())) {
+    static const char name_encoding[8] = "encoding";
+    const char *p;
+    long l;
+    if (rb_enc_get_index(sym) != ENCINDEX_US_ASCII) return -1;
+    RSTRING_GETMEM(sym, p, l);
+    if (l <= 0) return -1;
+    if (l == sizeof(name_encoding) &&
+	memcmp(p, name_encoding, sizeof(name_encoding)) == 0) {
 	int idx = rb_enc_find_index(StringValueCStr(val));
 	return idx;
     }
-    else if (sym == ID2SYM(rb_intern("E"))) {
+    else if (l == 1 && *p == 'E') {
 	if (val == Qfalse) return rb_usascii_encindex();
 	else if (val == Qtrue) return rb_utf8_encindex();
 	/* bogus ignore */
@@ -1327,7 +1334,8 @@ r_symreal(struct load_arg *arg, int ivar https://github.com/ruby/ruby/blob/trunk/marshal.c#L1334
     int idx = -1;
     st_index_t n = arg->symbols->num_entries;
 
-    st_insert(arg->symbols, (st_data_t)n, (st_data_t)0);
+    if (rb_enc_str_asciionly_p(s)) rb_enc_associate_index(s, ENCINDEX_US_ASCII);
+    st_insert(arg->symbols, (st_data_t)n, (st_data_t)s);
     if (ivar) {
 	long num = r_long(arg);
 	while (num-- > 0) {
@@ -1336,10 +1344,8 @@ r_symreal(struct load_arg *arg, int ivar https://github.com/ruby/ruby/blob/trunk/marshal.c#L1344
 	}
     }
     if (idx > 0) rb_enc_associate_index(s, idx);
-    sym = rb_str_intern(s);
-    st_insert(arg->symbols, (st_data_t)n, (st_data_t)sym);
 
-    return sym;
+    return s;
 }
 
 static VALUE
@@ -1367,7 +1373,7 @@ r_symbol(struct load_arg *arg) https://github.com/ruby/ruby/blob/trunk/marshal.c#L1373
 static VALUE
 r_unique(struct load_arg *arg)
 {
-    return rb_sym2str(r_symbol(arg));
+    return r_symbol(arg);
 }
 
 static VALUE
@@ -1465,7 +1471,7 @@ r_ivar(VALUE obj, int *has_encoding, str https://github.com/ruby/ruby/blob/trunk/marshal.c#L1471
 		if (has_encoding) *has_encoding = TRUE;
 	    }
 	    else {
-		rb_ivar_set(obj, SYM2ID(sym), val);
+		rb_ivar_set(obj, rb_intern_str(sym), val);
 	    }
 	} while (--len > 0);
     }
@@ -1790,13 +1796,13 @@ r_object0(struct load_arg *arg, int *ivp https://github.com/ruby/ruby/blob/trunk/marshal.c#L1796
 	    v = r_entry0(v, idx, arg);
 	    values = rb_ary_new2(len);
 	    for (i=0; i<len; i++) {
+		VALUE n = rb_sym2str(RARRAY_AREF(mem, i));
 		slot = r_symbol(arg);
 
-		if (RARRAY_AREF(mem, i) != slot) {
+		if (!rb_str_equal(n, slot)) {
 		    rb_raise(rb_eTypeError, "struct %"PRIsVALUE" not compatible (:%"PRIsVALUE" for :%"PRIsVALUE")",
 			     rb_class_name(klass),
-			     rb_sym2str(slot),
-			     rb_sym2str(RARRAY_AREF(mem, i)));
+			     slot, n);
 		}
                 rb_ary_push(values, r_object(arg));
 		arg->readable -= 2;
@@ -1934,11 +1940,12 @@ r_object0(struct load_arg *arg, int *ivp https://github.com/ruby/ruby/blob/trunk/marshal.c#L1940
 	else {
 	    v = r_symreal(arg, 0);
 	}
+	v = rb_str_intern(v);
 	v = r_leave(v, arg);
 	break;
 
       case TYPE_SYMLINK:
-	v = r_symlink(arg);
+	v = rb_str_intern(r_symlink(arg));
 	break;
 
       default:
Index: test/ruby/test_marshal.rb
===================================================================
--- test/ruby/test_marshal.rb	(revision 50056)
+++ test/ruby/test_marshal.rb	(revision 50057)
@@ -252,6 +252,17 @@ class TestMarshal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_marshal.rb#L252
     assert_include(Marshal.dump([:a, :a]), ';')
   end
 
+  def test_symlink_in_ivar
+    bug10991 = '[ruby-core:68587] [Bug #10991]'
+    sym = Marshal.load("\x04\x08" +
+                       "I" ":\x0bKernel" +
+                       ("\x06" +
+                        ("I" ":\x07@a" +
+                         ("\x06" ":\x07@b" "e;\x0""o:\x0bObject""\x0")) +
+                        "0"))
+    assert_equal(:Kernel, sym, bug10991)
+  end
+
   ClassUTF8 = eval("class R\u{e9}sum\u{e9}; self; end")
 
   iso_8859_1 = Encoding::ISO_8859_1

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

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