Skip to content

Commit 19da496

Browse files
authored
Merge pull request #1424 from nicholasbishop/bishop-better-varkey
uefi: Improve the VariableKey type and iterator
2 parents cbdd58c + 81af1a3 commit 19da496

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

uefi-test-runner/src/runtime/vars.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn test_variables() {
4848
let find_by_key = || {
4949
runtime::variable_keys().any(|k| {
5050
let k = k.as_ref().unwrap();
51-
k.name().unwrap() == NAME && &k.vendor == VENDOR
51+
k.name == NAME && &k.vendor == VENDOR
5252
})
5353
};
5454
assert!(find_by_key());

uefi/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ details of the deprecated items that were removed in this release.
1717
deprecated conversion from `uefi::table::boot::ScopedProtocol` has been
1818
removed.
1919
- Fixed `boot::open_protocol` to properly handle a null interface pointer.
20+
- `VariableKey` now has a public `name` field. This `name` field always contains
21+
a valid string, so the `VariableKey::name()` method has been deprecated. Since
22+
all fields of `VariableKey` are now public, the type can be constructed by
23+
users.
24+
- The `VariableKeys` iterator will now yield an error item if a variable name is
25+
not UCS-2.
2026

2127

2228
# uefi - 0.32.0 (2024-09-09)

uefi/src/runtime.rs

+13-18
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use uefi_raw::table::boot::MemoryDescriptor;
1515

1616
#[cfg(feature = "alloc")]
1717
use {
18-
crate::mem::make_boxed, crate::Guid, alloc::borrow::ToOwned, alloc::boxed::Box, alloc::vec::Vec,
18+
crate::mem::make_boxed, crate::CString16, crate::Guid, alloc::borrow::ToOwned,
19+
alloc::boxed::Box, alloc::vec::Vec,
1920
};
2021

2122
#[cfg(all(feature = "unstable", feature = "alloc"))]
@@ -303,15 +304,13 @@ impl Iterator for VariableKeys {
303304

304305
match result {
305306
Ok(()) => {
306-
// Copy the name buffer, truncated after the first null
307-
// character (if one is present).
308-
let name = if let Some(nul_pos) = self.name.iter().position(|c| *c == 0) {
309-
self.name[..=nul_pos].to_owned()
310-
} else {
311-
self.name.clone()
307+
// Convert the name to a `CStr16`, yielding an error if invalid.
308+
let Ok(name) = CStr16::from_u16_until_nul(&self.name) else {
309+
return Some(Err(Status::UNSUPPORTED.into()));
312310
};
311+
313312
Some(Ok(VariableKey {
314-
name,
313+
name: name.to_owned(),
315314
vendor: self.vendor,
316315
}))
317316
}
@@ -868,30 +867,26 @@ impl TryFrom<&[u8]> for Time {
868867
#[cfg(feature = "alloc")]
869868
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
870869
pub struct VariableKey {
871-
pub(crate) name: Vec<u16>,
872870
/// Unique identifier for the vendor.
873871
pub vendor: VariableVendor,
872+
873+
/// Name of the variable, unique with the vendor namespace.
874+
pub name: CString16,
874875
}
875876

876877
#[cfg(feature = "alloc")]
877878
impl VariableKey {
878879
/// Name of the variable.
880+
#[deprecated = "Use the VariableKey.name field instead"]
879881
pub fn name(&self) -> core::result::Result<&CStr16, crate::data_types::FromSliceWithNulError> {
880-
CStr16::from_u16_with_nul(&self.name)
882+
Ok(&self.name)
881883
}
882884
}
883885

884886
#[cfg(feature = "alloc")]
885887
impl Display for VariableKey {
886888
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
887-
write!(f, "VariableKey {{ name: ")?;
888-
889-
match self.name() {
890-
Ok(name) => write!(f, "\"{name}\"")?,
891-
Err(err) => write!(f, "Err({err:?})")?,
892-
}
893-
894-
write!(f, ", vendor: ")?;
889+
write!(f, "VariableKey {{ name: \"{}\", vendor: ", self.name)?;
895890

896891
if self.vendor == VariableVendor::GLOBAL_VARIABLE {
897892
write!(f, "GLOBAL_VARIABLE")?;

0 commit comments

Comments
 (0)