fix: do not set peer id with max (#491)

This commit is contained in:
Leon Zhao 2024-10-03 21:07:11 +08:00 committed by GitHub
parent a93efd5f51
commit b1e03d914e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 17 additions and 1 deletions

View file

@ -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<ContainerID> },
#[error("You cannot set the `PeerID` with `PeerID::MAX`, which is an internal specific value")]
InvalidPeerID,
}
#[derive(Error, Debug, PartialEq)]

View file

@ -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();

View file

@ -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)
);
}