wireguard-control: check if FFI gave us a null pointer with a 0 ret code

ffi-segfault-fix
Jake McGinty 2021-11-26 00:16:56 +09:00
parent 2c31a4b6ec
commit cf04cc0c23
1 changed files with 2 additions and 2 deletions

View File

@ -386,14 +386,14 @@ pub fn apply(builder: &DeviceUpdate, iface: &InterfaceName) -> io::Result<()> {
pub fn get_by_name(name: &InterfaceName) -> Result<Device, io::Error> { pub fn get_by_name(name: &InterfaceName) -> Result<Device, io::Error> {
let mut device: *mut wireguard_control_sys::wg_device = ptr::null_mut(); let mut device: *mut wireguard_control_sys::wg_device = ptr::null_mut();
let result = unsafe { let ret = unsafe {
wireguard_control_sys::wg_get_device( wireguard_control_sys::wg_get_device(
(&mut device) as *mut _ as *mut *mut wireguard_control_sys::wg_device, (&mut device) as *mut _ as *mut *mut wireguard_control_sys::wg_device,
name.as_ptr(), name.as_ptr(),
) )
}; };
let result = if result == 0 { let result = if ret == 0 && !device.is_null() {
Ok(Device::from(unsafe { &*device })) Ok(Device::from(unsafe { &*device }))
} else { } else {
Err(io::Error::last_os_error()) Err(io::Error::last_os_error())