Skip to content

Commit 99b8be6

Browse files
committed
Add FdtWriter::property_cstring
Since FdtWriter only accept &str, it does a validation to make sure it's NULL-terminated and copies the whole string in Cstring::new(). This is unnecessary in some circumstances. For example, for "bootargs" property, linux_loader can create CString [1]. However, library users need to convert it into String [2]. This patches adds another property helper `property_cstring`, which accepts a &CStr value directly. [1] https://docs.rs/linux-loader/latest/linux_loader/cmdline/struct.Cmdline.html#method.as_cstring [2] https://github.com/firecracker-microvm/firecracker/blob/3853362520b81efc8ce6559148d023379a5a4da4/src/vmm/src/arch/aarch64/fdt.rs#L240-L246 Signed-off-by: Seiya Nuta <[email protected]>
1 parent ef5bd73 commit 99b8be6

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# Upcoming Release
2+
3+
## Added
4+
5+
- Added `FdtWriter::property_cstring` method to allow setting `&CStr` string
6+
value directly.
7+
8+
## Fixed
9+
110
# v0.3.0
211

312
## Added

src/writer.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use alloc::string::String;
1010
use alloc::vec::Vec;
1111
use core::cmp::{Ord, Ordering};
1212
use core::convert::TryInto;
13+
use core::ffi::CStr;
1314
use core::fmt;
1415
use core::mem::size_of_val;
1516
#[cfg(feature = "std")]
@@ -448,7 +449,12 @@ impl FdtWriter {
448449
/// Write a string property.
449450
pub fn property_string(&mut self, name: &str, val: &str) -> Result<()> {
450451
let cstr_value = CString::new(val).map_err(|_| Error::InvalidString)?;
451-
self.property(name, cstr_value.to_bytes_with_nul())
452+
self.property_cstring(name, &cstr_value)
453+
}
454+
455+
/// Write a C string property.
456+
pub fn property_cstring(&mut self, name: &str, val: &CStr) -> Result<()> {
457+
self.property(name, val.to_bytes_with_nul())
452458
}
453459

454460
/// Write a stringlist property.

0 commit comments

Comments
 (0)