Skip to content

crash on undefined pbrMetallicRoughness #1433

@snoyer

Description

@snoyer

VTK may1 crash when loading a glTF file containing a material that does not define the optional pbrMetallicRoughness property, see attached samples: material-pbrMetallicRoughness.zip.

VTK can however handle a material's pbrMetallicRoughness being empty; so accessing the property with an empty default value instead of the current unchecked access should be enough of a fix

diff --git a/IO/Geometry/vtkGLTFDocumentLoaderInternals.cxx b/IO/Geometry/vtkGLTFDocumentLoaderInternals.cxx
index 713e9d966c..dab4af87b9 100644
--- a/IO/Geometry/vtkGLTFDocumentLoaderInternals.cxx
+++ b/IO/Geometry/vtkGLTFDocumentLoaderInternals.cxx
@@ -657,7 +657,7 @@ bool vtkGLTFDocumentLoaderInternals::LoadMaterial(
   double metallicFactor = 1;
   double roughnessFactor = 1;
 
-  const auto& pbrRoot = root["pbrMetallicRoughness"];
+  const auto& pbrRoot = root.value("pbrMetallicRoughness", R"({})"_json);
   if (!pbrRoot.empty())
   {
     if (vtkGLTFUtils::GetDoubleValue(pbrRoot, "metallicFactor", metallicFactor))

Footnotes

  1. it might also work in some cases (eg. different builds of F3D)? but, as per nlohmann/json's documentation, "It is undefined behavior to access a const object with a non-existing key"

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions