godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
90.97k stars 21.16k forks source link

var_to_str may stuck in infinite loop #96418

Open jinyangcruise opened 2 months ago

jinyangcruise commented 2 months ago

Tested versions

System information

Windows 11, Vulkan API 1.3.205 - Forward+ - Using Vulkan Device #0: NVIDIA - NVIDIA GeForce RTX 3060 Ti

Issue description

In some cases, var_to_str may lead to an infinite loop. for example:

extends Node2D

var a
var b

func _ready() -> void:
    a = A.new()
    a.parent = self

    b = B.new() # Comment out these two lines, and the infinite loop will not occur.
    b.parent = self # Comment out these two lines, and the infinite loop will not occur.

    print(var_to_str(self))

class A:
    var parent

class B:
    var parent

Steps to reproduce

  1. open MRP and run

Minimal reproduction project (MRP)

test_var_to_str.zip

jsjtxietian commented 2 months ago

The parent makes it recursively calls write many times.

for (const PropertyInfo &E : props) {
    if (E.usage & PROPERTY_USAGE_STORAGE || E.usage & PROPERTY_USAGE_SCRIPT_VARIABLE) {
        //must be serialized

        if (first) {
            first = false;
        } else {
            p_store_string_func(p_store_string_ud, ",");
        }

        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);
    }
}