File tree 1 file changed +10
-3
lines changed
1 file changed +10
-3
lines changed Original file line number Diff line number Diff line change @@ -402,6 +402,11 @@ impl Lua {
402
402
return ptr:: null_mut ( ) ;
403
403
}
404
404
405
+ // Do not allocate more than isize::MAX
406
+ if nsize > isize:: MAX as usize {
407
+ return ptr:: null_mut ( ) ;
408
+ }
409
+
405
410
// Are we fit to the memory limits?
406
411
let mut mem_diff = nsize as isize ;
407
412
if !ptr. is_null ( ) {
@@ -411,12 +416,14 @@ impl Lua {
411
416
if mem_info. memory_limit > 0 && new_used_memory > mem_info. memory_limit {
412
417
return ptr:: null_mut ( ) ;
413
418
}
414
-
415
- let new_layout = Layout :: from_size_align_unchecked ( nsize, ffi:: SYS_MIN_ALIGN ) ;
416
419
mem_info. used_memory += mem_diff;
417
420
418
421
if ptr. is_null ( ) {
419
422
// Allocate new memory
423
+ let new_layout = match Layout :: from_size_align ( nsize, ffi:: SYS_MIN_ALIGN ) {
424
+ Ok ( layout) => layout,
425
+ Err ( _) => return ptr:: null_mut ( ) ,
426
+ } ;
420
427
let new_ptr = alloc:: alloc ( new_layout) as * mut c_void ;
421
428
if new_ptr. is_null ( ) {
422
429
alloc:: handle_alloc_error ( new_layout) ;
@@ -428,7 +435,7 @@ impl Lua {
428
435
let old_layout = Layout :: from_size_align_unchecked ( osize, ffi:: SYS_MIN_ALIGN ) ;
429
436
let new_ptr = alloc:: realloc ( ptr as * mut u8 , old_layout, nsize) as * mut c_void ;
430
437
if new_ptr. is_null ( ) {
431
- alloc:: handle_alloc_error ( new_layout ) ;
438
+ alloc:: handle_alloc_error ( old_layout ) ;
432
439
}
433
440
new_ptr
434
441
}
You can’t perform that action at this time.
0 commit comments