@@ -19,7 +19,7 @@ use crate::pyobject::{
19
19
PyObjectRef , PyRef , PyResult , PyValue , TryFromObject , TypeProtocol ,
20
20
} ;
21
21
use crate :: sequence:: { self , SimpleSeq } ;
22
- use crate :: vm:: { ReprGuard , VirtualMachine } ;
22
+ use crate :: vm:: { ReprGuard , VirtualMachine , MAX_MEMORY_SIZE } ;
23
23
24
24
/// Built-in mutable sequence.
25
25
///
@@ -119,14 +119,16 @@ impl PyList {
119
119
Ok ( result) => match result. as_bigint ( ) . to_u8 ( ) {
120
120
Some ( result) => elements. push ( result) ,
121
121
None => {
122
- return Err ( vm. new_value_error ( "bytes must be in range (0, 256)" . to_owned ( ) ) )
122
+ return Err (
123
+ vm. new_value_error ( "bytes must be in range (0, 256)" . to_owned ( ) )
124
+ ) ;
123
125
}
124
126
} ,
125
127
_ => {
126
128
return Err ( vm. new_type_error ( format ! (
127
129
"'{}' object cannot be interpreted as an integer" ,
128
130
elem. class( ) . name
129
- ) ) )
131
+ ) ) ) ;
130
132
}
131
133
}
132
134
}
@@ -468,25 +470,31 @@ impl PyList {
468
470
}
469
471
470
472
#[ pymethod( name = "__mul__" ) ]
471
- fn mul ( & self , counter : isize , vm : & VirtualMachine ) -> PyObjectRef {
472
- let new_elements = sequence:: seq_mul ( & self . borrow_sequence ( ) , counter)
473
- . cloned ( )
474
- . collect ( ) ;
475
- vm. ctx . new_list ( new_elements)
473
+ fn mul ( & self , counter : isize , vm : & VirtualMachine ) -> PyResult < PyObjectRef > {
474
+ if counter < 0 || self . len ( ) * ( counter as usize ) < MAX_MEMORY_SIZE {
475
+ let new_elements = sequence:: seq_mul ( & self . borrow_sequence ( ) , counter)
476
+ . cloned ( )
477
+ . collect ( ) ;
478
+ return Ok ( vm. ctx . new_list ( new_elements) ) ;
479
+ }
480
+ return Err ( vm. new_memory_error ( "" . to_owned ( ) ) ) ;
476
481
}
477
482
478
483
#[ pymethod( name = "__rmul__" ) ]
479
- fn rmul ( & self , counter : isize , vm : & VirtualMachine ) -> PyObjectRef {
484
+ fn rmul ( & self , counter : isize , vm : & VirtualMachine ) -> PyResult < PyObjectRef > {
480
485
self . mul ( counter, & vm)
481
486
}
482
487
483
488
#[ pymethod( name = "__imul__" ) ]
484
- fn imul ( zelf : PyRef < Self > , counter : isize ) -> PyRef < Self > {
485
- let new_elements = sequence:: seq_mul ( & zelf. borrow_sequence ( ) , counter)
486
- . cloned ( )
487
- . collect ( ) ;
488
- zelf. elements . replace ( new_elements) ;
489
- zelf
489
+ fn imul ( zelf : PyRef < Self > , counter : isize ) -> PyResult < Self > {
490
+ if counter < 0 || zelf. len ( ) * ( counter as usize ) < MAX_MEMORY_SIZE {
491
+ let new_elements = sequence:: seq_mul ( & zelf. borrow_sequence ( ) , counter)
492
+ . cloned ( )
493
+ . collect ( ) ;
494
+ zelf. elements . replace ( new_elements) ;
495
+ return Ok ( * zelf) ;
496
+ }
497
+ return Err ( vm. new_memory_error ( "" . to_owned ( )) ) ;
490
498
}
491
499
492
500
#[ pymethod]
0 commit comments