Skip to content

Bug in the 7.12 (A Production Unsafe Deque) final code #315

@Tomer-Eliahu

Description

@Tomer-Eliahu

I think there is a bug in the final code. By copying the 7.12 final code to the Rust playground and running the following you can observe it:

        let mut m: LinkedList<u32> = LinkedList::new();
        m.extend([1, 2, 3, 4, 5, 6]);
        let mut cursor = m.cursor_mut();
        assert_eq!(cursor.current(), None);
        cursor.move_next();
        assert_eq!(cursor.current(), Some(&mut 1));
        let before = cursor.split_before();
        
        //prints false then the program panics
        println!("{:#?}", before.front.is_none());

The issue is that in the new list before we create, the front still points to the node with element 1 in m.

This bug is from the split_before function in 7.10:

pub fn split_before(&mut self) -> LinkedList<T> {
    if let Some(cur) = self.cur {
        // We are pointing at a real element, so the list is non-empty.
        unsafe {
            //...
            // What the output will become
            let output_front = self.list.front; // <-- incorrect if we are at index 0 (the first non-ghost node)
            //.....
            LinkedList {
                front: output_front, <-- this is still self.list.front.
                back: output_back,
                len: output_len,
                _boo: PhantomData,
            }
           //.....
}

There is also a mirror of this bug in split_after (where output_back = self.list.back even if the cursor is on the last non-ghost node in the list).
You can observe it by running:

        let mut m: LinkedList<u32> = LinkedList::new();
        m.extend([1, 2, 3, 4, 5, 6]);
        let mut cursor = m.cursor_mut();
        assert_eq!(cursor.current(), None);
        cursor.move_prev();
        assert_eq!(cursor.current(), Some(&mut 6));
        let after = cursor.split_after();
        
        //prints false
        println!("{:#?}", after.back.is_none());

Metadata

Metadata

Assignees

No one assigned

    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