std: Don't panic when removing a nonexistent UEFI var

`std::env::remove_var` does not say that deleting a nonexistent variable
is an error (and at least on Linux, it indeed does not cause an
error).

The UEFI Shell Protocol spec also doesn't say it's an error, but the
edk2 implementation delegates to the UEFI runtime `SetVariable`
function, which returns `EFI_NOT_FOUND` when trying to delete a
nonexistent variable.

Change the UEFI implementation to check for a `NotFound` error and treat
it as success.
This commit is contained in:
Nicholas Bishop 2026-02-08 12:29:59 -05:00
parent 08a4ce529f
commit 4d07a8f24c

View file

@ -43,7 +43,20 @@ mod uefi_env {
pub(crate) fn unset(key: &OsStr) -> io::Result<()> {
let mut key_ptr = helpers::os_string_to_raw(key)
.ok_or(io::const_error!(io::ErrorKind::InvalidInput, "invalid key"))?;
unsafe { set_raw(key_ptr.as_mut_ptr(), crate::ptr::null_mut()) }
let r = unsafe { set_raw(key_ptr.as_mut_ptr(), crate::ptr::null_mut()) };
// The UEFI Shell spec only lists `EFI_SUCCESS` as a possible return value for
// `SetEnv`, but the edk2 implementation can return errors. Allow most of these
// errors to bubble up to the caller, but ignore `NotFound` errors; deleting a
// nonexistent variable is not listed as an error condition of
// `std::env::remove_var`.
if let Err(err) = &r
&& err.kind() == io::ErrorKind::NotFound
{
Ok(())
} else {
r
}
}
pub(crate) fn get_all() -> io::Result<Vec<(OsString, OsString)>> {