Malformed struct value string makes ValueElement::getValue() and Document::validate() throw instead of returning null / false
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
- 72/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- cpp
- Ambito
- computer-graphics
Direzione di ricerca
Riproduci con il piccolo programma del report, poi leggi source/MaterialXCore/Value.cpp: createValueFromStrings() restituisce direttamente AggregateValue::createAggregateValueFromString(), che lancia un'eccezione quando il numero di token non corrisponde al numero di membri. Confronta con il percorso scalare (TypedValue::createFromString) che intercetta l'errore di tipo e restituisce un puntatore vuoto, e decidi se l'intercettazione debba stare in createValueFromStrings() o nel parser degli aggregati. Fatto significa che getValue() restituisce null e Document::validate() restituisce false con un messaggio "Invalid value" invece di lanciare un'eccezione, secondo Element.cpp con la sua chiamata a validateRequire.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
What happens
For an element whose type is a struct typedef with members, a value string that is not a valid struct literal (here value="5" for a one-member struct) makes ValueElement::getValue() throw Exception("Wrong number of initializers - expect 1"). Document::validate() calls getValue() without catching, so the exception escapes validate() instead of the document being reported invalid.
What should happen
Value::createValueFromStrings(), which ValueElement::getValue() calls, is documented to return an empty pointer when the conversion fails (Value.h#L74-L77):
@return A shared pointer to a typed value, or an empty shared pointer if the conversion to the given data type cannot be performed.
ValueElement::validate() depends on that behaviour to report the error (Element.cpp#L814):
validateRequire(getValue() != nullptr, res, message, "Invalid value");
The scalar path behaves this way: TypedValue<T>::createFromString catches ExceptionTypeError and returns an empty pointer. The struct path does not. Neither getValue() nor createValueFromStrings() documents a @throws.
Related: a separate defect in parseStructValueString() (a trailing empty member is dropped) reaches the same throw for a well-formed value; it is reported in its own issue. The two have different causes and fixes.
Steps to reproduce
#include <MaterialXCore/Document.h>
#include <MaterialXFormat/XmlIo.h>
#include <fstream>
#include <iostream>
namespace mx = MaterialX;
int main()
{
const std::string xml =
"<?xml version=\"1.0\"?>\n"
"<materialx version=\"1.39\">\n"
" <typedef name=\"mystruct\">\n"
" <member name=\"x\" type=\"float\"/>\n"
" </typedef>\n"
" <input name=\"in1\" type=\"mystruct\" value=\"5\"/>\n"
"</materialx>\n";
const std::string path = "badstruct.mtlx";
{
std::ofstream f(path);
f << xml;
}
mx::DocumentPtr doc = mx::createDocument();
try
{
mx::readFromXmlFile(doc, path);
}
catch (const std::exception& e)
{
std::cout << "readFromXmlFile threw (unexpected): " << e.what() << "\n";
return 5;
}
mx::TypeDefPtr typeDef = doc->getTypeDef("mystruct");
std::cout << "typedef found: " << (typeDef ? "yes" : "no")
<< ", members: " << (typeDef ? typeDef->getMembers().size() : 0) << "\n";
mx::InputPtr input;
for (mx::InputPtr in : doc->getChildrenOfType<mx::Input>())
{
if (in->getName() == "in1")
input = in;
}
std::cout << "input found: " << (input ? "yes" : "no") << "\n";
if (!input || !typeDef)
return 6;
try
{
mx::ValuePtr v = input->getValue();
std::cout << "getValue returned: " << (v ? "non-null" : "null") << "\n";
}
catch (const std::exception& e)
{
std::cout << "FAIL getValue threw instead of returning null: " << e.what() << "\n";
}
try
{
std::string msg;
bool ok = doc->validate(&msg);
std::cout << "validate returned: " << (ok ? "true" : "false")
<< ", message: " << msg << "\n";
if (ok)
{
std::cout << "FAIL validate returned true for invalid value\n";
return 4;
}
return 0;
}
catch (const std::exception& e)
{
std::cout << "FAIL Document::validate threw: " << e.what() << "\n";
return 2;
}
}
Observed output
typedef found: yes, members: 1
input found: yes
FAIL getValue threw instead of returning null: Wrong number of initializers - expect 1
FAIL Document::validate threw: Wrong number of initializers - expect 1
Expected output
typedef found: yes, members: 1
input found: yes
getValue returned: null
validate returned: false, message: <... Invalid value ...>
Possible cause (suggestion)
Value::createValueFromStrings() returns AggregateValue::createAggregateValueFromString() directly (Value.cpp#L278). That function throws a plain Exception when the token count does not match the member count (Value.cpp#L361-L373). A value without braces always yields zero tokens. Catching that case, or returning an empty pointer there, would match the documented contract.
Environment
- Links point at commit 9176de82cd993bace533b734e17f9ca738061714 (MaterialX 1.39.6). The code involved is unchanged at
main52c258d91be9 (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
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di AcademySoftwareFoundation/MaterialX
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
AcademySoftwareFoundation/MaterialX#3140 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
AcademySoftwareFoundation/MaterialX#3139 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
AcademySoftwareFoundation/MaterialX#3137 ·
I maintainer di solito rispondono entro 2 giorni
-
Cyclic 'inherit' on TargetDefs causes an infinite loop and unbounded memory growthForse già presa @shubhamrai9122-creator l’ha presa 33 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
AcademySoftwareFoundation/MaterialX#3096 ·
I maintainer di solito rispondono entro 2 giorni
-
glTF Vector3 image definition variant does not include a scale factorForse già presa @kwokcb l’ha presa 94 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
AcademySoftwareFoundation/MaterialX#3054 ·
I maintainer di solito rispondono entro 2 giorni
Tutte le issue di AcademySoftwareFoundation/MaterialX
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
mw00/project-maya#1368 ·
I maintainer di solito rispondono entro 2 giorni
-
needs triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
[LLK][QSR][PERF] eltwise_binary: perf harness INIT enables direct indexing, production does notAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
tenstorrent/tt-metal#67662 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
maplibre/maplibre-native#5159 ·
I maintainer di solito rispondono entro 1 giorno
-
enhancement question
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
lbr-stack/lbr_fri_ros2_stack#420 · 1 commento ·