Repository navigation
uplink(fix): Own the FFI values through a guard - #105
Merged
Merged
Conversation
This was referenced Nov 24, 2026
Member
egonelbre
approved these changes
Nov 24, 2026
Every FFI value was freed by hand, and nothing tied its lifetime to the
code using it. Add `Guard`, which owns an FFI value and frees it on drop,
and use it for everything that the FFI returns. The constructors that take
ownership of FFI values now take a `Guard`, and the ones that don't take a
reference.
This fixes:
- The double free when an FFI call that returns a `Grant` fails, for
example, `Grant::new("invalid")`.
- The use-after-free of the custom metadata of `upload::Info`.
- The leak of `EncryptionKey`.
- The leaks of errors in `Download`, `Upload`, `PartUpload`, the
iterators, `Project::create_bucket`, and closing on drop.
- The panic in `Project::create_bucket` when the bucket exists but its
information cannot be retrieved.
The FFI types own memory that must be freed exactly once, but deriving `Copy` allowed to free a value more than once, or to use it after it was freed, without the compiler complaining. BREAKING CHANGE: the types exposed by uplink-sys no longer implement `Copy` and `Clone`.
|
Contributor
|
@GodTamIt thanks for the PR |
Contributor
Author
|
@ifraixedes @egonelbre If you're looking for new maintainers to the repo, I'm happy to take that on. I'll be actively contributing regardless 🙂 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



This adds a
Guardtype that owns whatever the FFI returns and frees it on drop, so we don't have to free anything by hand anymore.ifraixedesbrought up the idea of wrapper types withDropa long time ago in #80 (comment), and I finally got around to it.This fixes:
Grantfails, e.g.Grant::new("invalid")upload::Infocreate_bucket()when the bucket exists but its info can't be readEncryptionKeyand a bunch of FFI errors never being freedIt also turns off
Copyfor the bindgen types, so the first two don't compile anymore. That's a breaking change foruplink-sys, since its types loseCopyandClone.The integration tests haven't been run on this. They fail in CI (on
maintoo) becauseimg.dev.storj.iodoesn't resolve anymore.This replaces #101, #102, #103 and #104.
Fixes #99
cc @egonelbre - I think
ifraixedeshas left Storj, so tagging you for context. #80 was a similar memory bug, and that's where we talked about doing this.