Skip to content

Commit 91a8062

Browse files
Timmmmllogiq
authored andcommitted
Fix WASM chunk_count
The loop condition was incorrect which meant it would read beyond the end of `haystack` and crash if it went beyond the end of memory. It would probably give incorrect counts too.
1 parent 240c02c commit 91a8062

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/simd/wasm.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ unsafe fn u8x16_from_offset(slice: &[u8], offset: usize) -> v128 {
1616
v128_load(slice.as_ptr().add(offset) as *const _)
1717
}
1818

19+
// Load four 16-byte vectors from a slice at a given offset.
20+
// This function assumes that the slice has at least 64 bytes available from the offset.
1921
#[target_feature(enable = "simd128")]
2022
unsafe fn u8x16x4_from_offset(slice: &[u8], offset: usize) -> (v128, v128, v128, v128) {
2123
debug_assert!(
@@ -76,7 +78,7 @@ pub unsafe fn chunk_count(haystack: &[u8], needle: u8) -> usize {
7678
let mut count = 0;
7779
let mut offset = 0;
7880

79-
while haystack.len() >= offset + 16 * 255 {
81+
while haystack.len() >= offset + 64 * 255 {
8082
let (mut count1, mut count2, mut count3, mut count4) = (
8183
u8x16_splat(0),
8284
u8x16_splat(0),

0 commit comments

Comments
 (0)