From c6903162c4ca9f56189b911cdaaf76714f7a0207 Mon Sep 17 00:00:00 2001 From: Evan Mesterhazy Date: Thu, 15 Feb 2024 22:33:36 -0500 Subject: [PATCH] Fix the ContentHash implementations for std::Option and MergedTreeId The `ContentHash` documentation specifies that implementations for enums should hash the ordinal number of the variant contained in the enum as a 32-bit little-endian number and then hash the contents of the variant, if any. The current implementations for `std::Option` and `MergedTreeId` are non-conformant since they hash the ordinal number as a u8 with platform specific endianness. Fixes #3051 --- lib/src/backend.rs | 4 ++-- lib/src/content_hash.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/src/backend.rs b/lib/src/backend.rs index 76779804b34..edc81b31851 100644 --- a/lib/src/backend.rs +++ b/lib/src/backend.rs @@ -114,11 +114,11 @@ impl ContentHash for MergedTreeId { fn hash(&self, state: &mut impl digest::Update) { match self { MergedTreeId::Legacy(tree_id) => { - state.update(b"0"); + state.update(&0u32.to_le_bytes()); ContentHash::hash(tree_id, state); } MergedTreeId::Merge(tree_ids) => { - state.update(b"1"); + state.update(&1u32.to_le_bytes()); ContentHash::hash(tree_ids, state); } } diff --git a/lib/src/content_hash.rs b/lib/src/content_hash.rs index 8f5d67a3a71..5b25797db0b 100644 --- a/lib/src/content_hash.rs +++ b/lib/src/content_hash.rs @@ -76,9 +76,9 @@ impl ContentHash for String { impl ContentHash for Option { fn hash(&self, state: &mut impl digest::Update) { match self { - None => state.update(&[0]), + None => state.update(&0u32.to_le_bytes()), Some(x) => { - state.update(&[1]); + state.update(&0u32.to_le_bytes()); x.hash(state) } }