diff --git a/uefi/CHANGELOG.md b/uefi/CHANGELOG.md index b609e59d8..99e032ff1 100644 --- a/uefi/CHANGELOG.md +++ b/uefi/CHANGELOG.md @@ -16,6 +16,7 @@ - `boot::allocate_pages` no longer panics if the allocation is at address zero. The allocation is retried instead, and in all failure cases an error is returned rather than panicking. +- The `Display` impl for `CStr8` now excludes the trailing null character. # uefi - 0.34.1 (2025-02-07) diff --git a/uefi/src/data_types/strs.rs b/uefi/src/data_types/strs.rs index 4fd86a2f1..5c06a2db4 100644 --- a/uefi/src/data_types/strs.rs +++ b/uefi/src/data_types/strs.rs @@ -196,7 +196,7 @@ impl fmt::Debug for CStr8 { impl fmt::Display for CStr8 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - for c in self.0.iter() { + for c in &self.0[..&self.0.len() - 1] { ::fmt(c, f)?; } Ok(()) @@ -763,6 +763,7 @@ where mod tests { use super::*; use crate::{cstr16, cstr8}; + use alloc::format; use alloc::string::String; // Tests if our CStr8 type can be constructed from a valid core::ffi::CStr @@ -783,6 +784,18 @@ mod tests { assert_eq!(>::borrow(string), &[b'a', 0]); } + #[test] + fn test_cstr8_display() { + let s = cstr8!("abc"); + assert_eq!(format!("{s}"), "abc"); + } + + #[test] + fn test_cstr16_display() { + let s = cstr16!("abc"); + assert_eq!(format!("{s}"), "abc"); + } + #[test] fn test_cstr16_num_bytes() { let s = CStr16::from_u16_with_nul(&[65, 66, 67, 0]).unwrap();