@@ -171,9 +171,13 @@ pub mod linux {
171171 // mmap allocator for large frequent memory allocation and take jemalloc
172172 // as fallback.
173173 let raw = ffi:: rallocx ( ptr. cast ( ) . as_ptr ( ) , new_layout. size ( ) , flags) as * mut u8 ;
174- raw. add ( old_layout. size ( ) )
175- . write_bytes ( 0 , new_layout. size ( ) - old_layout. size ( ) ) ;
176- NonNull :: new ( raw as * mut ( ) ) . unwrap ( )
174+ if raw. is_null ( ) {
175+ return Err ( AllocError ) ;
176+ } else {
177+ raw. add ( old_layout. size ( ) )
178+ . write_bytes ( 0 , new_layout. size ( ) - old_layout. size ( ) ) ;
179+ NonNull :: new ( raw as * mut ( ) ) . unwrap ( )
180+ }
177181 } ;
178182
179183 Ok ( NonNull :: < [ u8 ] > :: from_raw_parts (
@@ -209,9 +213,13 @@ pub mod linux {
209213 } else {
210214 let data_address =
211215 ffi:: rallocx ( ptr. cast ( ) . as_ptr ( ) , new_layout. size ( ) , flags) as * mut u8 ;
212- let metadata = new_layout. size ( ) ;
213- let slice = std:: slice:: from_raw_parts_mut ( data_address, metadata) ;
214- NonNull :: new ( slice) . ok_or ( AllocError ) ?
216+ if data_address. is_null ( ) {
217+ return Err ( AllocError ) ;
218+ } else {
219+ let metadata = new_layout. size ( ) ;
220+ let slice = std:: slice:: from_raw_parts_mut ( data_address, metadata) ;
221+ NonNull :: new ( slice) . ok_or ( AllocError ) ?
222+ }
215223 } ;
216224
217225 Ok ( new_ptr)
0 commit comments