@@ -52,11 +52,15 @@ pub unsafe extern "C" fn malloc(len: usize) -> *mut c_void {
5252 -1 ,
5353 0 ,
5454 ) ;
55- // This is guaranteed to be aligned
56- * ( ptr as * mut usize ) = full_len;
57- ptr = ptr. offset ( CANARY_SIZE as isize ) ;
58- libc:: memset ( ptr, get_mem_init ( ) . into ( ) , len + alloc_extra_mem) ;
59- ptr
55+ if ptr == libc:: MAP_FAILED {
56+ libc:: PT_NULL as * mut libc:: c_void
57+ } else {
58+ // This is guaranteed to be aligned
59+ * ( ptr as * mut usize ) = full_len;
60+ ptr = ptr. offset ( CANARY_SIZE as isize ) ;
61+ libc:: memset ( ptr, get_mem_init ( ) . into ( ) , len + alloc_extra_mem) ;
62+ ptr
63+ }
6064}
6165
6266#[ no_mangle]
@@ -78,16 +82,20 @@ pub unsafe extern "C" fn calloc(n_items: usize, item_len: usize) -> *mut c_void
7882 -1 ,
7983 0 ,
8084 ) ;
81- // This is guaranteed to be aligned
82- * ( ptr as * mut usize ) = full_len;
83- ptr = ptr. offset ( CANARY_SIZE as isize ) ;
84- libc:: memset ( ptr, 0 , len) ;
85- libc:: memset (
86- ptr. offset ( len as isize ) ,
87- get_mem_init ( ) . into ( ) ,
88- alloc_extra_mem,
89- ) ;
90- ptr
85+ if ptr == libc:: MAP_FAILED {
86+ libc:: PT_NULL as * mut libc:: c_void
87+ } else {
88+ // This is guaranteed to be aligned
89+ * ( ptr as * mut usize ) = full_len;
90+ ptr = ptr. offset ( CANARY_SIZE as isize ) ;
91+ libc:: memset ( ptr, 0 , len) ;
92+ libc:: memset (
93+ ptr. offset ( len as isize ) ,
94+ get_mem_init ( ) . into ( ) ,
95+ alloc_extra_mem,
96+ ) ;
97+ ptr
98+ }
9199}
92100
93101#[ no_mangle]
0 commit comments