Avoid breaking the result array by side-effect in C++.

This commit is contained in:
Yukihiro "Matz" Matsumoto
2020-08-07 22:59:27 +09:00
parent 6fbc03b3a8
commit 67d92c48b5
+3 -1
View File
@@ -357,7 +357,9 @@ mrb_ary_sample(mrb_state *mrb, mrb_value ary)
mrb_ary_push(mrb, result, mrb_fixnum_value(r));
}
for (i=0; i<n; i++) {
mrb_ary_set(mrb, result, i, RARRAY_PTR(ary)[mrb_fixnum(RARRAY_PTR(result)[i])]);
mrb_int idx = mrb_fixnum(RARRAY_PTR(result)[i]);
mrb_value elem = RARRAY_PTR(ary)[idx];
mrb_ary_set(mrb, result, i, elem);
}
return result;
}