From d1f567f2c484205c8f19292fac8de9f33c2461a5 Mon Sep 17 00:00:00 2001 From: Slava Koyfman Date: Thu, 6 Jul 2023 13:19:01 +0300 Subject: [PATCH] Fix lifetime warning in Rust 1.70 (#352) * Fix lifetime warning in Rust 1.70 Starting with rustc 1.70.0, the code in raw.rs:hash_get_multi produced the following warnings: this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime While it would work in practice due to the underlying implementation just doing pointer juggling, this warning was correct: The CString created within the macro `f` would be destroyed before the reference to the pointer taken from it with `as_ptr` was used. Resolved by binding the lifetime of the CStrings to that of the iterator variable, which is alive until the function returns. * Proper fix this time (hopefully) --- src/raw.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/raw.rs b/src/raw.rs index 7294285b..1648a233 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -473,6 +473,11 @@ where { assert_eq!(fields.len(), values.len()); + let fields = fields + .iter() + .map(|e| CString::new(e.clone())) + .collect::, _>>()?; + let mut fi = fields.iter(); let mut vi = values.iter_mut(); @@ -491,9 +496,7 @@ where } macro_rules! f { () => { - CString::new((*fi.next().unwrap()).clone()) - .unwrap() - .as_ptr() + fi.next().unwrap().as_ptr() }; } macro_rules! v {