Skip to content

uefi: Exclude null byte from CStr8 Display impl #1553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions uefi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 14 additions & 1 deletion uefi/src/data_types/strs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
<Char8 as fmt::Display>::fmt(c, f)?;
}
Ok(())
Expand Down Expand Up @@ -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
Expand All @@ -783,6 +784,18 @@ mod tests {
assert_eq!(<CStr8 as Borrow<[u8]>>::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();
Expand Down
Loading