Struct value with an empty last member ("{hello;}") is not parsed back: parseStructValueString drops the trailing empty token

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

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

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

Direzione di ricerca

La logica di suddivisione è in source/MaterialXCore/Value.cpp in parseStructValueString(); inizia dall'append finale protetto da !part.empty() intorno alle righe 223-224 e confrontalo con il modo in cui le parti vuote precedenti vengono aggiunte dopo un separatore. Riproduci con lo snippet dell'issue, poi verifica che Value::createValueFromStrings() accetti l'output del writer "{hello;}" e che {} e i casi con un singolo membro restino corretti. Fatto significa che parseStructValueString("{hello;}") produce due token e che il round trip restituisce "hello" e "" senza lanciare.

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

Descrizione

What happens

parseStructValueString("{hello;}") returns 1 token, while "{;world}" returns 2 and "{a;;c}" returns 3. An empty member is kept in every position except the last. As a result, a two-member struct whose last member is an empty string cannot be read back. AggregateValue::getValueString() writes {"hello", ""} as "{hello;}", and Value::createValueFromStrings() with the same typedef then throws Wrong number of initializers - expect 2.

Related: #3138 reports that this throw escapes ValueElement::getValue() and Document::validate() for any malformed struct value. That is a separate defect with its own fix; this one is about a well-formed value being split wrongly.

What should happen

The specification describes a struct value as "a brace surrounded, semicolon-separated list of numbers and strings" whose parts line up with the typedef members in order (MaterialX.Specification.md#L277). The library's own writer produces "{hello;}" for these two members (Value.cpp#L316-L331), so the reader should accept it and return two members, "hello" and "". At minimum, Value::createValueFromStrings() should not throw: it is documented to return "an empty shared pointer if the conversion to the given data type cannot be performed" (Value.h#L74-L77).

Steps to reproduce

#include <MaterialXCore/Document.h>
#include <MaterialXCore/Value.h>

#include <cstdio>
#include <string>

using namespace MaterialX;

int main()
{
    int failures = 0;

    // 1. Tokenizer
    StringVec t1 = parseStructValueString("{hello;}");
    StringVec t2 = parseStructValueString("{;world}");
    StringVec t3 = parseStructValueString("{a;;c}");
    printf("parseStructValueString(\"{hello;}\") -> %zu tokens (expected 2)\n", t1.size());
    printf("parseStructValueString(\"{;world}\") -> %zu tokens (expected 2)\n", t2.size());
    printf("parseStructValueString(\"{a;;c}\")   -> %zu tokens (expected 3)\n", t3.size());
    if (t1.size() != 2)
    {
        printf("FAIL: trailing empty member dropped: got %zu tokens, expected 2\n", t1.size());
        failures++;
    }

    // 2. Round trip through the library's own writer
    DocumentPtr doc = createDocument();
    TypeDefPtr typeDef = doc->addTypeDef("pairtype");
    MemberPtr ma = typeDef->addMember("a");
    ma->setType("string");
    ma->setValueString("hello");
    MemberPtr mb = typeDef->addMember("b");
    mb->setType("string");
    mb->setValueString("world");

    AggregateValuePtr av = AggregateValue::createAggregateValue("pairtype");
    av->appendValue(Value::createValue(std::string("hello")));
    av->appendValue(Value::createValue(std::string("")));
    std::string written = av->getValueString();
    printf("AggregateValue{\"hello\",\"\"}.getValueString() -> \"%s\"\n", written.c_str());
    try
    {
        ValuePtr back = Value::createValueFromStrings(written, "pairtype", typeDef);
        if (!back)
            printf("FAIL: round trip through the library's own writer returned null\n");
        else
            printf("round trip ok -> %s\n", back->getValueString().c_str());
    }
    catch (const std::exception& e)
    {
        printf("FAIL: parsing the library's own output \"%s\" threw: %s\n", written.c_str(), e.what());
        failures++;
    }

    return failures ? 1 : 0;
}

Observed output

parseStructValueString("{hello;}") -> 1 tokens (expected 2)
parseStructValueString("{;world}") -> 2 tokens (expected 2)
parseStructValueString("{a;;c}")   -> 3 tokens (expected 3)
FAIL: trailing empty member dropped: got 1 tokens, expected 2
AggregateValue{"hello",""}.getValueString() -> "{hello;}"
FAIL: parsing the library's own output "{hello;}" threw: Wrong number of initializers - expect 2

Expected output

parseStructValueString("{hello;}") -> 2 tokens (expected 2)
parseStructValueString("{;world}") -> 2 tokens (expected 2)
parseStructValueString("{a;;c}")   -> 3 tokens (expected 3)
AggregateValue{"hello",""}.getValueString() -> "{hello;}"
round trip ok -> {hello;}

Possible cause (suggestion)

At the end of parseStructValueString(), the last accumulated part is appended only if (!part.empty()) (Value.cpp#L223-L224). Parts before a separator are always appended, empty or not. Appending the last part whenever the braces held at least one separator would make the three cases consistent. The exception itself comes from AggregateValue::createAggregateValueFromString() (Value.cpp#L361-L373). That throw is the same one reached for any malformed struct value.

Environment

  • Links point at commit 9176de82cd993bace533b734e17f9ca738061714 (MaterialX 1.39.6). parseStructValueString() and createAggregateValueFromString() are unchanged at main 52c258d91be9 (21 Nov 2026).
  • MaterialX 1.39.6, commit 52c258d91be910c1ed4b6c971a160e80b357bbe7 (main, read on 23 Nov 2026).
  • Ubuntu 24.04.5 LTS (WSL2), x86_64, GCC 13.3.0, CMake 3.28.3 with Ninja, static libraries.
  • Debug configuration with the project's own MATERIALX_DYNAMIC_ANALYSIS=ON (-fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all, the program built with the same flags), which reported nothing for this program. Python, viewer, render, tests and the GLSL/OSL/MDL/MSL/Slang generators were off.
  • The same behaviour was first seen at 9176de82cd993bace533b734e17f9ca738061714 (Windows 11 x64, MSVC 19.42, Debug).

Found by VaultLens, an analysis tool that uses AI to catch bugs. If anything is wrong, please let us know.

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.