Cyclic 'inherit' on TargetDefs causes an infinite loop and unbounded memory growth

Aperta Adatta ai principianti
#3,096 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

@shubhamrai9122-creator ci sta già lavorando.

Dal 26/10/2026.

  • #3121 di @shubhamrai9122-creator — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
88/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
cpp

Direzione di ricerca

Inizia in source/MaterialXCore/Definition.cpp, leggendo TargetDef::getMatchingTargets() e NodeDef::getImplementation(). Esegui i casi di test di regressione forniti per i cicli di ereditarietà propria e reciproca. Il lavoro è completato quando getMatchingTargets() restituisce ogni target una sola volta e getImplementation("T") o getImplementation("T1") restituisce nullptr senza bloccarsi né causare una crescita illimitata della memoria.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

<targetdef>s can inherit from themselves, or from each other in a cycle.

<materialx>
    <nodedef name="ND_x" node="foo"/>
    <targetdef name="T" inherit="T"/>
</materialx>

Calling getImplementation("T") on the nodedef never returns.

If you have a look at https://github.com/AcademySoftwareFoundation/MaterialX/blob/7d0baeeb0b88b24394cbb4cb73aa0794d641af0a/source/MaterialXCore/Definition.cpp

StringVec TargetDef::getMatchingTargets() const
{
    StringVec result = { getName() };
    ElementPtr base = getInheritsFrom();
    while (base)                              // <-- Never null if the chain is a cycle
    {
        result.push_back(base->getName());    // <-- Grows on every iteration
        base = base->getInheritsFrom();
    }
    return result;
}

This is called from NodeDef::getImplementation() whenever a non-empty target matches a targetdef:

const TargetDefPtr targetDef = getDocument()->getTargetDef(target);
const StringVec candidateTargets = targetDef ? targetDef->getMatchingTargets() : StringVec();

Running this regression test under timeout, the process never finishes, and memory keeps climbing because the result vector grows on each pass (got to about 275 MB after 10s).

// A cyclic targetdef inheritance chain must not hang getMatchingTargets(),
// which is reached from NodeDef::getImplementation() with a non-empty target.
TEST_CASE("TargetDef inheritance cycle", "[nodedef]")
{
    mx::DocumentPtr doc = mx::createDocument();

    SECTION("self cycle")
    {
        mx::readFromXmlString(
            doc,
            R"(<?xml version="1.0"?>
<materialx>
  <nodedef name="ND_x" node="foo"/>
  <targetdef name="T" inherit="T"/>
</materialx>
)");

        REQUIRE(doc->getTargetDef("T")->getMatchingTargets() == mx::StringVec{ "T" });
        REQUIRE(doc->getNodeDef("ND_x")->getImplementation("T") == nullptr);
    }

    SECTION("mutual cycle")
    {
        mx::readFromXmlString(
            doc,
            R"(<?xml version="1.0"?>
<materialx>
  <nodedef name="ND_x" node="foo"/>
  <targetdef name="T1" inherit="T2"/>
  <targetdef name="T2" inherit="T1"/>
</materialx>
)");

        REQUIRE(doc->getTargetDef("T1")->getMatchingTargets() == mx::StringVec{ "T1", "T2" });
        REQUIRE(doc->getNodeDef("ND_x")->getImplementation("T1") == nullptr);
    }
}
Lingua principale
C++
Stelle
2.3k
Fork
472
Merge medio
1g 4h
PR unite (30g)
7

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di AcademySoftwareFoundation/MaterialX

Tutte le issue di AcademySoftwareFoundation/MaterialX

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.