mirror of
https://github.com/ruby/ruby.git
synced 2025-09-17 17:43:59 +02:00
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] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_3@54637 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
79e826d7f1
commit
9dc33b83bb
6 changed files with 99 additions and 34 deletions
|
@ -1,3 +1,12 @@
|
||||||
|
Mon Apr 18 18:05:29 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
|
||||||
|
intermediate IDs.
|
||||||
|
|
||||||
Mon Apr 18 17:54:40 2016 Joe Swatosh <joe.swatosh@gmail.com>
|
Mon Apr 18 17:54:40 2016 Joe Swatosh <joe.swatosh@gmail.com>
|
||||||
|
|
||||||
* ext/win32/lib/win32/registry.rb (DeleteValue, DeleteKey): fix
|
* ext/win32/lib/win32/registry.rb (DeleteValue, DeleteKey): fix
|
||||||
|
|
24
ext/-test-/struct/duplicate.c
Normal file
24
ext/-test-/struct/duplicate.c
Normal file
|
@ -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);
|
||||||
|
}
|
69
struct.c
69
struct.c
|
@ -330,6 +330,27 @@ rb_struct_alloc_noinit(VALUE klass)
|
||||||
return struct_alloc(klass);
|
return struct_alloc(klass);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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_sym_intern_ascii_cstr(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
|
static VALUE
|
||||||
struct_define_without_accessor(VALUE outer, const char *class_name, VALUE super, rb_alloc_func_t alloc, VALUE members)
|
struct_define_without_accessor(VALUE outer, const char *class_name, VALUE super, rb_alloc_func_t alloc, VALUE members)
|
||||||
{
|
{
|
||||||
|
@ -364,15 +385,10 @@ rb_struct_define_without_accessor_under(VALUE outer, const char *class_name, VAL
|
||||||
{
|
{
|
||||||
va_list ar;
|
va_list ar;
|
||||||
VALUE members;
|
VALUE members;
|
||||||
char *name;
|
|
||||||
|
|
||||||
members = rb_ary_tmp_new(0);
|
|
||||||
va_start(ar, alloc);
|
va_start(ar, alloc);
|
||||||
while ((name = va_arg(ar, char*)) != NULL) {
|
members = struct_make_members_list(ar);
|
||||||
rb_ary_push(members, ID2SYM(rb_intern(name)));
|
|
||||||
}
|
|
||||||
va_end(ar);
|
va_end(ar);
|
||||||
OBJ_FREEZE_RAW(members);
|
|
||||||
|
|
||||||
return struct_define_without_accessor(outer, class_name, super, alloc, members);
|
return struct_define_without_accessor(outer, class_name, super, alloc, members);
|
||||||
}
|
}
|
||||||
|
@ -382,15 +398,10 @@ rb_struct_define_without_accessor(const char *class_name, VALUE super, rb_alloc_
|
||||||
{
|
{
|
||||||
va_list ar;
|
va_list ar;
|
||||||
VALUE members;
|
VALUE members;
|
||||||
char *name;
|
|
||||||
|
|
||||||
members = rb_ary_tmp_new(0);
|
|
||||||
va_start(ar, alloc);
|
va_start(ar, alloc);
|
||||||
while ((name = va_arg(ar, char*)) != NULL) {
|
members = struct_make_members_list(ar);
|
||||||
rb_ary_push(members, ID2SYM(rb_intern(name)));
|
|
||||||
}
|
|
||||||
va_end(ar);
|
va_end(ar);
|
||||||
OBJ_FREEZE_RAW(members);
|
|
||||||
|
|
||||||
return struct_define_without_accessor(0, class_name, super, alloc, members);
|
return struct_define_without_accessor(0, class_name, super, alloc, members);
|
||||||
}
|
}
|
||||||
|
@ -400,17 +411,10 @@ rb_struct_define(const char *name, ...)
|
||||||
{
|
{
|
||||||
va_list ar;
|
va_list ar;
|
||||||
VALUE st, ary;
|
VALUE st, ary;
|
||||||
char *mem;
|
|
||||||
|
|
||||||
ary = rb_ary_tmp_new(0);
|
|
||||||
|
|
||||||
va_start(ar, name);
|
va_start(ar, name);
|
||||||
while ((mem = va_arg(ar, char*)) != 0) {
|
ary = struct_make_members_list(ar);
|
||||||
ID slot = rb_intern(mem);
|
|
||||||
rb_ary_push(ary, ID2SYM(slot));
|
|
||||||
}
|
|
||||||
va_end(ar);
|
va_end(ar);
|
||||||
OBJ_FREEZE_RAW(ary);
|
|
||||||
|
|
||||||
if (!name) st = anonymous_struct(rb_cStruct);
|
if (!name) st = anonymous_struct(rb_cStruct);
|
||||||
else st = new_struct(rb_str_new2(name), rb_cStruct);
|
else st = new_struct(rb_str_new2(name), rb_cStruct);
|
||||||
|
@ -422,17 +426,10 @@ rb_struct_define_under(VALUE outer, const char *name, ...)
|
||||||
{
|
{
|
||||||
va_list ar;
|
va_list ar;
|
||||||
VALUE ary;
|
VALUE ary;
|
||||||
char *mem;
|
|
||||||
|
|
||||||
ary = rb_ary_tmp_new(0);
|
|
||||||
|
|
||||||
va_start(ar, name);
|
va_start(ar, name);
|
||||||
while ((mem = va_arg(ar, char*)) != 0) {
|
ary = struct_make_members_list(ar);
|
||||||
ID slot = rb_intern(mem);
|
|
||||||
rb_ary_push(ary, ID2SYM(slot));
|
|
||||||
}
|
|
||||||
va_end(ar);
|
va_end(ar);
|
||||||
OBJ_FREEZE_RAW(ary);
|
|
||||||
|
|
||||||
return setup_struct(rb_define_class_under(outer, name, rb_cStruct), ary);
|
return setup_struct(rb_define_class_under(outer, name, rb_cStruct), ary);
|
||||||
}
|
}
|
||||||
|
@ -492,7 +489,7 @@ rb_struct_s_def(int argc, VALUE *argv, VALUE klass)
|
||||||
VALUE name, rest;
|
VALUE name, rest;
|
||||||
long i;
|
long i;
|
||||||
VALUE st;
|
VALUE st;
|
||||||
ID id;
|
st_table *tbl;
|
||||||
|
|
||||||
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
|
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
|
||||||
name = argv[0];
|
name = argv[0];
|
||||||
|
@ -503,12 +500,18 @@ rb_struct_s_def(int argc, VALUE *argv, VALUE klass)
|
||||||
--argc;
|
--argc;
|
||||||
++argv;
|
++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++) {
|
for (i=0; i<argc; i++) {
|
||||||
id = rb_to_id(argv[i]);
|
VALUE mem = rb_to_symbol(argv[i]);
|
||||||
RARRAY_ASET(rest, i, ID2SYM(id));
|
if (st_insert(tbl, mem, Qtrue)) {
|
||||||
rb_ary_set_len(rest, i+1);
|
rb_raise(rb_eArgError, "duplicate member: %"PRIsVALUE, mem);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
rest = rb_hash_keys(rest);
|
||||||
|
st_clear(tbl);
|
||||||
|
RBASIC_CLEAR_CLASS(rest);
|
||||||
OBJ_FREEZE_RAW(rest);
|
OBJ_FREEZE_RAW(rest);
|
||||||
if (NIL_P(name)) {
|
if (NIL_P(name)) {
|
||||||
st = anonymous_struct(klass);
|
st = anonymous_struct(klass);
|
||||||
|
|
22
test/-ext-/struct/test_duplicate.rb
Normal file
22
test/-ext-/struct/test_duplicate.rb
Normal file
|
@ -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
|
|
@ -367,6 +367,13 @@ module TestStruct
|
||||||
assert_nil(o.dig(:b, 0))
|
assert_nil(o.dig(:b, 0))
|
||||||
end
|
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
|
class TopStruct < Test::Unit::TestCase
|
||||||
include TestStruct
|
include TestStruct
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#define RUBY_VERSION "2.3.0"
|
#define RUBY_VERSION "2.3.0"
|
||||||
#define RUBY_RELEASE_DATE "2016-04-18"
|
#define RUBY_RELEASE_DATE "2016-04-18"
|
||||||
#define RUBY_PATCHLEVEL 92
|
#define RUBY_PATCHLEVEL 93
|
||||||
|
|
||||||
#define RUBY_RELEASE_YEAR 2016
|
#define RUBY_RELEASE_YEAR 2016
|
||||||
#define RUBY_RELEASE_MONTH 4
|
#define RUBY_RELEASE_MONTH 4
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue