Skip to content

[Core] Array and Dictionary read only behavior is unsafe #93702

Open
@AThousandShips

Description

@AThousandShips

Tested versions

N/A

System information

N/A

Issue description

For example:

You also have that, in C++, the following is broken, for example:

Array tmp;
tmp.push_back(1);
tmp.push_back(2);
tmp.make_read_only();
assert(tmp[0] != tmp[1]);

All due to the fact that the return from the subscript operator and Array::get return a reference to a single internal read_only value

This would also cause problems in cases like:

const Variant &first_value = arr[0];

for (int i = 1; i < arr.size(); ++i) {
    // Do something involving access to `arr`, if the array is read-only it will change the value in `first_value`.
}

The main problem is that the non-const subscript operator has to return a non-const reference, if this wasn't necessary we could safely return const Variant & for the const operator and just trust that no one messes with it with casting, just like the const operator for LocalVector, but since we might be working with a non-const but read-only Array reference we can't reasonably error or crash when accessing the non-const operator.


A possible temporary improvement would be to remove the use of the read-only data in the const access cases (this is already the case in Dictionary), that would reduce the impact somewhat, but the non-const case is more tricky.

Steps to reproduce

N/A

Minimal reproduction project (MRP)

N/A

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions