Skip to content

Commit

Permalink
uplink(fix): Add missing doc unsafe blocks & lint
Browse files Browse the repository at this point in the history
Document unsafe blocks that didn't have a `SAFETY` documentation and add
the Clippy linter rules to detect unsafe undocumented blocks.
  • Loading branch information
ifraixedes committed Jul 12, 2024
1 parent 1c6edbc commit 497c270
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions uplink/src/edge/linksharing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ pub fn share_url(
let key = helpers::cstring_from_str_fn_arg("key", key)?;
let res: ulksys::UplinkStringResult;

// SAFETY: We are sure the CString(s) are valid because we created them from valid Rust strings.
// We get raw pointers to them and to pass them to the FFI function.
// We are sure the FFI function will not free them.
// We take the ownership back for the ones that we transferred the ownership to the FFI
// function to drop them and not leak memory.
unsafe {
let c_base_url = base_url.into_raw();
let c_access_key = access_key.into_raw();
Expand Down
1 change: 1 addition & 0 deletions uplink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//! documentation and error messages when referencing the low level crate.
#![deny(missing_docs)]
#![deny(clippy::undocumented_unsafe_blocks)]

pub mod access;
pub mod bucket;
Expand Down
8 changes: 8 additions & 0 deletions uplink/src/object/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ impl Upload {

/// Updates the custom metadata to be included with the object.
pub fn set_custom_metadata(&mut self, metadata: &mut metadata::Custom) -> Result<()> {
// SAFETY: We are sure that FFI doesn't take ownership of the two parameters.
// Metadata FFI type is obtained from a safe wrapper.
let err = unsafe {
ulksys::uplink_upload_set_custom_metadata(
self.inner.upload,
Expand Down Expand Up @@ -236,6 +238,9 @@ impl Info {
let is_prefix = upload.is_prefix;
let upload_id;
let key;
// SAFETY: We have guarantee that upload fields aren't null through the `ensure` method
// call of the `Ensurer` trait. We make a copy of the C string pointers to Rust strings,
// and we free the memory of the C strings to not leak memory.
unsafe {
upload_id = CStr::from_ptr(upload.upload_id)
.to_str()
Expand Down Expand Up @@ -445,6 +450,9 @@ impl PartUpload {
}

let c_etag = res.expect("BUG: this result was verified to be an Ok, probably the check has been accidentally removed due to a refactoring");
// SAFETY: We get a pointer from a Rust string after guarantee that doesn't contain any 0
// byte (NULL byte) and we pass it to the FFI.
// `self.inner` is guarantee to be valid by the constructor of this struct.
let err = unsafe {
ulksys::uplink_part_upload_set_etag(
self.inner.part_upload,
Expand Down

0 comments on commit 497c270

Please sign in to comment.