From b141e8d0e1ac32165e908519bff75def327c0480 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 27 Sep 2022 16:39:19 +0800 Subject: [PATCH] Do not use stable hashing with all-zero seed. --- CHANGELOG.md | 3 ++- build.rs | 1 - src/config.rs | 1 - src/func/hashing.rs | 6 +++++- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 70fc8550..28fe4604 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,8 @@ New features ### Stable hashing -* It is now possible to specify a fixed _seed_ for use with the `ahash` hasher in order to force stable (i.e. deterministic) hashes for function signatures. +* It is now possible to specify a fixed _seed_ for use with the `ahash` hasher, via an environment variable, in order to force stable (i.e. deterministic) hashes for function signatures. This is necessary when using Rhai across shared-library boundaries. +* A build script is now used to extract the environment variable (`RHAI_AHASH_SEED`) and splice it into the source code before compilation. Enhancements ------------ diff --git a/build.rs b/build.rs index d17bb3da..a8fa7496 100644 --- a/build.rs +++ b/build.rs @@ -10,7 +10,6 @@ fn main() { f.write_fmt(format_args!( "//! Configuration settings for this Rhai build -#![allow(dead_code)] " )) diff --git a/src/config.rs b/src/config.rs index 43d49b6a..91e4716a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,3 @@ //! Configuration settings for this Rhai build -#![allow(dead_code)] pub const AHASH_SEED: Option<[u64; 4]> = None; diff --git a/src/func/hashing.rs b/src/func/hashing.rs index 53eac8c3..cee84062 100644 --- a/src/func/hashing.rs +++ b/src/func/hashing.rs @@ -76,7 +76,11 @@ impl BuildHasher for StraightHasherBuilder { #[must_use] pub fn get_hasher() -> ahash::AHasher { if let Some([seed1, seed2, seed3, seed4]) = config::AHASH_SEED { - ahash::RandomState::with_seeds(seed1, seed2, seed3, seed4).build_hasher() + if seed1 | seed2 | seed3 | seed4 != 0 { + ahash::RandomState::with_seeds(seed1, seed2, seed3, seed4).build_hasher() + } else { + ahash::AHasher::default() + } } else { ahash::AHasher::default() }