From b75c8a6754efdeb7b9713f19938c03b8f3e652a9 Mon Sep 17 00:00:00 2001 From: Tim Boudreau Date: Mon, 26 Aug 2024 19:02:07 -0400 Subject: [PATCH] Fix #6 - CStrings used in constructor are passed as pointers to the OS's logging subsystem, which may attempt to use them after they have been dropped. The fix simply adds the CStrings as fields to the logger instance, so they are guaraneteed to be retained for the lifetime of the logger. --- Cargo.toml | 2 +- src/lib.rs | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 56742b6..fbec72b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "oslog" description = "A minimal safe wrapper around Apple's Logging system" repository = "https://github.com/steven-joruk/oslog" -version = "0.2.0" +version = "0.2.1" authors = ["Steven Joruk "] edition = "2021" license = "MIT" diff --git a/src/lib.rs b/src/lib.rs index 6a4ef4d..5014c99 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,6 +39,12 @@ impl From for Level { pub struct OsLog { inner: os_log_t, + /// These need to remain allocated or system logging code can use + /// them after they are freed. + #[allow(dead_code)] + subsystem: Option, + #[allow(dead_code)] + category: Option, } unsafe impl Send for OsLog {} @@ -64,7 +70,11 @@ impl OsLog { assert!(!inner.is_null(), "Unexpected null value from os_log_create"); - Self { inner } + Self { + inner, + subsystem: Some(subsystem), + category: Some(category), + } } #[inline] @@ -73,7 +83,11 @@ impl OsLog { assert!(!inner.is_null(), "Unexpected null value for OS_DEFAULT_LOG"); - Self { inner } + Self { + inner, + subsystem: None, + category: None, + } } #[inline]