Bug: glTF2 exporter dereferences null for point-cloud meshes, faces without indices and unresolved bone names

Open
#7,059 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp

Research direction

Start with code/AssetLib/glTF2/glTF2Exporter.cpp at ExportSkin and ExportMeshes, then follow node mesh references through ExportNode and ExportScene. Reproduce the point-cloud, empty-face, and unresolved-bone cases with the supplied program under address and undefined behavior sanitizers. Done when all three export successfully or return an error without undefined behavior.

Written by the indexing model from the issue text.

Description

Fuzzer glTF2.0 Sanitizer

Describe the bug

Exporting to glTF 2 (gltf2/glb2) dereferences a null pointer for three kinds of scenes that the importers produce from small files (master eca3ce1):

  1. A mesh without faces (a point cloud, e.g. a PLY with only vertices). Since #5878, ExportMeshes skips meshes with mNumFaces == 0, but ExportNode/ExportScene still look up the glTF mesh by the aiScene mesh index (mAsset->meshes.Get(n->mMeshes[i]), glTF2Exporter.cpp:1553, 1606). With only a point cloud there is no glTF mesh at all and writing the node dereferences null (glTF2::Write(Node), glTF2AssetWriter.inl:749). With several meshes, every node after a skipped mesh refers to the wrong mesh.
  2. A first face without indices (a truncated PLY gives one face with 0 indices). The index array is then empty and ExportMeshes passes &indices[0] to ExportData (glTF2Exporter.cpp:1326, and the same code for 32-bit indices).
  3. A bone whose node cannot be found by name. ExportSkin looks the node up with mAsset.nodes.Get(aib->mName.C_Str()) (glTF2Exporter.cpp:1072), but nodes are registered under the id FindUniqueID made, which differs for empty or duplicate names. The empty Ref<Node> is then dereferenced (nodeRef->jointName, :1073). The ASE loader yields such a bone (empty name) for a file with only the header line.

To Reproduce

// glTF2 exporter crashes on scenes produced by the importers. Build with -fsanitize=address,undefined.
// Usage: ./repro points | empty-face | skeleton
#include <assimp/Exporter.hpp>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <cstdio>
#include <cstring>
#include <string>

// point cloud: 5 vertices, no faces
static const char *PLY_POINTS = "ply\nformat ascii 1.0\nelement vertex 5\nproperty float x\nproperty float y\nproperty float z\nend_header\n"
                                "0 0 0\n1 0 0\n0 1 0\n0 0 1\n1 1 1\n";
// truncated after the first number: the loader accepts it and yields one face with 0 indices
static const char *PLY_TRUNC = "ply\nformat ascii 1.0\nelement vertex 3\nproperty float x\nproperty float y\nproperty float z\n"
                               "element face 1\nproperty list uchar int vertex_indices\nend_header\n0 ";
// header only: the ASE loader builds a skeleton mesh whose bone is named after a node with an empty name
static const char *ASE_HEADER = "*3DSMAX_ASCIIEXPORT\t200\n";

int main(int argc, char **argv) {
    std::string m = argc > 1 ? argv[1] : "";
    const char *in = m == "points" ? PLY_POINTS : m == "empty-face" ? PLY_TRUNC : m == "skeleton" ? ASE_HEADER : nullptr;
    if (!in) { fprintf(stderr, "usage: %s points|empty-face|skeleton\n", argv[0]); return 2; }
    Assimp::Importer imp;
    const aiScene *s = imp.ReadFileFromMemory(in, strlen(in), 0, m == "skeleton" ? "ase" : "ply");
    if (!s) { printf("import failed: %s\n", imp.GetErrorString()); return 1; }
    Assimp::Exporter ex;
    const aiExportDataBlob *b = ex.ExportToBlob(s, "glb2");
    printf("export: %s\n", b ? "ok" : ex.GetErrorString());
    return 0;
}

With -fsanitize=address,undefined (clang 21, UBSAN_OPTIONS=halt_on_error=1):

$ ./repro points
stl_vector.h:1264:9: runtime error: reference binding to null pointer of type 'glTF2::Mesh *'
    #1 glTFCommon::Ref<glTF2::Mesh>::operator->() code/AssetLib/glTFCommon/glTFCommon.h:271:30
    #2 glTF2::Write(..., glTF2::Node&, glTF2::AssetWriter&) code/AssetLib/glTF2/glTF2AssetWriter.inl:749:35
$ ./repro empty-face
stl_vector.h:1264:9: runtime error: reference binding to null pointer of type 'unsigned short'
    #0 std::vector<unsigned short>::operator[](unsigned long)
    #1 Assimp::glTF2Exporter::ExportMeshes() code/AssetLib/glTF2/glTF2Exporter.cpp:1326:77
$ ./repro skeleton
code/AssetLib/glTFCommon/glTFCommon.h:271:30: runtime error: member call on null pointer of type 'std::vector<glTF2::Node *>'
    #0 glTFCommon::Ref<glTF2::Node>::operator->() code/AssetLib/glTFCommon/glTFCommon.h:271:30
    #1 ExportSkin(...) code/AssetLib/glTF2/glTF2Exporter.cpp
    #2 Assimp::glTF2Exporter::ExportMeshes() code/AssetLib/glTF2/glTF2Exporter.cpp:1368:13

Expected behavior

The export succeeds, or fails with an error from GetErrorString(), without undefined behavior.

Possible fix

For reference only (master eca3ce1). It does not skip faceless meshes, so the node indices stay aligned (such a mesh is written with only its vertices, as a POINTS primitive); it writes the index accessor only when there are indices; and ExportSkin fails the export when a bone's node is not found. Keeping the skip from #5878 and remapping the nodes' mesh indices instead would also fix case 1; I chose the smaller change.

--- a/code/AssetLib/glTF2/glTF2Exporter.cpp
+++ b/code/AssetLib/glTF2/glTF2Exporter.cpp
@@ -1070,6 +1070,11 @@
         // aib->mName   =====>  skinRef->jointNames
         // Find the node with id = mName.
         Ref<Node> nodeRef = mAsset.nodes.Get(aib->mName.C_Str());
+        // nodes are registered under the id FindUniqueID made (empty or duplicate names are rewritten), so a bone name may not resolve: fail the export instead of dereferencing an empty Ref
+        if (!nodeRef) {
+            delete[] jointsPerVertex;
+            throw DeadlyExportError("GLTF2: no node found for bone \"", aib->mName.C_Str(), "\"");
+        }
         nodeRef->jointName = nodeRef->name;
 
         unsigned int jointNamesIndex = 0;
@@ -1214,9 +1219,8 @@
 
     for (unsigned int idx_mesh = 0; idx_mesh < mScene->mNumMeshes; ++idx_mesh) {
         const aiMesh *aim = mScene->mMeshes[idx_mesh];
-        if (aim->mNumFaces == 0) {
-            continue;
-        }
+        // a mesh without faces (a point cloud) is exported too: the nodes refer to the glTF meshes by the aiScene mesh index
+        // (ExportNode), so skipping one shifts every later index; the primitive below writes indices only when there are faces
 
         std::string name = aim->mName.C_Str();
 
@@ -1323,8 +1327,11 @@
                     }
                 }
 
-                p.indices = ExportData(*mAsset, meshId, b, indices.size(), &indices[0], AttribType::SCALAR, AttribType::SCALAR,
-                        ComponentType_UNSIGNED_SHORT, BufferViewTarget_ELEMENT_ARRAY_BUFFER);
+                // faces without indices leave the index array empty: nothing to export, and no element to take the address of
+                if (!indices.empty()) {
+                    p.indices = ExportData(*mAsset, meshId, b, indices.size(), &indices[0], AttribType::SCALAR, AttribType::SCALAR,
+                            ComponentType_UNSIGNED_SHORT, BufferViewTarget_ELEMENT_ARRAY_BUFFER);
+                }
             } else {
                 std::vector<IndicesType> indices;
                 indices.resize(nIndices);
@@ -1334,8 +1341,11 @@
                     }
                 }
 
-                p.indices = ExportData(*mAsset, meshId, b, indices.size(), &indices[0], AttribType::SCALAR, AttribType::SCALAR,
-                        ComponentType_UNSIGNED_INT, BufferViewTarget_ELEMENT_ARRAY_BUFFER);
+                // faces without indices leave the index array empty: nothing to export, and no element to take the address of
+                if (!indices.empty()) {
+                    p.indices = ExportData(*mAsset, meshId, b, indices.size(), &indices[0], AttribType::SCALAR, AttribType::SCALAR,
+                            ComponentType_UNSIGNED_INT, BufferViewTarget_ELEMENT_ARRAY_BUFFER);
+                }
             }
         }
 

With it points and empty-face export successfully and skeleton fails with GLTF2: no node found for bone ""; no sanitizer reports.

Platform

  • OS: Linux x86_64, clang 21, -fsanitize=address,undefined
  • Version: master eca3ce1 (also v6.0.5)

Additional context

#6899 (OSS-Fuzz, null-dereference READ in glTF2Exporter::ExportMeshes) may be case 2 or 3; its test case is not visible to me.

Assisted-by: Claude Code

Dominant language
C++
Stars
13.3k
Forks
3.3k
Avg merge
3d 5h
Merged PRs (30d)
12

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from assimp/assimp

All issues in assimp/assimp

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.