Commit 2cb183f1 authored by Masaki Muranaka's avatar Masaki Muranaka

Avoid memcpy() on copying structures.

parent 0debca9b
...@@ -94,6 +94,16 @@ mrb_struct_s_members_m(mrb_state *mrb, mrb_value klass) ...@@ -94,6 +94,16 @@ mrb_struct_s_members_m(mrb_state *mrb, mrb_value klass)
return ary; return ary;
} }
static inline void
struct_copy(mrb_value *dst, const mrb_value *src, size_t size)
{
size_t i;
for (i = 0; i < size; i++) {
dst[i] = src[i];
}
}
/* 15.2.18.4.6 */ /* 15.2.18.4.6 */
/* /*
* call-seq: * call-seq:
...@@ -431,7 +441,7 @@ mrb_struct_initialize_withArg(mrb_state *mrb, int argc, mrb_value *argv, mrb_val ...@@ -431,7 +441,7 @@ mrb_struct_initialize_withArg(mrb_state *mrb, int argc, mrb_value *argv, mrb_val
st = RSTRUCT(self); st = RSTRUCT(self);
st->ptr = (mrb_value *)mrb_calloc(mrb, sizeof(mrb_value), n); st->ptr = (mrb_value *)mrb_calloc(mrb, sizeof(mrb_value), n);
st->len = n; st->len = n;
memcpy(st->ptr, argv, sizeof(mrb_value)*argc); struct_copy(st->ptr, argv, argc);
return self; return self;
} }
...@@ -530,7 +540,7 @@ mrb_struct_init_copy(mrb_state *mrb, mrb_value copy) ...@@ -530,7 +540,7 @@ mrb_struct_init_copy(mrb_state *mrb, mrb_value copy)
if (RSTRUCT_LEN(copy) != RSTRUCT_LEN(s)) { if (RSTRUCT_LEN(copy) != RSTRUCT_LEN(s)) {
mrb_raise(mrb, E_TYPE_ERROR, "struct size mismatch"); mrb_raise(mrb, E_TYPE_ERROR, "struct size mismatch");
} }
memcpy(RSTRUCT_PTR(copy), RSTRUCT_PTR(s), sizeof(mrb_value)*RSTRUCT_LEN(copy)); struct_copy(RSTRUCT_PTR(copy), RSTRUCT_PTR(s), RSTRUCT_LEN(copy));
return copy; return copy;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment