From e0bf8a240376cf8d7c85dc20abfeb64189665e0f Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Thu, 25 Jan 2024 14:54:14 +0100 Subject: [PATCH] fix some bugs in concat_byte_chunks, serialize_bsatn --- .../core/src/db/datastore/locking_tx_datastore/mod.rs | 11 +++-------- crates/sats/src/algebraic_value/ser.rs | 9 +++++++-- crates/sats/src/bsatn/ser.rs | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/crates/core/src/db/datastore/locking_tx_datastore/mod.rs b/crates/core/src/db/datastore/locking_tx_datastore/mod.rs index fec445e9c5b..b6520a1ef23 100644 --- a/crates/core/src/db/datastore/locking_tx_datastore/mod.rs +++ b/crates/core/src/db/datastore/locking_tx_datastore/mod.rs @@ -816,17 +816,12 @@ impl TxState { } } -struct SequencesState { +#[derive(Default)] +pub struct SequencesState { sequences: HashMap, } impl SequencesState { - pub fn new() -> Self { - Self { - sequences: HashMap::new(), - } - } - pub fn get_sequence_mut(&mut self, seq_id: SequenceId) -> Option<&mut Sequence> { self.sequences.get_mut(&seq_id) } @@ -1832,7 +1827,7 @@ impl Locking { Self { memory: Arc::new(Mutex::new(BTreeMap::new())), committed_state: Arc::new(RwLock::new(CommittedState::new())), - sequence_state: Arc::new(Mutex::new(SequencesState::new())), + sequence_state: Arc::new(Mutex::new(SequencesState::default())), database_address, } } diff --git a/crates/sats/src/algebraic_value/ser.rs b/crates/sats/src/algebraic_value/ser.rs index c290d741360..cfcb7a6b696 100644 --- a/crates/sats/src/algebraic_value/ser.rs +++ b/crates/sats/src/algebraic_value/ser.rs @@ -122,11 +122,16 @@ impl ser::Serializer for ValueSerializer { /// /// - `total_len == chunks.map(|c| c.len()).sum() <= isize::MAX` unsafe fn concat_byte_chunks<'a>(total_len: usize, chunks: impl Iterator) -> Vec { + if total_len == 0 { + return Vec::new(); + } + // Allocate space for `[u8; total_len]` on the heap. let layout = Layout::array::(total_len); // SAFETY: Caller promised that `total_len <= isize`. let layout = unsafe { layout.unwrap_unchecked() }; - let ptr = alloc::alloc(layout); + // SAFETY: We checked above that `layout.size() != 0`. + let ptr = unsafe { alloc::alloc(layout) }; if ptr.is_null() { alloc::handle_alloc_error(layout); } @@ -157,7 +162,7 @@ unsafe fn concat_byte_chunks<'a>(total_len: usize, chunks: impl Iterator ser::Serializer for Serializer<'_, W> { value.serialize(self) } - unsafe fn serialize_bsatn(self, ty: &crate::AlgebraicType, mut bsatn: &[u8]) -> Result { - debug_assert!(AlgebraicValue::decode(ty, &mut bsatn).is_ok()); + unsafe fn serialize_bsatn(self, ty: &crate::AlgebraicType, bsatn: &[u8]) -> Result { + debug_assert!(AlgebraicValue::decode(ty, &mut { bsatn }).is_ok()); self.writer.put_slice(bsatn); Ok(()) }