Skip to content

Commit

Permalink
Fix lifetime warning in Rust 1.70 (#352)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
slavak authored Jul 6, 2023
1 parent eefacd3 commit d1f567f
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,11 @@ where
{
assert_eq!(fields.len(), values.len());

let fields = fields
.iter()
.map(|e| CString::new(e.clone()))
.collect::<Result<Vec<CString>, _>>()?;

let mut fi = fields.iter();
let mut vi = values.iter_mut();

Expand All @@ -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 {
Expand Down

0 comments on commit d1f567f

Please sign in to comment.