@@ -23,10 +23,8 @@ use crate::{
23
23
gas:: { Gas , GasMeter , Token , GasMeterResult , ChargedAmount } ,
24
24
wasm:: env_def:: ConvertibleToWasm ,
25
25
} ;
26
- use sp_sandbox;
27
26
use parity_wasm:: elements:: ValueType ;
28
- use frame_system;
29
- use frame_support:: dispatch:: DispatchError ;
27
+ use frame_support:: { dispatch:: DispatchError , ensure} ;
30
28
use sp_std:: prelude:: * ;
31
29
use codec:: { Decode , DecodeAll , Encode } ;
32
30
use sp_runtime:: traits:: SaturatedConversion ;
@@ -420,6 +418,7 @@ where
420
418
pub fn read_sandbox_memory ( & self , ptr : u32 , len : u32 )
421
419
-> Result < Vec < u8 > , DispatchError >
422
420
{
421
+ ensure ! ( len <= self . schedule. limits. max_memory_size( ) , Error :: <E :: T >:: OutOfBounds ) ;
423
422
let mut buf = vec ! [ 0u8 ; len as usize ] ;
424
423
self . memory . get ( ptr, buf. as_mut_slice ( ) )
425
424
. map_err ( |_| Error :: < E :: T > :: OutOfBounds ) ?;
@@ -1179,17 +1178,23 @@ define_env!(Env, <E: Ext>,
1179
1178
let rent_allowance: BalanceOf <<E as Ext >:: T > =
1180
1179
ctx. read_sandbox_memory_as( rent_allowance_ptr, rent_allowance_len) ?;
1181
1180
let delta = {
1181
+ const KEY_SIZE : usize = 32 ;
1182
+
1182
1183
// We can eagerly allocate because we charged for the complete delta count already
1183
- let mut delta = Vec :: with_capacity( delta_count as usize ) ;
1184
+ // We still need to make sure that the allocation isn't larger than the memory
1185
+ // allocator can handle.
1186
+ ensure!(
1187
+ delta_count
1188
+ . saturating_mul( KEY_SIZE as u32 ) <= ctx. schedule. limits. max_memory_size( ) ,
1189
+ Error :: <E :: T >:: OutOfBounds ,
1190
+ ) ;
1191
+ let mut delta = vec![ [ 0 ; KEY_SIZE ] ; delta_count as usize ] ;
1184
1192
let mut key_ptr = delta_ptr;
1185
1193
1186
- for _ in 0 ..delta_count {
1187
- const KEY_SIZE : usize = 32 ;
1188
-
1189
- // Read the delta into the provided buffer and collect it into the buffer.
1190
- let mut delta_key: StorageKey = [ 0 ; KEY_SIZE ] ;
1191
- ctx. read_sandbox_memory_into_buf( key_ptr, & mut delta_key) ?;
1192
- delta. push( delta_key) ;
1194
+ for i in 0 ..delta_count {
1195
+ // Read the delta into the provided buffer
1196
+ // This cannot panic because of the loop condition
1197
+ ctx. read_sandbox_memory_into_buf( key_ptr, & mut delta[ i as usize ] ) ?;
1193
1198
1194
1199
// Offset key_ptr to the next element.
1195
1200
key_ptr = key_ptr. checked_add( KEY_SIZE as u32 ) . ok_or( Error :: <E :: T >:: OutOfBounds ) ?;
0 commit comments