fix according to the comments

This commit is contained in:
Guanqun Lu 2018-09-18 23:32:35 +08:00
parent b3cf9ccb9d
commit 039c75589c
1 changed files with 39 additions and 9 deletions

View File

@ -1,6 +1,5 @@
use std::ops; use std::ops;
use std::{u32, usize}; use std::{u32, usize};
use std::cmp;
use std::fmt; use std::fmt;
use std::iter::repeat; use std::iter::repeat;
use parity_wasm::elements::Local; use parity_wasm::elements::Local;
@ -1262,13 +1261,15 @@ impl ValueStack {
#[inline] #[inline]
fn extend(&mut self, values: &[RuntimeValue]) -> Result<(), TrapKind> { fn extend(&mut self, values: &[RuntimeValue]) -> Result<(), TrapKind> {
let len = cmp::min(values.len(), self.buf.len() - self.sp); let avail_len = self.buf.len() - self.sp;
self.buf[self.sp..(self.sp+len)].copy_from_slice(&values[..len]); let to_copy_len = values.len();
self.sp += len;
if len < values.len() { if to_copy_len <= avail_len {
Err(TrapKind::StackOverflow) self.buf[self.sp..(self.sp+to_copy_len)].copy_from_slice(&values[..to_copy_len]);
} else { self.sp += to_copy_len;
Ok(()) Ok(())
} else {
Err(TrapKind::StackOverflow)
} }
} }
@ -1286,8 +1287,13 @@ mod tests {
#[test] #[test]
fn test_stack_extend() { fn test_stack_extend() {
let mut stack = ValueStack::with_limit(5); let mut stack = ValueStack::with_limit(5);
let values = vec![RuntimeValue::I32(1), RuntimeValue::I32(2), RuntimeValue::I32(3), let values = vec![
RuntimeValue::I32(4), RuntimeValue::I32(5)]; RuntimeValue::I32(1),
RuntimeValue::I32(2),
RuntimeValue::I32(3),
RuntimeValue::I32(4),
RuntimeValue::I32(5)
];
assert!(stack.extend(&values).is_ok()); assert!(stack.extend(&values).is_ok());
assert_eq!(stack.len(), 5); assert_eq!(stack.len(), 5);
@ -1303,4 +1309,28 @@ mod tests {
assert_eq!(stack.len(), 0); assert_eq!(stack.len(), 0);
} }
#[test]
fn test_stack_extend_overflow() {
let mut stack = ValueStack::with_limit(4);
let values = vec![
RuntimeValue::I32(1),
RuntimeValue::I32(2),
RuntimeValue::I32(3),
RuntimeValue::I32(4),
RuntimeValue::I32(5)
];
assert!(stack.extend(&values).is_err());
assert_eq!(stack.len(), 0);
assert!(stack.push(RuntimeValue::I32(6)).is_ok());
assert_eq!(stack.len(), 1);
// check each elements
assert_eq!(stack.pop(), RuntimeValue::I32(6));
assert_eq!(stack.len(), 0);
}
} }