-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Integer Overflow in
FuzzyList
(#569)
Turns out the code I ported for the `FuzzyList` essentially counts consecutive ranges of characters that match in both strings. The way it does this is by multiplying the score by 2 for each matching character. So this quickly overflows "any" integer type. We could saturate, but since the score is fairly rough anyway and the original implementation is in JavaScript where floating point numbers are used anyway, we simply do the same here and switch to f64. Sorting floating point numbers is of course iffy, but we don't even have any `NaN` values here anyway, so we can just use a simplified ordering.
- Loading branch information
Showing
4 changed files
with
36 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use core::cmp::Ordering; | ||
|
||
#[repr(transparent)] | ||
pub struct NotNaN(pub f64); | ||
|
||
impl PartialEq for NotNaN { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.cmp(other) == Ordering::Equal | ||
} | ||
} | ||
|
||
impl Eq for NotNaN {} | ||
|
||
impl PartialOrd for NotNaN { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
|
||
impl Ord for NotNaN { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
self.0.partial_cmp(&other.0).unwrap_or(Ordering::Equal) | ||
} | ||
} |