Skip to content
Snippets Groups Projects
Commit c54db6f0 authored by Peter Limkilde Svendsen's avatar Peter Limkilde Svendsen
Browse files

Add integer overflow check to calloc

parent d2502056
No related branches found
No related tags found
1 merge request!188add calloc integer overflow check
......@@ -186,12 +186,18 @@ pub unsafe extern "C" fn bsearch(
#[no_mangle]
pub unsafe extern "C" fn calloc(nelem: size_t, elsize: size_t) -> *mut c_void {
let size = nelem * elsize;
let ptr = malloc(size);
if !ptr.is_null() {
intrinsics::write_bytes(ptr as *mut u8, 0, size);
//Handle possible integer overflow in size calculation
let size_result = nelem.checked_mul(elsize);
match size_result {
Some(size) => {
let ptr = malloc(size);
if !ptr.is_null() {
intrinsics::write_bytes(ptr as *mut u8, 0, size);
}
ptr
},
None => core::ptr::null_mut()
}
ptr
}
#[repr(C)]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment