y-crdt / y-octo

CRDT implementation which is compatible with https://github.com/yjs/yjs
https://octobase.pro/
Other
187 stars 5 forks source link

fix: soundness issue in Somr #9

Closed zxch3n closed 1 year ago

zxch3n commented 1 year ago

Currently, the following code causes undefined behavior.

    #[test]
    fn test_inner_mut() {
        let five = Somr::new(5);
        fn add(a: &Somr<i32>, b: &Somr<i32>) {
            a.get_mut().map(|x| *x += *b.get().unwrap()).unwrap();
        }

        add(&five, &five);
        assert_eq!(five.get().copied().unwrap(), 10);
    }
CleanShot 2023-08-25 at 23 04 22@2x

The original fn get_mut(&self) -> Option<&mut T> is error-prone, as the compiler cannot prevent programmers from creating multiple mut ref to the same element. Making it unsafe could reduce the scope that needs to be audited.