aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-04-16 01:00:13 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-04-16 01:00:13 +0000
commitda2e0721b1ba94e8e786224469fe4bf7550c996a (patch)
tree6251858faae77bd6f9b9cfeaf71a5426906af6ab
parentc746d5027d0a57ef5996b3394e7c6a2f2b96df0d (diff)
downloadruby-da2e0721b1ba94e8e786224469fe4bf7550c996a.tar.gz
struct.c: unique members
* struct.c (struct_make_members_list, rb_struct_s_def): member names should be unique. [ruby-core:74971] [Bug #12291] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54612 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--ext/-test-/struct/duplicate.c24
-rw-r--r--struct.c27
-rw-r--r--test/-ext-/struct/test_duplicate.rb22
-rw-r--r--test/ruby/test_struct.rb7
5 files changed, 77 insertions, 8 deletions
diff --git a/ChangeLog b/ChangeLog
index 0e1f1dec32..cb107431f0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,7 @@
-Sat Apr 16 09:59:40 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
+Sat Apr 16 10:00:11 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * struct.c (struct_make_members_list, rb_struct_s_def): member
+ names should be unique. [ruby-core:74971] [Bug #12291]
* struct.c (struct_make_members_list): extract making member name
list from char* va_list, with creating symbols without
diff --git a/ext/-test-/struct/duplicate.c b/ext/-test-/struct/duplicate.c
new file mode 100644
index 0000000000..596d32aad8
--- /dev/null
+++ b/ext/-test-/struct/duplicate.c
@@ -0,0 +1,24 @@
+#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);
+}
diff --git a/struct.c b/struct.c
index ee63f4e148..a4cab45945 100644
--- a/struct.c
+++ b/struct.c
@@ -334,12 +334,19 @@ static VALUE
struct_make_members_list(va_list ar)
{
char *mem;
- VALUE ary = rb_ary_tmp_new(0);
+ 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_sym_intern_ascii_cstr(mem);
- rb_ary_push(ary, sym);
+ 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;
}
@@ -482,7 +489,7 @@ rb_struct_s_def(int argc, VALUE *argv, VALUE klass)
VALUE name, rest;
long i;
VALUE st;
- ID id;
+ st_table *tbl;
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
name = argv[0];
@@ -493,12 +500,18 @@ rb_struct_s_def(int argc, VALUE *argv, VALUE klass)
--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);
OBJ_FREEZE_RAW(rest);
if (NIL_P(name)) {
st = anonymous_struct(klass);
diff --git a/test/-ext-/struct/test_duplicate.rb b/test/-ext-/struct/test_duplicate.rb
new file mode 100644
index 0000000000..77d61988b6
--- /dev/null
+++ b/test/-ext-/struct/test_duplicate.rb
@@ -0,0 +1,22 @@
+# 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
diff --git a/test/ruby/test_struct.rb b/test/ruby/test_struct.rb
index 38632a1981..1223d4b816 100644
--- a/test/ruby/test_struct.rb
+++ b/test/ruby/test_struct.rb
@@ -367,6 +367,13 @@ module TestStruct
assert_nil(o.dig(:b, 0))
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