Skip to content

Soundness issue with Dictionary find/get #720

Open
@capickett

Description

@capickett

Adding this test to dictionary.rs exhibits a runtime crash when running on an Apple Silicon mac with cargo test.

    #[test]
    fn this_is_ub() {
        let mut mut_dict: CFMutableDictionary<CFString, CFString> = CFMutableDictionary::new();
        let string_key = CFString::from_static_string("Bar");
        // make a runtime string that assuredly comes from the heap
        let string_val = CFString::new(format!("{:?}", SystemTime::now()).as_str());
        mut_dict.add(
            &string_key,
            &string_val,
        );

        let dict = mut_dict.to_immutable();
        let val = dict.get(string_key);

        mut_dict.remove_all();
        drop(string_val);

        print!("{}", *val);
    }

The issue here is that dict.get() returns an ItemRef<'a, CFString> which is tied to the lifetime of dict. However, dict is aliasing the same underlying storage as mut_dict, which means nothing prevents us removing the value and causing UB.

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