# HG changeset patch # User Yuya Nishihara # Date 2019-09-01 08:35:14 # Node ID 01d3ce3281cf68fdfa6e63d47093dff78da46903 # Parent f75f47b3ea416b97ff42e725d50f5047f59e822e rust-cpython: fix unsafe inner(py).borrow_mut() calls Since self.inner is managed by PySharedState, it must not be borrowed mutably through the RefCell interface. Otherwise, the underlying object could be mutated while a reference is leaked to Python world. diff --git a/rust/hg-cpython/src/dirstate/dirstate_map.rs b/rust/hg-cpython/src/dirstate/dirstate_map.rs --- a/rust/hg-cpython/src/dirstate/dirstate_map.rs +++ b/rust/hg-cpython/src/dirstate/dirstate_map.rs @@ -169,8 +169,7 @@ py_class!(pub class DirstateMap |py| { Ok(filename?.extract::(py)?.data(py).to_owned()) }) .collect(); - self.inner(py) - .borrow_mut() + self.borrow_mut(py)? .clear_ambiguous_times(files?, now.extract(py)?); Ok(py.None()) } @@ -205,25 +204,20 @@ py_class!(pub class DirstateMap |py| { def hastrackeddir(&self, d: PyObject) -> PyResult { let d = d.extract::(py)?; - Ok(self - .inner(py) - .borrow_mut() + Ok(self.borrow_mut(py)? .has_tracked_dir(d.data(py)) .to_py_object(py)) } def hasdir(&self, d: PyObject) -> PyResult { let d = d.extract::(py)?; - Ok(self - .inner(py) - .borrow_mut() + Ok(self.borrow_mut(py)? .has_dir(d.data(py)) .to_py_object(py)) } def parents(&self, st: PyObject) -> PyResult { - self.inner(py) - .borrow_mut() + self.borrow_mut(py)? .parents(st.extract::(py)?.data(py)) .and_then(|d| { Ok((PyBytes::new(py, &d.p1), PyBytes::new(py, &d.p2)) @@ -241,16 +235,13 @@ py_class!(pub class DirstateMap |py| { let p1 = extract_node_id(py, &p1)?; let p2 = extract_node_id(py, &p2)?; - self.inner(py) - .borrow_mut() + self.borrow_mut(py)? .set_parents(&DirstateParents { p1, p2 }); Ok(py.None()) } def read(&self, st: PyObject) -> PyResult> { - match self - .inner(py) - .borrow_mut() + match self.borrow_mut(py)? .read(st.extract::(py)?.data(py)) { Ok(Some(parents)) => Ok(Some( @@ -353,7 +344,7 @@ py_class!(pub class DirstateMap |py| { def getdirs(&self) -> PyResult { // TODO don't copy, share the reference - self.inner(py).borrow_mut().set_dirs(); + self.borrow_mut(py)?.set_dirs(); Dirs::from_inner( py, DirsMultiset::from_dirstate( @@ -364,7 +355,7 @@ py_class!(pub class DirstateMap |py| { } def getalldirs(&self) -> PyResult { // TODO don't copy, share the reference - self.inner(py).borrow_mut().set_all_dirs(); + self.borrow_mut(py)?.set_all_dirs(); Dirs::from_inner( py, DirsMultiset::from_dirstate( @@ -422,8 +413,7 @@ py_class!(pub class DirstateMap |py| { ) -> PyResult { let key = key.extract::(py)?; let value = value.extract::(py)?; - self.inner(py) - .borrow_mut() + self.borrow_mut(py)? .copy_map .insert(key.data(py).to_vec(), value.data(py).to_vec()); Ok(py.None()) @@ -434,7 +424,7 @@ py_class!(pub class DirstateMap |py| { default: Option ) -> PyResult> { let key = key.extract::(py)?; - match self.inner(py).borrow_mut().copy_map.remove(key.data(py)) { + match self.borrow_mut(py)?.copy_map.remove(key.data(py)) { Some(_) => Ok(None), None => Ok(default), }