@@ -29,6 +29,8 @@ pub enum NSTDAllocError {
29
29
NSTD_ALLOC_ERROR_HEAP_NOT_FOUND ,
30
30
/// A heap is invalid.
31
31
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 ,
32
34
}
33
35
impl NSTDAllocError {
34
36
/// Converts an [NSTDWindowsAllocError] into an [NSTDAllocError].
@@ -77,7 +79,12 @@ impl NSTDAllocError {
77
79
pub unsafe extern "C" fn nstd_alloc_allocate ( size : NSTDUInt ) -> NSTDAnyMut {
78
80
#[ cfg( not( any( target_family = "unix" , target_os = "windows" ) ) ) ]
79
81
{
82
+ use crate :: NSTD_NULL ;
80
83
use alloc:: alloc:: Layout ;
84
+ // Make sure `size` is valid for `layout`.
85
+ if size > isize:: MAX as usize {
86
+ return NSTD_NULL ;
87
+ }
81
88
let layout = Layout :: from_size_align_unchecked ( size, 1 ) ;
82
89
alloc:: alloc:: alloc ( layout) . cast ( )
83
90
}
@@ -125,7 +132,12 @@ pub unsafe extern "C" fn nstd_alloc_allocate(size: NSTDUInt) -> NSTDAnyMut {
125
132
pub unsafe extern "C" fn nstd_alloc_allocate_zeroed ( size : NSTDUInt ) -> NSTDAnyMut {
126
133
#[ cfg( not( any( target_family = "unix" , target_os = "windows" ) ) ) ]
127
134
{
135
+ use crate :: NSTD_NULL ;
128
136
use alloc:: alloc:: Layout ;
137
+ // Make sure `size` is valid for `layout`.
138
+ if size > isize:: MAX as usize {
139
+ return NSTD_NULL ;
140
+ }
129
141
let layout = Layout :: from_size_align_unchecked ( size, 1 ) ;
130
142
alloc:: alloc:: alloc_zeroed ( layout) . cast ( )
131
143
}
@@ -186,7 +198,7 @@ pub unsafe extern "C" fn nstd_alloc_allocate_zeroed(size: NSTDUInt) -> NSTDAnyMu
186
198
/// nstd_alloc_deallocate(&mut mem, SIZE);
187
199
/// }
188
200
/// ```
189
- #[ inline]
201
+ #[ cfg_attr ( any ( target_family = "unix" , target_os = "windows" ) , inline) ]
190
202
#[ cfg_attr( feature = "clib" , no_mangle) ]
191
203
#[ cfg_attr(
192
204
any( target_family = "unix" , target_os = "windows" ) ,
@@ -200,6 +212,10 @@ pub unsafe extern "C" fn nstd_alloc_reallocate(
200
212
#[ cfg( not( any( target_family = "unix" , target_os = "windows" ) ) ) ]
201
213
{
202
214
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
+ }
203
219
let layout = Layout :: from_size_align_unchecked ( size, 1 ) ;
204
220
let new_mem = alloc:: alloc:: realloc ( ( * ptr) . cast ( ) , layout, new_size) ;
205
221
if !new_mem. is_null ( ) {
@@ -229,6 +245,10 @@ pub unsafe extern "C" fn nstd_alloc_reallocate(
229
245
///
230
246
/// - `NSTDUInt size` - The number of bytes to free.
231
247
///
248
+ /// # Returns
249
+ ///
250
+ /// `NSTDAllocError errc` - The allocation operation error code.
251
+ ///
232
252
/// # Safety
233
253
///
234
254
/// - 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(
246
266
/// nstd_alloc_deallocate(&mut mem, 24);
247
267
/// }
248
268
/// ```
249
- #[ inline]
269
+ #[ cfg_attr ( any ( target_family = "unix" , target_os = "windows" ) , inline) ]
250
270
#[ cfg_attr( feature = "clib" , no_mangle) ]
251
271
#[ cfg_attr(
252
272
any( target_family = "unix" , target_os = "windows" ) ,
253
273
allow( unused_variables)
254
274
) ]
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 {
256
279
#[ cfg( not( any( target_family = "unix" , target_os = "windows" ) ) ) ]
257
280
{
258
281
use crate :: NSTD_NULL ;
259
282
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
+ }
260
287
let layout = Layout :: from_size_align_unchecked ( size, 1 ) ;
261
288
alloc:: alloc:: dealloc ( ( * ptr) . cast ( ) , layout) ;
262
289
* ptr = NSTD_NULL ;
290
+ NSTDAllocError :: NSTD_ALLOC_ERROR_NONE
263
291
}
264
292
#[ cfg( target_family = "unix" ) ]
265
293
{
266
294
nstd_os_unix_alloc_deallocate ( ptr) ;
295
+ NSTDAllocError :: NSTD_ALLOC_ERROR_NONE
267
296
}
268
297
#[ cfg( target_os = "windows" ) ]
269
298
{
270
- nstd_os_windows_alloc_deallocate ( ptr) ;
299
+ NSTDAllocError :: from_windows ( nstd_os_windows_alloc_deallocate ( ptr) )
271
300
}
272
301
}
0 commit comments