fix var_to_str infinite loop

This commit is contained in:
Nazarii 2024-09-20 16:25:19 +03:00
parent 0a4aedb360
commit 563bebce31

View file

@ -2155,6 +2155,11 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str
List<PropertyInfo> props; List<PropertyInfo> props;
obj->get_property_list(&props); obj->get_property_list(&props);
thread_local HashSet<Object *> used_objects;
if (p_recursion_count == 0) {
used_objects.clear();
}
bool first = true; bool first = true;
for (const PropertyInfo &E : props) { for (const PropertyInfo &E : props) {
if (E.usage & PROPERTY_USAGE_STORAGE || E.usage & PROPERTY_USAGE_SCRIPT_VARIABLE) { if (E.usage & PROPERTY_USAGE_STORAGE || E.usage & PROPERTY_USAGE_SCRIPT_VARIABLE) {
@ -2165,9 +2170,20 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str
} else { } else {
p_store_string_func(p_store_string_ud, ","); p_store_string_func(p_store_string_ud, ",");
} }
Variant v = obj->get(E.name);
if (v.get_type() == Variant::OBJECT) {
if (v.get_validated_object() == obj) {
p_store_string_func(p_store_string_ud, "\"" + String("self"));
continue;
}
if (used_objects.has(obj)) {
p_store_string_func(p_store_string_ud, "\"" + obj->get_class() + "\": Recursive object");
continue;
}
used_objects.insert(obj);
}
p_store_string_func(p_store_string_ud, "\"" + E.name + "\":"); p_store_string_func(p_store_string_ud, "\"" + E.name + "\":");
write(obj->get(E.name), p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud, p_recursion_count, p_compat); write(v, p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud, p_recursion_count, p_compat);
} }
} }