Skip to content

Commit

Permalink
fix(util): fixing static memleak in version-info macro
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Savitskiy <[email protected]>
  • Loading branch information
dsavitskiy committed Jun 22, 2023
1 parent 8925974 commit 98a7f08
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions version-info/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,17 @@ macro_rules! version_info_string {
}

/// Gets package's version info as a static str.
/// Each call to this macro leaks a string.
#[macro_export]
macro_rules! version_info_str {
() => {
Box::leak(Box::new(String::from($crate::version_info!()))) as &'static str
};
() => {{
static mut BUF: Vec<u8> = Vec::new();
let s = String::from($crate::version_info!());
unsafe {
BUF.resize(s.len(), 0);
BUF.clone_from_slice(s.as_bytes());
}
unsafe { std::str::from_utf8_unchecked(BUF.as_slice()) }
}};
}

/// Formats package related information.
Expand Down

0 comments on commit 98a7f08

Please sign in to comment.