litegraph.js icon indicating copy to clipboard operation
litegraph.js copied to clipboard

`LiteGraph.cloneObject` leaves zombie array elements on inputs/outputs during `configure()` and `clone()`

Open codeCraft-Ritik opened this issue 3 weeks ago • 0 comments

  • Affected File: src/litegraph.js & src/litegraph.js
  • Affected Lines: Line 664–666 (cloneObject) and Line 2553 (LGraphNode.prototype.configure)
  • Component: Serialization & Cloning Subsystem
  • Severity: High (Data corruption / Schema inconsistency)

Description

LiteGraph.cloneObject(obj, target) copies serialized properties into an existing target container:

cloneObject: function(obj, target) {
    if (obj == null) return null;
    var r = JSON.parse(JSON.stringify(obj));
    if (!target) return r;
    for (var i in r) {
        target[i] = r[i];
    }
    return target;
}

When LGraphNode.prototype.configure configures dynamic containers like this.inputs or this.outputs:

this[j] = LiteGraph.cloneObject(info[j], this[j]);

If this[j] was initialized in the constructor with more items than the incoming state (e.g. node created with 3 default slots, but the saved state or clone has only 1 slot after dynamic removal), for (var i in r) iterates only over keys 0..r.length-1. It never resizes target.length.

As a result, deleted slots are resurrected as zombie inputs/outputs. This also corrupts node.clone(), which calls node.configure(data).

Steps to Reproduce

  1. Register a node type with 3 inputs in its constructor.
  2. Create an instance and remove 2 inputs.
  3. Call node.clone(), or configure a fresh instance with the 1-input state.
  4. Inspect node.inputs.length.

Expected Behavior

Configuring or cloning a node with 1 input slot must yield an instance with 1 input slot.

Actual Behavior

The node retains the 2 constructor slots at indices 1 and 2, resulting in 3 inputs (['new_slot', 'in2', 'in3']).

Minimal Test Case

function MultiSlotNode() {
    this.addInput("in1", "number");
    this.addInput("in2", "number");
    this.addInput("in3", "number");
}
LiteGraph.registerNodeType("test/multislot", MultiSlotNode);

const node = LiteGraph.createNode("test/multislot");
node.inputs.pop();
node.inputs.pop(); // Now has 1 input

const clone = node.clone();
console.log("Expected 1 input, got:", clone.inputs.length); // Prints 3
console.log("Slot names:", clone.inputs.map(i => i.name)); // ['in1', 'in2', 'in3']

Recommended Fix

In LiteGraph.cloneObject, truncate target if both target and r are arrays:

if (Array.isArray(target) && Array.isArray(r)) {
    target.length = r.length;
}

codeCraft-Ritik avatar Nov 07 '26 05:11 codeCraft-Ritik