@@ -29,6 +29,8 @@ pub enum NSTDAllocError {
2929 NSTD_ALLOC_ERROR_HEAP_NOT_FOUND ,
3030 /// A heap is invalid.
3131 NSTD_ALLOC_ERROR_INVALID_HEAP ,
32+ /// An allocation function received input parameters that resulted in an invalid memory layout.
33+ NSTD_ALLOC_ERROR_INVALID_LAYOUT ,
3234}
3335impl NSTDAllocError {
3436 /// Converts an [NSTDWindowsAllocError] into an [NSTDAllocError].
@@ -77,7 +79,12 @@ impl NSTDAllocError {
7779pub unsafe extern "C" fn nstd_alloc_allocate ( size : NSTDUInt ) -> NSTDAnyMut {
7880 #[ cfg( not( any( target_family = "unix" , target_os = "windows" ) ) ) ]
7981 {
82+ use crate :: NSTD_NULL ;
8083 use alloc:: alloc:: Layout ;
84+ // Make sure `size` is valid for `layout`.
85+ if size > isize:: MAX as usize {
86+ return NSTD_NULL ;
87+ }
8188 let layout = Layout :: from_size_align_unchecked ( size, 1 ) ;
8289 alloc:: alloc:: alloc ( layout) . cast ( )
8390 }
@@ -125,7 +132,12 @@ pub unsafe extern "C" fn nstd_alloc_allocate(size: NSTDUInt) -> NSTDAnyMut {
125132pub unsafe extern "C" fn nstd_alloc_allocate_zeroed ( size : NSTDUInt ) -> NSTDAnyMut {
126133 #[ cfg( not( any( target_family = "unix" , target_os = "windows" ) ) ) ]
127134 {
135+ use crate :: NSTD_NULL ;
128136 use alloc:: alloc:: Layout ;
137+ // Make sure `size` is valid for `layout`.
138+ if size > isize:: MAX as usize {
139+ return NSTD_NULL ;
140+ }
129141 let layout = Layout :: from_size_align_unchecked ( size, 1 ) ;
130142 alloc:: alloc:: alloc_zeroed ( layout) . cast ( )
131143 }
@@ -186,7 +198,7 @@ pub unsafe extern "C" fn nstd_alloc_allocate_zeroed(size: NSTDUInt) -> NSTDAnyMu
186198/// nstd_alloc_deallocate(&mut mem, SIZE);
187199/// }
188200/// ```
189- #[ inline]
201+ #[ cfg_attr ( any ( target_family = "unix" , target_os = "windows" ) , inline) ]
190202#[ cfg_attr( feature = "clib" , no_mangle) ]
191203#[ cfg_attr(
192204 any( target_family = "unix" , target_os = "windows" ) ,
@@ -200,6 +212,10 @@ pub unsafe extern "C" fn nstd_alloc_reallocate(
200212 #[ cfg( not( any( target_family = "unix" , target_os = "windows" ) ) ) ]
201213 {
202214 use alloc:: alloc:: Layout ;
215+ // Make sure `size` is valid for `layout`.
216+ if size > isize:: MAX as usize {
217+ return NSTDAllocError :: NSTD_ALLOC_ERROR_INVALID_LAYOUT ;
218+ }
203219 let layout = Layout :: from_size_align_unchecked ( size, 1 ) ;
204220 let new_mem = alloc:: alloc:: realloc ( ( * ptr) . cast ( ) , layout, new_size) ;
205221 if !new_mem. is_null ( ) {
@@ -229,6 +245,10 @@ pub unsafe extern "C" fn nstd_alloc_reallocate(
229245///
230246/// - `NSTDUInt size` - The number of bytes to free.
231247///
248+ /// # Returns
249+ ///
250+ /// `NSTDAllocError errc` - The allocation operation error code.
251+ ///
232252/// # Safety
233253///
234254/// - Behavior is undefined if `ptr` is not a value returned by `nstd_alloc_allocate[_zeroed]`.
@@ -246,27 +266,36 @@ pub unsafe extern "C" fn nstd_alloc_reallocate(
246266/// nstd_alloc_deallocate(&mut mem, 24);
247267/// }
248268/// ```
249- #[ inline]
269+ #[ cfg_attr ( any ( target_family = "unix" , target_os = "windows" ) , inline) ]
250270#[ cfg_attr( feature = "clib" , no_mangle) ]
251271#[ cfg_attr(
252272 any( target_family = "unix" , target_os = "windows" ) ,
253273 allow( unused_variables)
254274) ]
255- pub unsafe extern "C" fn nstd_alloc_deallocate ( ptr : & mut NSTDAnyMut , size : NSTDUInt ) {
275+ pub unsafe extern "C" fn nstd_alloc_deallocate (
276+ ptr : & mut NSTDAnyMut ,
277+ size : NSTDUInt ,
278+ ) -> NSTDAllocError {
256279 #[ cfg( not( any( target_family = "unix" , target_os = "windows" ) ) ) ]
257280 {
258281 use crate :: NSTD_NULL ;
259282 use alloc:: alloc:: Layout ;
283+ // Make sure `size` is valid for `layout`.
284+ if size > isize:: MAX as usize {
285+ return NSTDAllocError :: NSTD_ALLOC_ERROR_INVALID_LAYOUT ;
286+ }
260287 let layout = Layout :: from_size_align_unchecked ( size, 1 ) ;
261288 alloc:: alloc:: dealloc ( ( * ptr) . cast ( ) , layout) ;
262289 * ptr = NSTD_NULL ;
290+ NSTDAllocError :: NSTD_ALLOC_ERROR_NONE
263291 }
264292 #[ cfg( target_family = "unix" ) ]
265293 {
266294 nstd_os_unix_alloc_deallocate ( ptr) ;
295+ NSTDAllocError :: NSTD_ALLOC_ERROR_NONE
267296 }
268297 #[ cfg( target_os = "windows" ) ]
269298 {
270- nstd_os_windows_alloc_deallocate ( ptr) ;
299+ NSTDAllocError :: from_windows ( nstd_os_windows_alloc_deallocate ( ptr) )
271300 }
272301}
0 commit comments