Skip to content
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] Fix property access on read-only Dictionary #89647

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions core/variant/variant_setget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,21 @@ void Variant::set_named(const StringName &p_member, const Variant &p_value, bool
return;
}
} else if (type == Variant::DICTIONARY) {
Variant *v = VariantGetInternalPtr<Dictionary>::get_ptr(this)->getptr(p_member);
Dictionary &dict = *VariantGetInternalPtr<Dictionary>::get_ptr(this);

if (dict.is_read_only()) {
r_valid = false;
return;
}

Variant *v = dict.getptr(p_member);
if (v) {
*v = p_value;
r_valid = true;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would just silently fail before as it uses the read-only temporary

} else {
VariantGetInternalPtr<Dictionary>::get_ptr(this)->operator[](p_member) = p_value;
r_valid = true;
dict[p_member] = p_value;
}

r_valid = true;
} else {
r_valid = false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
func test():
var dictionary := { "a": 0 }
dictionary.make_read_only()
dictionary.a = 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
GDTEST_RUNTIME_ERROR
>> SCRIPT ERROR
>> on function: test()
>> runtime/errors/read_only_dictionary.gd
>> 4
>> Invalid assignment of property or key 'a' with value of type 'int' on a base object of type 'Dictionary'.
Loading