Skip to content

uplink(fix): Own the FFI values through a guard - #105

Merged
egonelbre merged 2 commits into
storj-thirdparty:mainfrom
GodTamIt:ffi-guard
Nov 24, 2026
Merged

egonelbre merged 2 commits into
storj-thirdparty:mainfrom
GodTamIt:ffi-guard

Conversation

@GodTamIt

Copy link
Copy Markdown
Contributor

This adds a Guard type that owns whatever the FFI returns and frees it on drop, so we don't have to free anything by hand anymore. ifraixedes brought up the idea of wrapper types with Drop a long time ago in #80 (comment), and I finally got around to it.

This fixes:

  • Double free when creating a Grant fails, e.g. Grant::new("invalid")
  • Use-after-free of the custom metadata in upload::Info
  • Panic in create_bucket() when the bucket exists but its info can't be read
  • EncryptionKey and a bunch of FFI errors never being freed

It also turns off Copy for the bindgen types, so the first two don't compile anymore. That's a breaking change for uplink-sys, since its types lose Copy and Clone.

The integration tests haven't been run on this. They fail in CI (on main too) because img.dev.storj.io doesn't resolve anymore.

This replaces #101, #102, #103 and #104.

Fixes #99

cc @egonelbre - I think ifraixedes has left Storj, so tagging you for context. #80 was a similar memory bug, and that's where we talked about doing this.

@ifraixedes

Copy link
Copy Markdown
Member

@GodTamIt yes, you're right. I don't work in Storj anymore, and unfortunately I cannot continue as a maintainer of this repo because of other priorities.

Nonetheless, I appreciate that you have sent this PR. It's a good refactor for this library.

Thank you @GodTamIt

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`.
@sonarqubecloud

Copy link
Copy Markdown

@egonelbre
egonelbre merged commit eeafb2a into storj-thirdparty:main Nov 24, 2026
5 checks passed
@egonelbre

Copy link
Copy Markdown
Contributor

@GodTamIt thanks for the PR

@GodTamIt

Copy link
Copy Markdown
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 🙂

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.

Segfault with invalid Grant

3 participants