Skip to content

Commit 70b4a92

Browse files
Added changes for rust-lang/rust#95295.
1 parent b764061 commit 70b4a92

File tree

12 files changed

+111
-25
lines changed

12 files changed

+111
-25
lines changed

include/nstd/alloc.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ typedef enum {
1313
/// Getting a handle to a heap failed.
1414
NSTD_ALLOC_ERROR_HEAP_NOT_FOUND,
1515
/// A heap is invalid.
16-
NSTD_ALLOC_ERROR_INVALID_HEAP
16+
NSTD_ALLOC_ERROR_INVALID_HEAP,
17+
/// An allocation function received input parameters that resulted in an invalid memory layout.
18+
NSTD_ALLOC_ERROR_INVALID_LAYOUT
1719
} NSTDAllocError;
1820

1921
/// Allocates a block of memory on the heap.
@@ -84,11 +86,15 @@ NSTDAPI NSTDAllocError nstd_alloc_reallocate(NSTDAnyMut *ptr, NSTDUInt size, NST
8486
///
8587
/// - `NSTDUInt size` - The number of bytes to free.
8688
///
89+
/// # Returns
90+
///
91+
/// `NSTDAllocError errc` - The allocation operation error code.
92+
///
8793
/// # Safety
8894
///
8995
/// - Behavior is undefined if `ptr` is not a value returned by `nstd_alloc_allocate[_zeroed]`.
9096
///
9197
/// - `size` must be the same value that was used to allocate the memory buffer.
92-
NSTDAPI void nstd_alloc_deallocate(NSTDAnyMut *ptr, NSTDUInt size);
98+
NSTDAPI NSTDAllocError nstd_alloc_deallocate(NSTDAnyMut *ptr, NSTDUInt size);
9399

94100
#endif

include/nstd/cstring.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ NSTDAPI NSTDChar nstd_cstring_pop(NSTDCString *cstring);
237237
///
238238
/// # Panics
239239
///
240-
/// This operation may panic if getting a handle to the heap fails.
240+
/// Panics if deallocating fails.
241241
NSTDAPI void nstd_cstring_free(NSTDCString cstring);
242242

243243
#endif

include/nstd/heap_ptr.h

+4
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ NSTDAPI NSTDAnyMut nstd_heap_ptr_get_mut(NSTDHeapPtr *hptr);
9999
/// # Parameters:
100100
///
101101
/// - `NSTDHeapPtr hptr` - A pointer to the heap object.
102+
///
103+
/// # Panics
104+
///
105+
/// Panics if freeing the heap memory fails.
102106
NSTDAPI void nstd_heap_ptr_free(NSTDHeapPtr hptr);
103107

104108
#endif

include/nstd/shared_ptr.h

+5
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ NSTDAPI NSTDAny nstd_shared_ptr_get(const NSTDSharedPtr *shared_ptr);
9797
/// # Parameters:
9898
///
9999
/// - `NSTDSharedPtr shared_ptr` - The shared object to free.
100+
///
101+
/// # Panics
102+
///
103+
/// Panics if there are no more shared pointers referencing the shared data and freeing the heap
104+
/// memory fails.
100105
NSTDAPI void nstd_shared_ptr_free(NSTDSharedPtr shared_ptr);
101106

102107
#endif

include/nstd/string.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ NSTDAPI NSTDString nstd_string_from_u64(NSTDUInt64 v);
404404
///
405405
/// # Panics
406406
///
407-
/// This operation may panic if getting a handle to the heap fails.
407+
/// Panics if deallocating fails.
408408
NSTDAPI void nstd_string_free(NSTDString string);
409409

410410
#endif

include/nstd/vec.h

+4
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,10 @@ NSTDAPI NSTDAllocError nstd_vec_shrink(NSTDVec *vec);
375375
/// # Parameters:
376376
///
377377
/// - `NSTDVec vec` - The vector to free.
378+
///
379+
/// # Panics
380+
///
381+
/// Panics if deallocating fails.
378382
NSTDAPI void nstd_vec_free(NSTDVec vec);
379383

380384
#endif

src/alloc.rs

+33-4
Original file line numberDiff line numberDiff line change
@@ -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
}
3335
impl NSTDAllocError {
3436
/// Converts an [NSTDWindowsAllocError] into an [NSTDAllocError].
@@ -77,7 +79,12 @@ impl NSTDAllocError {
7779
pub 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 {
125132
pub 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
}

src/cstring.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ pub extern "C" fn nstd_cstring_pop(cstring: &mut NSTDCString) -> NSTDChar {
436436
///
437437
/// # Panics
438438
///
439-
/// This operation may panic if getting a handle to the heap fails.
439+
/// Panics if deallocating fails.
440440
#[inline]
441441
#[cfg_attr(feature = "clib", no_mangle)]
442442
#[allow(unused_variables)]

src/heap_ptr.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! A pointer type for single value heap allocation.
22
use crate::{
3-
alloc::{nstd_alloc_allocate, nstd_alloc_allocate_zeroed, nstd_alloc_deallocate},
3+
alloc::{
4+
nstd_alloc_allocate, nstd_alloc_allocate_zeroed, nstd_alloc_deallocate,
5+
NSTDAllocError::NSTD_ALLOC_ERROR_NONE,
6+
},
47
core::mem::nstd_core_mem_copy,
58
NSTDAny, NSTDAnyMut, NSTDUInt,
69
};
@@ -16,10 +19,16 @@ pub struct NSTDHeapPtr {
1619
}
1720
impl Drop for NSTDHeapPtr {
1821
/// [NSTDHeapPtr]'s destructor.
22+
///
23+
/// # Panics
24+
///
25+
/// Panics if deallocating fails.
1926
#[inline]
2027
fn drop(&mut self) {
2128
// SAFETY: Heap pointers are always non-null.
22-
unsafe { nstd_alloc_deallocate(&mut self.ptr, self.size) };
29+
unsafe {
30+
assert!(nstd_alloc_deallocate(&mut self.ptr, self.size) == NSTD_ALLOC_ERROR_NONE);
31+
}
2332
}
2433
}
2534

@@ -223,6 +232,10 @@ pub extern "C" fn nstd_heap_ptr_get_mut(hptr: &mut NSTDHeapPtr) -> NSTDAnyMut {
223232
/// # Parameters:
224233
///
225234
/// - `NSTDHeapPtr hptr` - A pointer to the heap object.
235+
///
236+
/// # Panics
237+
///
238+
/// Panics if freeing the heap memory fails.
226239
#[inline]
227240
#[cfg_attr(feature = "clib", no_mangle)]
228241
#[allow(unused_variables)]

src/shared_ptr.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! A reference counting smart pointer.
22
use crate::{
3-
alloc::{nstd_alloc_allocate, nstd_alloc_allocate_zeroed, nstd_alloc_deallocate},
3+
alloc::{
4+
nstd_alloc_allocate, nstd_alloc_allocate_zeroed, nstd_alloc_deallocate,
5+
NSTDAllocError::NSTD_ALLOC_ERROR_NONE,
6+
},
47
core::mem::nstd_core_mem_copy,
58
NSTDAny, NSTDAnyMut, NSTDUInt,
69
};
@@ -38,6 +41,10 @@ impl NSTDSharedPtr {
3841
}
3942
impl Drop for NSTDSharedPtr {
4043
/// [NSTDSharedPtr]'s destructor.
44+
///
45+
/// # Panics
46+
///
47+
/// Panics if deallocating fails.
4148
fn drop(&mut self) {
4249
// SAFETY: Shared pointers are always non-null.
4350
unsafe {
@@ -46,7 +53,7 @@ impl Drop for NSTDSharedPtr {
4653
*ptrs -= 1;
4754
// If the pointer count is zero, free the data.
4855
if *ptrs == 0 {
49-
nstd_alloc_deallocate(&mut self.ptr, self.size);
56+
assert!(nstd_alloc_deallocate(&mut self.ptr, self.size) == NSTD_ALLOC_ERROR_NONE);
5057
}
5158
}
5259
}
@@ -304,6 +311,11 @@ pub extern "C" fn nstd_shared_ptr_get(shared_ptr: &NSTDSharedPtr) -> NSTDAny {
304311
/// # Parameters:
305312
///
306313
/// - `NSTDSharedPtr shared_ptr` - The shared object to free.
314+
///
315+
/// # Panics
316+
///
317+
/// Panics if there are no more shared pointers referencing the shared data and freeing the heap
318+
/// memory fails.
307319
#[cfg_attr(feature = "clib", no_mangle)]
308320
#[allow(unused_variables)]
309321
pub extern "C" fn nstd_shared_ptr_free(shared_ptr: NSTDSharedPtr) {}

src/string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ gen_from_primitive!(
591591
///
592592
/// # Panics
593593
///
594-
/// This operation may panic if getting a handle to the heap fails.
594+
/// Panics if deallocating fails.
595595
#[inline]
596596
#[cfg_attr(feature = "clib", no_mangle)]
597597
#[allow(unused_variables)]

0 commit comments

Comments
 (0)