Skip to content

Commit 709efb8

Browse files
authored
Merge pull request #1553 from nicholasbishop/bishop-cstr8-display
uefi: Exclude null byte from CStr8 Display impl
2 parents a1d02e6 + 52c2001 commit 709efb8

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

uefi/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- `boot::allocate_pages` no longer panics if the allocation is at address
1717
zero. The allocation is retried instead, and in all failure cases an error is
1818
returned rather than panicking.
19+
- The `Display` impl for `CStr8` now excludes the trailing null character.
1920

2021

2122
# uefi - 0.34.1 (2025-02-07)

uefi/src/data_types/strs.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ impl fmt::Debug for CStr8 {
196196

197197
impl fmt::Display for CStr8 {
198198
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
199-
for c in self.0.iter() {
199+
for c in &self.0[..&self.0.len() - 1] {
200200
<Char8 as fmt::Display>::fmt(c, f)?;
201201
}
202202
Ok(())
@@ -763,6 +763,7 @@ where
763763
mod tests {
764764
use super::*;
765765
use crate::{cstr16, cstr8};
766+
use alloc::format;
766767
use alloc::string::String;
767768

768769
// Tests if our CStr8 type can be constructed from a valid core::ffi::CStr
@@ -783,6 +784,18 @@ mod tests {
783784
assert_eq!(<CStr8 as Borrow<[u8]>>::borrow(string), &[b'a', 0]);
784785
}
785786

787+
#[test]
788+
fn test_cstr8_display() {
789+
let s = cstr8!("abc");
790+
assert_eq!(format!("{s}"), "abc");
791+
}
792+
793+
#[test]
794+
fn test_cstr16_display() {
795+
let s = cstr16!("abc");
796+
assert_eq!(format!("{s}"), "abc");
797+
}
798+
786799
#[test]
787800
fn test_cstr16_num_bytes() {
788801
let s = CStr16::from_u16_with_nul(&[65, 66, 67, 0]).unwrap();

0 commit comments

Comments
 (0)