Skip to content

Commit

Permalink
Merge branch 'main' into AcademySoftwareFoundation#342-Python-script-…
Browse files Browse the repository at this point in the history
…fixes
  • Loading branch information
jstone-lucasfilm committed Nov 27, 2023
2 parents 4a6e2b1 + e45d67f commit 5207806
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
2 changes: 1 addition & 1 deletion source/MaterialXCore/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const std::tuple<int, int, int> LIBRARY_VERSION_TUPLE(MATERIALX_MAJOR_VERSION,

bool invalidNameChar(char c)
{
return !isalnum(c) && c != '_' && c != ':';
return !isalnum((unsigned char) c) && c != '_' && c != ':';
}

} // anonymous namespace
Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXFormat/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const string MATERIALX_SEARCH_PATH_ENV_VAR = "MATERIALX_SEARCH_PATH";

inline bool hasWindowsDriveSpecifier(const string& val)
{
return (val.length() > 1 && std::isalpha(val[0]) && (val[1] == ':'));
return (val.length() > 1 && std::isalpha((unsigned char) val[0]) && (val[1] == ':'));
}

//
Expand Down
46 changes: 45 additions & 1 deletion source/MaterialXTest/MaterialXFormat/XmlIo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <MaterialXTest/External/Catch/catch.hpp>

#include <MaterialXFormat/Environ.h>
#include <MaterialXFormat/File.h>
#include <MaterialXFormat/Util.h>
#include <MaterialXFormat/XmlIo.h>

Expand Down Expand Up @@ -262,6 +261,51 @@ TEST_CASE("Comments and newlines", "[xmlio]")
REQUIRE(origXml == newXml);
}

TEST_CASE("Fuzz testing", "[xmlio]")
{
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
mx::FilePath examplesPath = searchPath.find("resources/Materials/Examples/StandardSurface");

std::mt19937 rng(0);
std::uniform_int_distribution<size_t> randChar(0, 255);

for (const mx::FilePath& filename : examplesPath.getFilesInDirectory(mx::MTLX_EXTENSION))
{
// Read the example file into an XML string buffer.
const std::string origString = mx::readFile(examplesPath / filename);
REQUIRE(origString.size() > 0);
std::uniform_int_distribution<size_t> randPos(0, origString.size() - 1);

// Iterate over test runs.
for (size_t testRun = 0; testRun < 256; testRun++)
{
std::string editString = origString;

// Iterate over string edits.
for (size_t editIndex = 0; editIndex < 32; editIndex++)
{
// Randomly alter one character in the document.
size_t charIndex = randPos(rng);
size_t newChar = randChar(rng);
editString[charIndex] = (char) newChar;

// Attempt to interpret the edited string as a document, allowing only MaterialX exceptions.
mx::DocumentPtr doc = mx::createDocument();
try
{
mx::readFromXmlString(doc, editString, searchPath);
doc->validate();
}
catch (const mx::Exception&)
{
// On a MaterialX exception, proceed to the next test run.
break;
}
}
}
}
}

TEST_CASE("Locale region testing", "[xmlio]")
{
// In the United States, the thousands separator is a comma, while in Germany it is a period.
Expand Down
5 changes: 2 additions & 3 deletions source/MaterialXTest/MaterialXGenShader/GenShaderUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,8 @@ void shaderGenPerformanceTest(mx::GenContext& context)
REQUIRE(loadedDocuments.size() == documentsPaths.size());

// Shuffle the order of documents and perform document library import validatation and shadergen
std::random_device random_dev;
std::mt19937 generator(random_dev());
std::shuffle(loadedDocuments.begin(), loadedDocuments.end(), generator);
std::mt19937 rng(0);
std::shuffle(loadedDocuments.begin(), loadedDocuments.end(), rng);
for (const auto& doc : loadedDocuments)
{
doc->importLibrary(nodeLibrary);
Expand Down

0 comments on commit 5207806

Please sign in to comment.