Skip to content

Commit 75acdb8

Browse files
committed
Fix SETRAND/ADDRAND when c7 is empty
1 parent 3d3cf6c commit 75acdb8

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

vm/src/instr/randops.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,13 @@ impl RandOps {
106106
SafeRc::make_mut(&mut c7)[0] = Stack::make_null();
107107

108108
let mut t1v = t1v.into_tuple().expect("t1 was checked as tuple");
109-
SafeRc::make_mut(&mut t1v)[SmcInfoBase::RANDSEED_IDX] = int.into_dyn_value();
109+
{
110+
let t1v = SafeRc::make_mut(&mut t1v);
111+
if t1v.len() <= SmcInfoBase::RANDSEED_IDX {
112+
t1v.resize_with(SmcInfoBase::RANDSEED_IDX + 1, Stack::make_null);
113+
}
114+
t1v[SmcInfoBase::RANDSEED_IDX] = int.into_dyn_value();
115+
}
110116
let t1_len = t1v.len();
111117

112118
// NOTE: Restore c7 and control registers state.
@@ -207,6 +213,15 @@ pub mod test {
207213
#[test]
208214
#[traced_test]
209215
fn random() {
216+
assert_run_vm!("INT 123 RAND", c7: tuple![], [] => [int 0], exit_code: 7);
217+
assert_run_vm!("INT 123 RAND", c7: tuple![[]], [] => [int 0], exit_code: 7);
218+
219+
assert_run_vm!("INT 123 SETRAND", c7: tuple![], [] => [int 0], exit_code: 7);
220+
assert_run_vm!("INT 123 SETRAND", c7: tuple![[]], [] => [], exit_code: 0);
221+
222+
assert_run_vm!("INT 123 ADDRAND", c7: tuple![], [] => [int 0], exit_code: 7);
223+
assert_run_vm!("INT 123 ADDRAND", c7: tuple![[]], [] => [int 0], exit_code: 7);
224+
210225
let value = uint256("576f8d6b5ac3bcc80844b7d50b1cc6603444bbe7cfcf8fc0aa1ee3c636d9e339");
211226
let c7 = tuple![[
212227
null, // 0

0 commit comments

Comments
 (0)