You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
DoubleArray::get_unit() calls slice::get_unchecked() internally, but this slice is not checked in DoubleArray::new().
So, it is possible to cause a segmentation fault even though the caller does not use unsafe blocks.
use yada::DoubleArray;fnmain(){let da = DoubleArray::new(vec![]);ifletSome(index) = da.exact_match_search("example input"){dbg!(index);}}
$ cargo run --release
...
[1] 81236 segmentation fault cargo run --release
Solution 1: Change DoubleArray::new() to an unsafe function
This solution does not pay the additional runtime cost, but requires callers to be responsible for using the correct slice.
let da = unsafe{DoubleArray::new(trusted_bytes)};
Solution 2: Change slice::get_unchecked() to a checked function
The opposite of Solution 1.
let b = &self.0[index *UNIT_SIZE..(index + 1)*UNIT_SIZE];
or
let b = self.0.get(index *UNIT_SIZE..(index + 1)*UNIT_SIZE).ok_or_else(|| ...)?;
Solution 3: Check the argument of DoubleArray::new()
This is too much work, but it does not require any responsibility on callers and does not pay any additional costs during the search.
The text was updated successfully, but these errors were encountered:
DoubleArray::get_unit()
callsslice::get_unchecked()
internally, but this slice is not checked inDoubleArray::new()
.So, it is possible to cause a segmentation fault even though the caller does not use
unsafe
blocks.Solution 1: Change
DoubleArray::new()
to an unsafe functionThis solution does not pay the additional runtime cost, but requires callers to be responsible for using the correct slice.
Solution 2: Change
slice::get_unchecked()
to a checked functionThe opposite of Solution 1.
or
Solution 3: Check the argument of
DoubleArray::new()
This is too much work, but it does not require any responsibility on callers and does not pay any additional costs during the search.
The text was updated successfully, but these errors were encountered: