Skip to content
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

Unnecessary and unsound use of unsafe #581

Open
albertsgarde opened this issue Apr 5, 2024 · 1 comment
Open

Unnecessary and unsound use of unsafe #581

albertsgarde opened this issue Apr 5, 2024 · 1 comment

Comments

@albertsgarde
Copy link

In pattern_matching/myers/simple.rs, the implementation of the StatesHandler trait for ShortStatesHandler uses unsafe in the methods set_max_state and add_state. Current code:

    #[inline]
    fn set_max_state(&self, pos: usize, states: &mut [State<T, T::DistType>]) {
        //states[pos] = State::max();
        *unsafe { states.get_unchecked_mut(pos) } = State::max();
    }

    #[inline]
    fn add_state(
        &self,
        source: &Self::TracebackColumn,
        pos: usize,
        states: &mut [State<T, T::DistType>],
    ) {
        //states[pos] = source.clone();
        *unsafe { states.get_unchecked_mut(pos) } = *source;
    }

These uses of unsafe are both unnecessary and allow the caller to cause UB from safe Rust.
They are unnecessary since they can trivially be implemented in safe rust (as demonstrated by the commented code just above), and the performance benefits are almost certainly negligible (according to much discussion I've read which I unfortunately can't find atm).
Regardless of possible performance benefits, allowing UB from safe Rust should never be allowed and so they should be removed.

@albertsgarde
Copy link
Author

Fixed in #582

@albertsgarde albertsgarde changed the title Unnecessary and faulty use of unsafe Unnecessary and unsound use of unsafe Apr 7, 2024
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

No branches or pull requests

1 participant