From b1e03d914ec66d71d6b8ba249ffe89d1a53ed686 Mon Sep 17 00:00:00 2001 From: Leon Zhao Date: Thu, 3 Oct 2024 21:07:11 +0800 Subject: [PATCH] fix: do not set peer id with max (#491) --- crates/loro-common/src/error.rs | 4 ++++ crates/loro-internal/src/loro.rs | 3 +++ crates/loro-internal/tests/test.rs | 11 ++++++++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/loro-common/src/error.rs b/crates/loro-common/src/error.rs index 48d47638..ee92787f 100644 --- a/crates/loro-common/src/error.rs +++ b/crates/loro-common/src/error.rs @@ -1,3 +1,5 @@ +use std::error; + use serde_columnar::ColumnarError; use thiserror::Error; @@ -90,6 +92,8 @@ pub enum LoroError { "The container {container} is deleted. You cannot apply the op on a deleted container." )] ContainerDeleted { container: Box }, + #[error("You cannot set the `PeerID` with `PeerID::MAX`, which is an internal specific value")] + InvalidPeerID, } #[derive(Error, Debug, PartialEq)] diff --git a/crates/loro-internal/src/loro.rs b/crates/loro-internal/src/loro.rs index b5f04fa5..488bfda3 100644 --- a/crates/loro-internal/src/loro.rs +++ b/crates/loro-internal/src/loro.rs @@ -261,6 +261,9 @@ impl LoroDoc { #[inline(always)] pub fn set_peer_id(&self, peer: PeerID) -> LoroResult<()> { + if peer == PeerID::MAX { + return Err(LoroError::InvalidPeerID); + } let next_id = self.oplog.try_lock().unwrap().next_id(peer); if self.auto_commit.load(Acquire) { let doc_state = self.state.try_lock().unwrap(); diff --git a/crates/loro-internal/tests/test.rs b/crates/loro-internal/tests/test.rs index c02caaf3..e56f87e2 100644 --- a/crates/loro-internal/tests/test.rs +++ b/crates/loro-internal/tests/test.rs @@ -1,7 +1,7 @@ use std::sync::{atomic::AtomicBool, Arc, Mutex}; use fxhash::FxHashMap; -use loro_common::{ContainerID, ContainerType, LoroResult, LoroValue, ID}; +use loro_common::{ContainerID, ContainerType, LoroError, LoroResult, LoroValue, PeerID, ID}; use loro_internal::{ delta::ResolvedMapValue, event::{Diff, EventTriggerKind}, @@ -1262,3 +1262,12 @@ fn test_map_contains_key() { map.delete("bro").unwrap(); assert!(!map.contains_key("bro")); } + +#[test] +fn set_max_peer_id() { + let doc = LoroDoc::new_auto_commit(); + assert_eq!( + doc.set_peer_id(PeerID::MAX), + Result::Err(LoroError::InvalidPeerID) + ); +}