Skip to content

Commit

Permalink
fix: overflow in qgrams for k=32 (#478)
Browse files Browse the repository at this point in the history
* Fix undefined behaviour overflow for k=32

* Add a test to RankTransform ensuring `1<<(q*bits)` does not overflow

* Update MSRV to 1.53 for usize::BITS
  • Loading branch information
RagnarGrootKoerkamp committed Feb 9, 2022
1 parent 48bac1c commit 8048eb8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Expand Up @@ -92,11 +92,11 @@ jobs:
- name: Install MSRV toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: 1.51.0
toolchain: 1.53.0
override: true

- name: check if README matches MSRV defined here
run: grep '1.51.0' README.md
run: grep '1.53.0' README.md

- name: Run tests
uses: actions-rs/cargo@v1
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -57,7 +57,7 @@ For extra credit, feel free to familiarize yourself with:

## Minimum supported Rust version

Currently the minimum supported Rust version is 1.51.0.
Currently the minimum supported Rust version is 1.53.0.

## License

Expand Down
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);
}
}

0 comments on commit 8048eb8

Please sign in to comment.