Add cstr! macro
cstr!("X") is a shorthand for CStr::from_bytes_with_nul(b"X\0").unwrap()
Bug: 249054080
Test: TH
Change-Id: Iefdaa07179fe3b556fc9bc17595919ac93b7e569
diff --git a/pvmfw/src/crypto.rs b/pvmfw/src/crypto.rs
index 85dc6c9..275de7a 100644
--- a/pvmfw/src/crypto.rs
+++ b/pvmfw/src/crypto.rs
@@ -14,6 +14,8 @@
//! Wrapper around BoringSSL/OpenSSL symbols.
+use crate::cstr;
+
use core::convert::AsRef;
use core::ffi::{c_char, c_int, CStr};
use core::fmt;
@@ -81,14 +83,10 @@
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- let unknown_library = CStr::from_bytes_with_nul(b"{unknown library}\0").unwrap();
- let unknown_reason = CStr::from_bytes_with_nul(b"{unknown reason}\0").unwrap();
- let unknown_file = CStr::from_bytes_with_nul(b"??\0").unwrap();
-
let packed = self.packed_value();
- let library = self.library_name().unwrap_or(unknown_library).to_str().unwrap();
- let reason = self.reason().unwrap_or(unknown_reason).to_str().unwrap();
- let file = self.file.unwrap_or(unknown_file).to_str().unwrap();
+ let library = self.library_name().unwrap_or(cstr!("{unknown library}")).to_str().unwrap();
+ let reason = self.reason().unwrap_or(cstr!("{unknown reason}")).to_str().unwrap();
+ let file = self.file.unwrap_or(cstr!("??")).to_str().unwrap();
let line = self.line;
write!(f, "{file}:{line}: {library}: {reason} ({packed:#x})")