summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-random/src
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2020-08-07 22:59:27 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2020-10-12 16:21:31 +0900
commit67d92c48b5c6d9a1ca15ac431bead11fbfe2868b (patch)
tree75ec5679a852fbd5c9c8d4305085d0f4db400c03 /mrbgems/mruby-random/src
parent6fbc03b3a8f859eb34127d8b4c399a57f7d3d5d9 (diff)
downloadmruby-67d92c48b5c6d9a1ca15ac431bead11fbfe2868b.tar.gz
mruby-67d92c48b5c6d9a1ca15ac431bead11fbfe2868b.zip
Avoid breaking the `result` array by side-effect in C++.
Diffstat (limited to 'mrbgems/mruby-random/src')
-rw-r--r--mrbgems/mruby-random/src/random.c4
1 files changed, 3 insertions, 1 deletions
diff --git a/mrbgems/mruby-random/src/random.c b/mrbgems/mruby-random/src/random.c
index c91df35f3..fdb28e012 100644
--- a/mrbgems/mruby-random/src/random.c
+++ b/mrbgems/mruby-random/src/random.c
@@ -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;
}