Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lifetime warning in Rust 1.70 #352

Merged
merged 2 commits into from
Jul 6, 2023

Conversation

slavak
Copy link
Collaborator

@slavak slavak commented Jun 21, 2023

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 allocating a new Vec that contains the CStrings. This ensures they remain alive until the function returns.

@slavak slavak requested a review from gkorland June 21, 2023 13:22
@gkorland gkorland requested review from iddm and MeirShpilraien June 21, 2023 13:32
src/raw.rs Outdated Show resolved Hide resolved
CString::new((*fi.next().unwrap()).clone())
.unwrap()
.as_ptr()
fi.next().unwrap().as_ptr()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't we have the same problem as before? the next will return the CString which will be dropped right away and you will return its pointer. What am I missing?

Copy link
Collaborator

@iddm iddm Jun 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should just get rid of that macro for good if we can, and replace it with a function. I already fixed it in the past but then reverted for some reason (I can't remember), or we probably had another place with a similar issue.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think that the macro is the problem, working with pointer is unsafe and should be done carefully. Regardless, I do not like this macro as well ...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the macro itself is a problem as, well. I think the way it is written (and used) is.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably add a proc macro on redismodule-rs-macros that will generated this call and this way we will also not be limited to the number of fields...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a good idea to try.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some testing to check that the destructor is called at the correct time, but on second (third? fourth?) thought that testing might have been flawed and you might be right about the lifetime still being wrong.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand how a proc macro would work, but it's been a while since I've dug into the details of Rust macros. I'm all in favor of it if it's possible.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest we will fix the current issue first, and open another issue to define this proc macro.

Copy link
Collaborator

@MeirShpilraien MeirShpilraien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small question

slavak added 2 commits July 2, 2023 12:58
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.
@slavak
Copy link
Collaborator Author

slavak commented Jul 2, 2023

Pushed a new commit that should properly resolve this by (regrettably) allocating a new Vec.

Unfortunately I don't have any easy way of testing this actually works. Would appreciate some help on that front.

Copy link
Collaborator

@MeirShpilraien MeirShpilraien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
I was wondering how come our valgrind run did not catch it, but then I notice we do not have such run :) (seems like I already open an issue to add it), so till then I believe we can simply make sure that the warning is gone.
I believe we should also open a followup issue to design this API with proc macros (as discussed here).

@MeirShpilraien MeirShpilraien merged commit d1f567f into RedisLabsModules:master Jul 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants