Skip to content

DerefMut on Gc<T> allows undefined behaviour #41

@Frawstcrabs

Description

@Frawstcrabs

As the Gc type implements DerefMut, one can write the following safe code:

let gc_val = Gc::new(5);
let ptr1 = gc_val;
let ptr2 = gc_val;
let ref1 = ptr1.deref_mut();
let ref2 = ptr2.deref_mut();

At this point, there are now two simultaneous mutable references to the same value, which is undefined behaviour.
I would recommend the DerefMut trait be removed from Gc<T> and to leave only Deref, as only immutable references can be safely created for a type managing shared ownership. Those wishing to add mutability should wrap the value in a RefCell<T>, as one would with Rc<T>.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions