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

ruby-changes:42611

From: usa <ko1@a...>
Date: Fri, 22 Apr 2016 15:02:24 +0900 (JST)
Subject: [ruby-changes:42611] usa:r54685 (ruby_2_2): merge revision(s) 54611, 54612: [Backport #12291]

usa	2016-04-22 15:59:00 +0900 (Fri, 22 Apr 2016)

  New Revision: 54685

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

  Log:
    merge revision(s) 54611,54612: [Backport #12291]
    
    * struct.c (struct_make_members_list): extract making member name
      list from char* va_list, with creating symbols without
      intermediate IDs.
    
    * struct.c (struct_make_members_list, rb_struct_s_def): member
      names should be unique. [ruby-core:74971] [Bug #12291]

  Added files:
    branches/ruby_2_2/ext/-test-/struct/duplicate.c
    branches/ruby_2_2/test/-ext-/struct/test_duplicate.rb
  Modified directories:
    branches/ruby_2_2/
  Modified files:
    branches/ruby_2_2/ChangeLog
    branches/ruby_2_2/struct.c
    branches/ruby_2_2/test/ruby/test_struct.rb
    branches/ruby_2_2/version.h
Index: ruby_2_2/ChangeLog
===================================================================
--- ruby_2_2/ChangeLog	(revision 54684)
+++ ruby_2_2/ChangeLog	(revision 54685)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_2/ChangeLog#L1
+Fri Apr 22 15:47:58 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* struct.c (struct_make_members_list): extract making member name
+	  list from char* va_list, with creating symbols without
+	  intermediate IDs.
+
 Fri Apr 22 15:19:01 2016  Joe Swatosh  <joe.swatosh@g...>
 
 	* ext/win32/lib/win32/registry.rb (DeleteValue, DeleteKey): fix
Index: ruby_2_2/struct.c
===================================================================
--- ruby_2_2/struct.c	(revision 54684)
+++ ruby_2_2/struct.c	(revision 54685)
@@ -232,6 +232,27 @@ rb_struct_alloc_noinit(VALUE klass) https://github.com/ruby/ruby/blob/trunk/ruby_2_2/struct.c#L232
 }
 
 static VALUE
+struct_make_members_list(va_list ar)
+{
+    char *mem;
+    VALUE ary, list = rb_ident_hash_new();
+    st_table *tbl = RHASH_TBL(list);
+
+    RBASIC_CLEAR_CLASS(list);
+    while ((mem = va_arg(ar, char*)) != 0) {
+	VALUE sym = rb_id2sym(rb_intern(mem));
+	if (st_insert(tbl, sym, Qtrue)) {
+	    rb_raise(rb_eArgError, "duplicate member: %s", mem);
+	}
+    }
+    ary = rb_hash_keys(list);
+    st_clear(tbl);
+    RBASIC_CLEAR_CLASS(ary);
+    OBJ_FREEZE_RAW(ary);
+    return ary;
+}
+
+static VALUE
 struct_define_without_accessor(VALUE outer, const char *class_name, VALUE super, rb_alloc_func_t alloc, VALUE members)
 {
     VALUE klass;
@@ -265,15 +286,10 @@ rb_struct_define_without_accessor_under( https://github.com/ruby/ruby/blob/trunk/ruby_2_2/struct.c#L286
 {
     va_list ar;
     VALUE members;
-    char *name;
 
-    members = rb_ary_tmp_new(0);
     va_start(ar, alloc);
-    while ((name = va_arg(ar, char*)) != NULL) {
-        rb_ary_push(members, ID2SYM(rb_intern(name)));
-    }
+    members = struct_make_members_list(ar);
     va_end(ar);
-    OBJ_FREEZE(members);
 
     return struct_define_without_accessor(outer, class_name, super, alloc, members);
 }
@@ -283,15 +299,10 @@ rb_struct_define_without_accessor(const https://github.com/ruby/ruby/blob/trunk/ruby_2_2/struct.c#L299
 {
     va_list ar;
     VALUE members;
-    char *name;
 
-    members = rb_ary_tmp_new(0);
     va_start(ar, alloc);
-    while ((name = va_arg(ar, char*)) != NULL) {
-        rb_ary_push(members, ID2SYM(rb_intern(name)));
-    }
+    members = struct_make_members_list(ar);
     va_end(ar);
-    OBJ_FREEZE(members);
 
     return struct_define_without_accessor(0, class_name, super, alloc, members);
 }
@@ -301,15 +312,9 @@ rb_struct_define(const char *name, ...) https://github.com/ruby/ruby/blob/trunk/ruby_2_2/struct.c#L312
 {
     va_list ar;
     VALUE st, ary;
-    char *mem;
-
-    ary = rb_ary_tmp_new(0);
 
     va_start(ar, name);
-    while ((mem = va_arg(ar, char*)) != 0) {
-	ID slot = rb_intern(mem);
-	rb_ary_push(ary, ID2SYM(slot));
-    }
+    ary = struct_make_members_list(ar);
     va_end(ar);
 
     if (!name) st = anonymous_struct(rb_cStruct);
@@ -322,15 +327,9 @@ rb_struct_define_under(VALUE outer, cons https://github.com/ruby/ruby/blob/trunk/ruby_2_2/struct.c#L327
 {
     va_list ar;
     VALUE ary;
-    char *mem;
-
-    ary = rb_ary_tmp_new(0);
 
     va_start(ar, name);
-    while ((mem = va_arg(ar, char*)) != 0) {
-	ID slot = rb_intern(mem);
-	rb_ary_push(ary, ID2SYM(slot));
-    }
+    ary = struct_make_members_list(ar);
     va_end(ar);
 
     return setup_struct(rb_define_class_under(outer, name, rb_cStruct), ary);
@@ -391,7 +390,7 @@ rb_struct_s_def(int argc, VALUE *argv, V https://github.com/ruby/ruby/blob/trunk/ruby_2_2/struct.c#L390
     VALUE name, rest;
     long i;
     VALUE st;
-    ID id;
+    st_table *tbl;
 
     rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
     name = argv[0];
@@ -402,12 +401,18 @@ rb_struct_s_def(int argc, VALUE *argv, V https://github.com/ruby/ruby/blob/trunk/ruby_2_2/struct.c#L401
 	--argc;
 	++argv;
     }
-    rest = rb_ary_tmp_new(argc);
+    rest = rb_ident_hash_new();
+    RBASIC_CLEAR_CLASS(rest);
+    tbl = RHASH_TBL(rest);
     for (i=0; i<argc; i++) {
-	id = rb_to_id(argv[i]);
-	RARRAY_ASET(rest, i, ID2SYM(id));
-	rb_ary_set_len(rest, i+1);
+	VALUE mem = rb_to_symbol(argv[i]);
+	if (st_insert(tbl, mem, Qtrue)) {
+	    rb_raise(rb_eArgError, "duplicate member: %"PRIsVALUE, mem);
+	}
     }
+    rest = rb_hash_keys(rest);
+    st_clear(tbl);
+    RBASIC_CLEAR_CLASS(rest);
     if (NIL_P(name)) {
 	st = anonymous_struct(klass);
     }
Index: ruby_2_2/ext/-test-/struct/duplicate.c
===================================================================
--- ruby_2_2/ext/-test-/struct/duplicate.c	(revision 0)
+++ ruby_2_2/ext/-test-/struct/duplicate.c	(revision 54685)
@@ -0,0 +1,24 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_2/ext/-test-/struct/duplicate.c#L1
+#include "ruby.h"
+
+static VALUE
+bug_struct_new_duplicate(VALUE obj, VALUE name, VALUE mem)
+{
+    const char *n = NIL_P(name) ? 0 : StringValueCStr(name);
+    const char *m = StringValueCStr(mem);
+    return rb_struct_define(n, m, m, NULL);
+}
+
+static VALUE
+bug_struct_new_duplicate_under(VALUE obj, VALUE name, VALUE mem)
+{
+    const char *n = StringValueCStr(name);
+    const char *m = StringValueCStr(mem);
+    return rb_struct_define_under(obj, n, m, m, NULL);
+}
+
+void
+Init_duplicate(VALUE klass)
+{
+    rb_define_singleton_method(klass, "new_duplicate", bug_struct_new_duplicate, 2);
+    rb_define_singleton_method(klass, "new_duplicate_under", bug_struct_new_duplicate_under, 2);
+}

Property changes on: ruby_2_2/ext/-test-/struct/duplicate.c
___________________________________________________________________
Added: svn:eol-style
   + LF

Index: ruby_2_2/version.h
===================================================================
--- ruby_2_2/version.h	(revision 54684)
+++ ruby_2_2/version.h	(revision 54685)
@@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_2/version.h#L1
 #define RUBY_VERSION "2.2.5"
 #define RUBY_RELEASE_DATE "2016-04-22"
-#define RUBY_PATCHLEVEL 302
+#define RUBY_PATCHLEVEL 303
 
 #define RUBY_RELEASE_YEAR 2016
 #define RUBY_RELEASE_MONTH 4
Index: ruby_2_2/test/-ext-/struct/test_duplicate.rb
===================================================================
--- ruby_2_2/test/-ext-/struct/test_duplicate.rb	(revision 0)
+++ ruby_2_2/test/-ext-/struct/test_duplicate.rb	(revision 54685)
@@ -0,0 +1,22 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_2/test/-ext-/struct/test_duplicate.rb#L1
+# frozen_string_literal: false
+require 'test/unit'
+require "-test-/struct"
+
+class Bug::Struct::Test_Duplicate < Test::Unit::TestCase
+  def test_new_dupilicate
+    bug12291 = '[ruby-core:74971] [Bug #12291]'
+    assert_raise_with_message(ArgumentError, /duplicate member/, bug12291) {
+      Bug::Struct.new_duplicate(nil, "a")
+    }
+    assert_raise_with_message(ArgumentError, /duplicate member/, bug12291) {
+      Bug::Struct.new_duplicate("X", "a")
+    }
+  end
+
+  def test_new_dupilicate_under
+    bug12291 = '[ruby-core:74971] [Bug #12291]'
+    assert_raise_with_message(ArgumentError, /duplicate member/, bug12291) {
+      Bug::Struct.new_duplicate_under("x", "a")
+    }
+  end
+end

Property changes on: ruby_2_2/test/-ext-/struct/test_duplicate.rb
___________________________________________________________________
Added: svn:eol-style
   + LF

Index: ruby_2_2/test/ruby/test_struct.rb
===================================================================
--- ruby_2_2/test/ruby/test_struct.rb	(revision 54684)
+++ ruby_2_2/test/ruby/test_struct.rb	(revision 54685)
@@ -353,6 +353,13 @@ module TestStruct https://github.com/ruby/ruby/blob/trunk/ruby_2_2/test/ruby/test_struct.rb#L353
     assert_equal "[Bug #9353]", x.send(:a=, "[Bug #9353]")
   end
 
+  def test_new_dupilicate
+    bug12291 = '[ruby-core:74971] [Bug #12291]'
+    assert_raise_with_message(ArgumentError, /duplicate member/, bug12291) {
+      @Struct.new(:a, :a)
+    }
+  end
+
   class TopStruct < Test::Unit::TestCase
     include TestStruct
 

Property changes on: ruby_2_2
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /trunk:r54611


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

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