-
Notifications
You must be signed in to change notification settings - Fork 488
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(bindings/c): use ManuallyDrop
instead of forget
to make sure pointer is valid
#5166
Conversation
2bb9fc9
to
86b9a0b
Compare
10884ab
to
77193ff
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this is the real reason my previous patch failed, lol.
This can be a hidden knowledge that Go binding's struct should be synced with C binding's ..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution! I believe we can have a better PR title ..
drop(Vec::from_raw_parts(data_mut, (*ptr).len, (*ptr).len)); | ||
drop(Box::from_raw(ptr)); | ||
// transmuting `*const u8` to `*mut u8` is undefined behavior in any cases | ||
// however, fields type of `opendal_bytes` is already related to the zig binding |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
already related to the zig binding
Do you mean that the Zig binding should use non-const pointer also?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think so
// it is too weird that call `Box::new` outside `opendal_bytes::new` but dealloc it here | ||
// also, boxing `opendal_bytes` might not be necessary | ||
// `data` points to heap, so `opendal_bytes` could be passed as a stack value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reasonable. But IIRC other bindings rely on this interface also. We can update them all in a follow-up.
Then the free method is guarded by !self.data.is_null()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try in #5171
ManuallyDrop
instead of forget
to make sure pointer is valid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for helping fix this! Also thanks for @tisonkun's review.
ManuallyDrop
instead of forget
to make sure pointer is validManuallyDrop
instead of forget
to make sure pointer is valid
Which issue does this PR close?
Closes #5165.
Rationale for this change
refer to https://doc.rust-lang.org/std/mem/fn.forget.html
the implementation of
Vec::into_raw_parts
https://doc.rust-lang.org/1.81.0/src/alloc/vec/mod.rs.html#883
Also, transmute
*const _
to* mut _
is an undefined behavior in any cases, it is fixed in this commit.