FillZpp / sys-info-rs

Get system information in Rust.
MIT License
168 stars 56 forks source link

macos' get_os_release leaks and misbehaves #97

Open steven-joruk opened 3 years ago

steven-joruk commented 3 years ago

The get_os_release C function allocates the buffer on the heap and returns it, but on the rust side it doesn't take ownership of the memory:

buf = malloc(LEN);
let typ = ffi::CStr::from_ptr(rp).to_bytes();

Also, if an error occurs on the C side it uses strncpy to set it to "Unknown", but on rust's side it expects a NULL pointer to be returned in case of error:

// At this point `len` is _most likely_ set to `0` (e.g. in case of ENOMEM).
// We copy our string and reset the length.
strncpy(buf, "Unknown", LEN);
if rp == std::ptr::null() {
    Err(Error::Unknown)
}

There are other heap allocations on the C side I haven't verified are handled correctly on rust's side.