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

fix: overflow in qgrams for k=32 #478

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/alphabets/mod.rs
Expand Up @@ -326,7 +326,7 @@ impl RankTransform {
text: text.into_iter(),
ranks: self,
bits,
mask: (1 << (q * bits)) - 1,
mask: 1usize.checked_shl(q * bits).unwrap_or(0).wrapping_sub(1),
qgram: 0,
};

Expand Down Expand Up @@ -448,4 +448,13 @@ mod tests {
assert_eq!(Alphabet::new(b"ATCG"), Alphabet::new(b"TAGC"));
assert_ne!(Alphabet::new(b"ATCG"), Alphabet::new(b"ATC"));
}

/// When `q * bits == usize::BITS`, make sure that `1<<(1*bits)` does not overflow.
#[test]
fn test_qgram_shiftleft_overflow() {
let alphabet = Alphabet::new(b"ACTG");
let transform = RankTransform::new(&alphabet);
let text = b"ACTG".repeat(100);
transform.qgrams(usize::BITS / 2, text);
}
}