reallocarray introduction available on glibc 2.26. allocates an array of m*n elements but checking for overflow.

This commit is contained in:
David Carlier
2023-03-26 08:33:30 +01:00
parent ae745427cd
commit 3fe37e36fa
3 changed files with 34 additions and 0 deletions
+16
View File
@@ -812,6 +812,22 @@ pub unsafe extern "C" fn realloc(ptr: *mut c_void, size: size_t) -> *mut c_void
new_ptr
}
#[no_mangle]
pub unsafe extern "C" fn reallocarray(ptr: *mut c_void, m: size_t, n: size_t) -> *mut c_void {
//Handle possible integer overflow in size calculation
match m.checked_mul(n) {
Some(size) => {
realloc(ptr, size)
}
None => {
// For overflowing multiplication, we have to set errno here
platform::errno = ENOMEM;
ptr::null_mut()
}
}
}
#[no_mangle]
pub unsafe extern "C" fn realpath(pathname: *const c_char, resolved: *mut c_char) -> *mut c_char {
let ptr = if resolved.is_null() {