-
Notifications
You must be signed in to change notification settings - Fork 169
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: Remove unsafe uses of from_raw_parts from crc32 and murmur hash functions #554
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ | |
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use byteorder::{LittleEndian, ReadBytesExt}; | ||
|
||
fn hash_(data: &[u8], seed: u32) -> u32 { | ||
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] | ||
unsafe { | ||
|
@@ -45,17 +47,19 @@ const MURMUR_R: i32 = 47; | |
unsafe fn murmur_hash2_64a(data_bytes: &[u8], seed: u64) -> u64 { | ||
let len = data_bytes.len(); | ||
let len_64 = (len / 8) * 8; | ||
let data_bytes_64 = | ||
std::slice::from_raw_parts(&data_bytes[0..len_64] as *const [u8] as *const u64, len / 8); | ||
let num_u64 = len / 8; | ||
|
||
let mut h = seed ^ (MURMUR_PRIME.wrapping_mul(data_bytes.len() as u64)); | ||
for v in data_bytes_64 { | ||
let mut k = *v; | ||
let mut offset = 0; | ||
for _ in 0..num_u64 { | ||
let mut buf = &data_bytes[offset..offset + 8]; | ||
let mut k = buf.read_u64::<LittleEndian>().unwrap(); | ||
k = k.wrapping_mul(MURMUR_PRIME); | ||
k ^= k >> MURMUR_R; | ||
k = k.wrapping_mul(MURMUR_PRIME); | ||
h ^= k; | ||
h = h.wrapping_mul(MURMUR_PRIME); | ||
offset += 8; | ||
} | ||
|
||
let data2 = &data_bytes[len_64..]; | ||
|
@@ -106,16 +110,12 @@ unsafe fn crc32_hash(bytes: &[u8], seed: u32) -> u32 { | |
let num_words = num_bytes / u32_num_bytes; | ||
num_bytes %= u32_num_bytes; | ||
|
||
let bytes_u32: &[u32] = std::slice::from_raw_parts( | ||
&bytes[0..num_words * u32_num_bytes] as *const [u8] as *const u32, | ||
num_words, | ||
); | ||
Comment on lines
-109
to
-112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This violates a precondition for |
||
|
||
let mut offset = 0; | ||
let mut hash = seed; | ||
while offset < num_words { | ||
hash = _mm_crc32_u32(hash, bytes_u32[offset]); | ||
offset += 1; | ||
for _ in 0..num_words { | ||
let mut buf = &bytes[offset..offset + 4]; | ||
hash = _mm_crc32_u32(hash, buf.read_u32::<LittleEndian>().unwrap()); | ||
offset += 4; | ||
} | ||
|
||
offset = num_words * u32_num_bytes; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This violates a precondition for
from_raw_parts
becausedata_bytes
is not guaranteed to be correctly aligned foru64
.