-
-
Notifications
You must be signed in to change notification settings - Fork 19.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Core] Array
and Dictionary
read only behavior is unsafe
#93702
Comments
A potentially dirty but I'd say sufficient solution might be to add a proxy for the non-const return, akin to Alternatively the proxy would simply work like the Edit: Doing some testing with a proxy based solution |
We could also make the |
The |
My idea was the following : |
Again I'd say this fix should be local, not in |
I agree with you, and the proxy sounds like a good idea ! Just to be sure I understand what you meant :
The potential issue I see here is that if the |
With some testing the immediate concern is that the resulting value would need to copy a lot of behaviour from Adding some read-only state to Will keep testing out some ideas for the proxy and potential adjustments across the code to account for it The most trivial solution would be to replace the current read only data with another vector, but that would be pretty fragile I think, and wasteful, the issue is having the ability to produce a unique, discardable value for each access, but that would be difficult without leaking memory The even more trivial but less safe solution would just be to not protect the subscript at all, this might be an acceptable temporary solution |
Some candidate solutions:
Did some basic work but probably won't be able to focus on a direct fix on this for the time being, but some loose pointers |
That proxy return variant is cursed. 🙈 There are 3 constraints the array currently tries to fulfill: Variant get(int p_index) const;
Variant const& get_ref(int p_index) const;
Variant& get_mutable_ref(int p_index); Any solution needs to satisfy the above. The get_ref could be deleted. |
Tested versions
N/A
System information
N/A
Issue description
For example:
You also have that, in C++, the following is broken, for example:
All due to the fact that the return from the subscript operator and
Array::get
return a reference to a single internalread_only
valueThis would also cause problems in cases like:
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 forLocalVector
, but since we might be working with a non-const but read-onlyArray
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
The text was updated successfully, but these errors were encountered: