Skip to content

Commit

Permalink
Merge pull request #1553 from nicholasbishop/bishop-cstr8-display
Browse files Browse the repository at this point in the history
uefi: Exclude null byte from CStr8 Display impl
  • Loading branch information
phip1611 authored Mar 2, 2025
2 parents a1d02e6 + 52c2001 commit 709efb8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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

0 comments on commit 709efb8

Please sign in to comment.