Skip to content

Commit d8a4723

Browse files
uefi: Exclude null byte from CStr8 Display impl
This makes it match the CStr16 Display impl. Added tests for both.
1 parent 53b822e commit d8a4723

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
@@ -10,6 +10,7 @@
1010
- `SimpleNetwork::transmit` now passes the correct buffer size argument.
1111
Previously it incorrectly added the header size to the buffer length, which
1212
could cause the firmware to read past the end of the buffer.
13+
- The `Display` impl for `CStr8` now excludes the trailing null character.
1314

1415

1516
# 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)