Skip to content

Document all unsafe blocks. #562

Open
Open

Description

@reitermarkus

All unsafe blocks should be documented with a SAFETY comment and be scoped as closely as possible.

After all are documented, the clippy::undocumented_unsafe_blocks lint should be enabled.

Also, the unsafe_op_in_unsafe_fn lint should be enabled (default in edition 2024), so that unsafe blocks in unsafe functions are also documented.

Activity

  1. added this to the v1.0.0 milestone on Apr 26, 2025
  2. Mortano commented on Aug 13, 2025

    @Mortano
    Contributor

    I should be able to help with that, if the help is still wanted. Are there any contribution guidelines for first-time contributors?

  3. zeenix commented on Aug 13, 2025

    @zeenix
    Contributor

    I should be able to help with that, if the help is still wanted.

    I'm pretty sure it is. :) Thanks.

    Are there any contribution guidelines for first-time contributors?

    None really. We do have a CoC (see CODE_OF_CONDUCT.md file) but that's pretty normal these days.

  4. Mortano commented on Aug 13, 2025

    @Mortano
    Contributor

    Ok I'll start working on this then :)

  5. Mortano commented on Aug 15, 2025

    @Mortano
    Contributor

    Do you have a preference as to how detailed the documentation should be? Unsafe Rust is a tricky beast and many of the unsafe functions that are used have quite a long list of safety requirements. The way I'm approaching this now is to go through these lists and verify that each requirement is met and document that accordingly, but that makes the SAFETY comments quite long in some cases. Here is one example in DequeInner::make_contiguous, which uses a lot of pointer arithmetics and raw memcpy:

    ...
    } else if free >= back_len {
        // there is enough free space to copy the tail in one go,
        // this means that we first shift the head forwards, and then
        // copy the tail to the correct position.
        //
        // from: FGH....ABCDE
        // to:   ...ABCDEFGH.
    
        // SAFETY: Explaining it visually is easier:
        // step 1:   FGH....ABCDE
        //              |   |
        //              1   2
        //           |__|___|___|
        //  back_len---^  ^   ^---- front_len
        //              free
        // step 2:   FGHABCDE....
        //           |       |
        //           3       4
        // Pointers: 1: `buffer_ptr.add(self.back)`
        //           2: `buffer_ptr.add(self.front)`
        //           3: `buffer_ptr`
        //           4: `buffer_ptr.add(self.back + front_len)`
        // All pointers are clearly within the underlying buffer and are properly aligned, meeting the first safety
        // requirement for `ptr.add` and the `ptr::copy...` variants.
        // In step 1 above, we see that pointer 1 is always `<=` than pointer 2, so the copy stays within the bounds
        // of the buffer, but it might overlap, hence the usage of `ptr::copy`
        // In step 2 above, we see that the target memory region does not overlap with the source region, as pointer 4
        // is larger than pointer 3 plus `back_len`. Finally the branch condition assures that the gap is large enough
        // to copy `back_len` elements into
        unsafe {
            ptr::copy(
                buffer_ptr.add(self.front),
                buffer_ptr.add(self.back),
                front_len,
            );
            // FGHABCDE....
            ptr::copy_nonoverlapping(
                buffer_ptr,
                buffer_ptr.add(self.back + front_len),
                back_len,
            );
            // ...ABCDEFGH.
        }
    
        self.front = back;
        self.back = 0;
    }
    ...

    The unit tests should catch the various edge cases that the algorithm itself has, so maybe I could reduce the comment length and simply state "We ensure that all pointers always stay within their bounds through the tests"?

  6. zeenix commented on Aug 16, 2025

    @zeenix
    Contributor

    Do you have a preference as to how detailed the documentation should be?

    I can't speak for @reitermarkus here but IMO while your attention to detail is appreciated, I really don't think we need this level of detail. Typically, you'd just have a bullet list for each safety requirement mentioned in the documentation of the API being used, shortly explaining why/how the requirement is satisfied.

    I typically keep them short but probably that's a bit too short. :)

  7. Mortano commented on Aug 17, 2025

    @Mortano
    Contributor

    Ok I'll try to keep it more concise then, thanks for the clarification

  8. Mortano commented on Aug 20, 2025

    @Mortano
    Contributor

    I found one potential safety violation related to #579 (see my comment in there). I'll continue checking and commenting the rest of the unsafe code and will comment here if I find something else, then we can discuss how to deal with each issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions