From 99b8be62807b497ed8c173a961f8cadbf8264d11 Mon Sep 17 00:00:00 2001 From: Seiya Nuta Date: Tue, 21 May 2024 02:36:57 +0000 Subject: [PATCH 1/2] 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 --- CHANGELOG.md | 9 +++++++++ src/writer.rs | 8 +++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9480412..07631fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +# Upcoming Release + +## Added + +- Added `FdtWriter::property_cstring` method to allow setting `&CStr` string + value directly. + +## Fixed + # v0.3.0 ## Added diff --git a/src/writer.rs b/src/writer.rs index f8ccbfc..dad7e12 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -10,6 +10,7 @@ use alloc::string::String; use alloc::vec::Vec; use core::cmp::{Ord, Ordering}; use core::convert::TryInto; +use core::ffi::CStr; use core::fmt; use core::mem::size_of_val; #[cfg(feature = "std")] @@ -448,7 +449,12 @@ impl FdtWriter { /// Write a string property. pub fn property_string(&mut self, name: &str, val: &str) -> Result<()> { let cstr_value = CString::new(val).map_err(|_| Error::InvalidString)?; - self.property(name, cstr_value.to_bytes_with_nul()) + self.property_cstring(name, &cstr_value) + } + + /// Write a C string property. + pub fn property_cstring(&mut self, name: &str, val: &CStr) -> Result<()> { + self.property(name, val.to_bytes_with_nul()) } /// Write a stringlist property. From fd645bcfe411b6e906b451247a5847cc8e25bb22 Mon Sep 17 00:00:00 2001 From: Seiya Nuta Date: Tue, 21 May 2024 15:51:03 +0900 Subject: [PATCH 2/2] Update CHANGELOG.md Signed-off-by: Seiya Nuta --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07631fa..f53511d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## Added -- Added `FdtWriter::property_cstring` method to allow setting `&CStr` string +- [[#72](https://github.com/rust-vmm/vm-fdt/pull/72)] Added + `FdtWriter::property_cstring` method to allow setting `&CStr` string value directly. ## Fixed