1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

obj_init_copy

* object.c (rb_obj_init_copy): should check if trusted too.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35922 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2012-06-05 11:13:18 +00:00
parent b1ae6e473e
commit 2073258a7d
11 changed files with 58 additions and 27 deletions

View file

@ -1,3 +1,7 @@
Tue Jun 5 20:13:15 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
* object.c (rb_obj_init_copy): should check if trusted too.
Tue Jun 5 19:59:13 2012 Tanaka Akira <akr@fsij.org> Tue Jun 5 19:59:13 2012 Tanaka Akira <akr@fsij.org>
* process.c (strtok): declaration removed because it is not used. * process.c (strtok): declaration removed because it is not used.

View file

@ -373,6 +373,7 @@ enumerator_init_copy(VALUE obj, VALUE orig)
{ {
struct enumerator *ptr0, *ptr1; struct enumerator *ptr0, *ptr1;
if (!OBJ_INIT_COPY(obj, orig)) return obj;
ptr0 = enumerator_ptr(orig); ptr0 = enumerator_ptr(orig);
if (ptr0->fib) { if (ptr0->fib) {
/* Fibers cannot be copied */ /* Fibers cannot be copied */
@ -1146,6 +1147,8 @@ generator_init_copy(VALUE obj, VALUE orig)
{ {
struct generator *ptr0, *ptr1; struct generator *ptr0, *ptr1;
if (!OBJ_INIT_COPY(obj, orig)) return obj;
ptr0 = generator_ptr(orig); ptr0 = generator_ptr(orig);
TypedData_Get_Struct(obj, struct generator, &generator_data_type, ptr1); TypedData_Get_Struct(obj, struct generator, &generator_data_type, ptr1);

7
file.c
View file

@ -4470,12 +4470,7 @@ rb_stat_init_copy(VALUE copy, VALUE orig)
{ {
struct stat *nst; struct stat *nst;
if (copy == orig) return orig; if (!OBJ_INIT_COPY(copy, orig)) return copy;
rb_check_frozen(copy);
/* need better argument type check */
if (!rb_obj_is_instance_of(orig, rb_obj_class(copy))) {
rb_raise(rb_eTypeError, "wrong argument class");
}
if (DATA_PTR(copy)) { if (DATA_PTR(copy)) {
xfree(DATA_PTR(copy)); xfree(DATA_PTR(copy));
DATA_PTR(copy) = 0; DATA_PTR(copy) = 0;

View file

@ -252,6 +252,9 @@ rb_check_trusted_inline(VALUE obj)
#define rb_check_trusted(obj) rb_check_trusted_inline(obj) #define rb_check_trusted(obj) rb_check_trusted_inline(obj)
#endif #endif
#define OBJ_INIT_COPY(obj, orig) \
((obj) != (orig) && (rb_obj_init_copy((obj), (orig)), 1))
/* eval.c */ /* eval.c */
int rb_sourceline(void); int rb_sourceline(void);
const char *rb_sourcefile(void); const char *rb_sourcefile(void);

3
io.c
View file

@ -6387,7 +6387,7 @@ rb_io_init_copy(VALUE dest, VALUE io)
off_t pos; off_t pos;
io = rb_io_get_io(io); io = rb_io_get_io(io);
if (dest == io) return dest; if (!OBJ_INIT_COPY(dest, io)) return dest;
GetOpenFile(io, orig); GetOpenFile(io, orig);
MakeOpenFile(dest, fptr); MakeOpenFile(dest, fptr);
@ -7286,6 +7286,7 @@ argf_initialize(VALUE argf, VALUE argv)
static VALUE static VALUE
argf_initialize_copy(VALUE argf, VALUE orig) argf_initialize_copy(VALUE argf, VALUE orig)
{ {
if (!OBJ_INIT_COPY(argf, orig)) return argf;
ARGF = argf_of(orig); ARGF = argf_of(orig);
ARGF.argv = rb_obj_dup(ARGF.argv); ARGF.argv = rb_obj_dup(ARGF.argv);
if (ARGF.inplace) { if (ARGF.inplace) {

View file

@ -342,6 +342,7 @@ rb_obj_init_copy(VALUE obj, VALUE orig)
{ {
if (obj == orig) return obj; if (obj == orig) return obj;
rb_check_frozen(obj); rb_check_frozen(obj);
rb_check_trusted(obj);
if (TYPE(obj) != TYPE(orig) || rb_obj_class(obj) != rb_obj_class(orig)) { if (TYPE(obj) != TYPE(orig) || rb_obj_class(obj) != rb_obj_class(orig)) {
rb_raise(rb_eTypeError, "initialize_copy should take same class object"); rb_raise(rb_eTypeError, "initialize_copy should take same class object");
} }

View file

@ -596,9 +596,14 @@ random_get_seed(VALUE obj)
static VALUE static VALUE
random_copy(VALUE obj, VALUE orig) random_copy(VALUE obj, VALUE orig)
{ {
rb_random_t *rnd1 = get_rnd(obj); rb_random_t *rnd1, *rnd2;
rb_random_t *rnd2 = get_rnd(orig); struct MT *mt;
struct MT *mt = &rnd1->mt;
if (!OBJ_INIT_COPY(obj, orig)) return obj;
rnd1 = get_rnd(obj);
rnd2 = get_rnd(orig);
mt = &rnd1->mt;
*rnd1 = *rnd2; *rnd1 = *rnd2;
mt->next = mt->state + numberof(mt->state) - mt->left + 1; mt->next = mt->state + numberof(mt->state) - mt->left + 1;

12
re.c
View file

@ -939,11 +939,8 @@ match_init_copy(VALUE obj, VALUE orig)
{ {
struct rmatch *rm; struct rmatch *rm;
if (obj == orig) return obj; if (!OBJ_INIT_COPY(obj, orig)) return obj;
if (!rb_obj_is_instance_of(orig, rb_obj_class(obj))) {
rb_raise(rb_eTypeError, "wrong argument class");
}
RMATCH(obj)->str = RMATCH(orig)->str; RMATCH(obj)->str = RMATCH(orig)->str;
RMATCH(obj)->regexp = RMATCH(orig)->regexp; RMATCH(obj)->regexp = RMATCH(orig)->regexp;
@ -3260,12 +3257,7 @@ rb_reg_init_copy(VALUE copy, VALUE re)
const char *s; const char *s;
long len; long len;
if (copy == re) return copy; if (!OBJ_INIT_COPY(copy, re)) return copy;
rb_check_frozen(copy);
/* need better argument type check */
if (!rb_obj_is_instance_of(re, rb_obj_class(copy))) {
rb_raise(rb_eTypeError, "wrong argument type");
}
rb_reg_check(re); rb_reg_check(re);
s = RREGEXP_SRC_PTR(re); s = RREGEXP_SRC_PTR(re);
len = RREGEXP_SRC_LEN(re); len = RREGEXP_SRC_LEN(re);

View file

@ -606,11 +606,7 @@ rb_struct_to_h(VALUE s)
VALUE VALUE
rb_struct_init_copy(VALUE copy, VALUE s) rb_struct_init_copy(VALUE copy, VALUE s)
{ {
if (copy == s) return copy; if (!OBJ_INIT_COPY(copy, s)) return copy;
rb_check_frozen(copy);
if (!rb_obj_is_instance_of(s, rb_obj_class(copy))) {
rb_raise(rb_eTypeError, "wrong argument class");
}
if (RSTRUCT_LEN(copy) != RSTRUCT_LEN(s)) { if (RSTRUCT_LEN(copy) != RSTRUCT_LEN(s)) {
rb_raise(rb_eTypeError, "struct size mismatch"); rb_raise(rb_eTypeError, "struct size mismatch");
} }

View file

@ -764,4 +764,36 @@ class TestObject < Test::Unit::TestCase
assert_equal((eval("raise #{code}") rescue $!.inspect), out.chomp, bug5473) assert_equal((eval("raise #{code}") rescue $!.inspect), out.chomp, bug5473)
end end
end end
def assert_not_initialize_copy
a = yield
b = yield
assert_nothing_raised("copy") {a.instance_eval {initialize_copy(b)}}
c = a.dup.freeze
assert_raise(RuntimeError, "frozen") {c.instance_eval {initialize_copy(b)}}
d = a.dup.trust
assert_raise(SecurityError, "untrust") do
proc {
$SAFE = 4
d.instance_eval {initialize_copy(b)}
}.call
end
[a, b, c, d]
end
def test_bad_initialize_copy
assert_not_initialize_copy {Object.new}
assert_not_initialize_copy {[].to_enum}
assert_not_initialize_copy {Enumerator::Generator.new {}}
assert_not_initialize_copy {Enumerator::Yielder.new {}}
assert_not_initialize_copy {File.stat(__FILE__)}
assert_not_initialize_copy {open(__FILE__)}.each(&:close)
assert_not_initialize_copy {ARGF.class.new}
assert_not_initialize_copy {Random.new}
assert_not_initialize_copy {//}
assert_not_initialize_copy {/.*/.match("foo")}
st = Struct.new(:foo)
assert_not_initialize_copy {st.new}
assert_not_initialize_copy {Time.now}
end
end end

3
time.c
View file

@ -3425,8 +3425,7 @@ time_init_copy(VALUE copy, VALUE time)
{ {
struct time_object *tobj, *tcopy; struct time_object *tobj, *tcopy;
if (copy == time) return copy; if (!OBJ_INIT_COPY(copy, time)) return copy;
time_modify(copy);
GetTimeval(time, tobj); GetTimeval(time, tobj);
GetTimeval(copy, tcopy); GetTimeval(copy, tcopy);
MEMCPY(tcopy, tobj, struct time_object, 1); MEMCPY(tcopy, tobj, struct time_object, 1);