summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-time
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2016-11-24 13:50:36 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2016-11-24 13:50:36 +0900
commit23c73ff3998782f5e2a7f1f3755fddf8ceed0d30 (patch)
treeffe7a77279806852caa8ad5f385ba0ea7a28621a /mrbgems/mruby-time
parentcef7bd5416a2c477c48aa99a61427008ea1dc491 (diff)
downloadmruby-23c73ff3998782f5e2a7f1f3755fddf8ceed0d30.tar.gz
mruby-23c73ff3998782f5e2a7f1f3755fddf8ceed0d30.zip
Time#initialize_copy: Check if source time is initialized.
To prevent crash from nasty code like: class Time def initialize end end a = Time.new b = Time.new a.initialize_copy b
Diffstat (limited to 'mrbgems/mruby-time')
-rw-r--r--mrbgems/mruby-time/src/time.c13
1 files changed, 10 insertions, 3 deletions
diff --git a/mrbgems/mruby-time/src/time.c b/mrbgems/mruby-time/src/time.c
index 19ce32832..5c23bd44a 100644
--- a/mrbgems/mruby-time/src/time.c
+++ b/mrbgems/mruby-time/src/time.c
@@ -611,16 +611,23 @@ static mrb_value
mrb_time_initialize_copy(mrb_state *mrb, mrb_value copy)
{
mrb_value src;
+ struct mrb_time *t1, *t2;
mrb_get_args(mrb, "o", &src);
if (mrb_obj_equal(mrb, copy, src)) return copy;
if (!mrb_obj_is_instance_of(mrb, src, mrb_obj_class(mrb, copy))) {
mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class");
}
- if (!DATA_PTR(copy)) {
- mrb_data_init(copy, mrb_malloc(mrb, sizeof(struct mrb_time)), &mrb_time_type);
+ t1 = (struct mrb_time *)DATA_PTR(copy);
+ t2 = (struct mrb_time *)DATA_PTR(src);
+ if (!t2) {
+ mrb_raise(mrb, E_ARGUMENT_ERROR, "uninitialized time");
}
- *(struct mrb_time *)DATA_PTR(copy) = *(struct mrb_time *)DATA_PTR(src);
+ if (!t1) {
+ t1 = (struct mrb_time *)mrb_malloc(mrb, sizeof(struct mrb_time));
+ mrb_data_init(copy, t1, &mrb_time_type);
+ }
+ *t1 = *t2;
return copy;
}