Skip to content

Commit 273a643

Browse files
committed
Merge pull request godotengine#89647 from AThousandShips/read_only_dict
[Core] Fix property access on read-only `Dictionary`
2 parents 85062e3 + ec29c3e commit 273a643

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

core/variant/variant_setget.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,21 @@ void Variant::set_named(const StringName &p_member, const Variant &p_value, bool
251251
return;
252252
}
253253
} else if (type == Variant::DICTIONARY) {
254-
Variant *v = VariantGetInternalPtr<Dictionary>::get_ptr(this)->getptr(p_member);
254+
Dictionary &dict = *VariantGetInternalPtr<Dictionary>::get_ptr(this);
255+
256+
if (dict.is_read_only()) {
257+
r_valid = false;
258+
return;
259+
}
260+
261+
Variant *v = dict.getptr(p_member);
255262
if (v) {
256263
*v = p_value;
257-
r_valid = true;
258264
} else {
259-
VariantGetInternalPtr<Dictionary>::get_ptr(this)->operator[](p_member) = p_value;
260-
r_valid = true;
265+
dict[p_member] = p_value;
261266
}
262267

268+
r_valid = true;
263269
} else {
264270
r_valid = false;
265271
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
func test():
2+
var dictionary := { "a": 0 }
3+
dictionary.make_read_only()
4+
dictionary.a = 1
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
GDTEST_RUNTIME_ERROR
2+
>> SCRIPT ERROR
3+
>> on function: test()
4+
>> runtime/errors/read_only_dictionary.gd
5+
>> 4
6+
>> Invalid assignment of property or key 'a' with value of type 'int' on a base object of type 'Dictionary'.

0 commit comments

Comments
 (0)