Skip to content

Commit

Permalink
Coding - Fix draco buffer index out of bounds
Browse files Browse the repository at this point in the history
Fixed an exception where the bufferIndex is out of bounds.
Happens when Draco compression is combined with SetMergeFaces.
  • Loading branch information
elias-bananaz authored Jan 23, 2025
1 parent 87a64bb commit bb84ecf
Showing 1 changed file with 40 additions and 44 deletions.
84 changes: 40 additions & 44 deletions src/RWGltf/RWGltf_CafWriter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -746,27 +746,27 @@ bool RWGltf_CafWriter::writeBinData (const Handle(TDocStd_Document)& theDocument
continue;
}

std::shared_ptr<RWGltf_CafWriter::Mesh> aMeshPtr;
#ifdef HAVE_DRACO
++aMeshIndex;
if (myDracoParameters.DracoCompression)
for (RWGltf_GltfFaceList::Iterator aGltfFaceIter (*aGltfFaceList); aGltfFaceIter.More() && aPSentryBin.More(); aGltfFaceIter.Next())
{
if (aMeshIndex <= aMeshes.size())
{
aMeshPtr = aMeshes.at(aMeshIndex - 1);
}
else
const Handle(RWGltf_GltfFace)& aGltfFace = aGltfFaceIter.Value();

std::shared_ptr<RWGltf_CafWriter::Mesh> aMeshPtr;
#ifdef HAVE_DRACO
++aMeshIndex;
if (myDracoParameters.DracoCompression)
{
aMeshes.push_back(std::make_shared<RWGltf_CafWriter::Mesh>(RWGltf_CafWriter::Mesh()));
aMeshPtr = aMeshes.back();
if (aMeshIndex <= aMeshes.size())
{
aMeshPtr = aMeshes.at(aMeshIndex - 1);
}
else
{
aMeshes.push_back(std::make_shared<RWGltf_CafWriter::Mesh>(RWGltf_CafWriter::Mesh()));
aMeshPtr = aMeshes.back();
}
}
}
#endif

for (RWGltf_GltfFaceList::Iterator aGltfFaceIter (*aGltfFaceList); aGltfFaceIter.More() && aPSentryBin.More(); aGltfFaceIter.Next())
{
const Handle(RWGltf_GltfFace)& aGltfFace = aGltfFaceIter.Value();

Handle(RWGltf_GltfFace) anOldGltfFace;
if (aWrittenPrimData.Find (aGltfFace->Shape, anOldGltfFace))
{
Expand Down Expand Up @@ -1899,23 +1899,21 @@ void RWGltf_CafWriter::writeMeshes (const RWGltf_GltfSceneNodeMap& theSceneNodeM
for (RWGltf_GltfFaceList::Iterator aFaceGroupIter (*aGltfFaceList); aFaceGroupIter.More(); aFaceGroupIter.Next())
{
const Handle(RWGltf_GltfFace)& aGltfFace = aFaceGroupIter.Value();
const int aPrevSize = aDracoBufIndMap.Size();
const int aTempDracoBufInd = aDracoBufInd;
if (myDracoParameters.DracoCompression
&& !aDracoBufIndMap.FindFromKey (aGltfFace->NodePos.Id, aDracoBufInd))
{
aDracoBufIndMap.Add (aGltfFace->NodePos.Id, aDracoBufInd);
}
int aCurrentDracoBufInd = 0;

writePrimArray (*aGltfFace, aNodeName, aDracoBufInd, toStartPrims);
if (aTempDracoBufInd != aDracoBufInd)
{
aDracoBufInd = aTempDracoBufInd;
}
if (!myDracoParameters.DracoCompression || aDracoBufIndMap.Size() > aPrevSize)
if (myDracoParameters.DracoCompression)
{
++aDracoBufInd;
// Check if we've seen this NodePos.Id before
if (!aDracoBufIndMap.FindFromKey(aGltfFace->NodePos.Id, aCurrentDracoBufInd))
{
// New Draco buffer entry needed
aCurrentDracoBufInd = aDracoBufInd;
aDracoBufIndMap.Add(aGltfFace->NodePos.Id, aCurrentDracoBufInd);
++aDracoBufInd;
}
}

writePrimArray(*aGltfFace, aNodeName, aCurrentDracoBufInd, toStartPrims);
}
}
else
Expand All @@ -1935,23 +1933,21 @@ void RWGltf_CafWriter::writeMeshes (const RWGltf_GltfSceneNodeMap& theSceneNodeM
}

const Handle(RWGltf_GltfFace)& aGltfFace = aGltfFaceList->First();
const int aPrevSize = aDracoBufIndMap.Size();
const int aTempDracoBufInd = aDracoBufInd;
if (myDracoParameters.DracoCompression
&& !aDracoBufIndMap.FindFromKey(aGltfFace->NodePos.Id, aDracoBufInd))
{
aDracoBufIndMap.Add(aGltfFace->NodePos.Id, aDracoBufInd);
}
int aCurrentDracoBufInd = 0;

writePrimArray (*aGltfFace, aNodeName, aDracoBufInd, toStartPrims);
if (aTempDracoBufInd != aDracoBufInd)
{
aDracoBufInd = aTempDracoBufInd;
}
if (!myDracoParameters.DracoCompression || aDracoBufIndMap.Size() > aPrevSize)
if (myDracoParameters.DracoCompression)
{
++aDracoBufInd;
// Check if we've seen this NodePos.Id before
if (!aDracoBufIndMap.FindFromKey(aGltfFace->NodePos.Id, aCurrentDracoBufInd))
{
// New Draco buffer entry needed
aCurrentDracoBufInd = aDracoBufInd;
aDracoBufIndMap.Add(aGltfFace->NodePos.Id, aCurrentDracoBufInd);
++aDracoBufInd;
}
}

writePrimArray(*aGltfFace, aNodeName, aCurrentDracoBufInd, toStartPrims);
}
}

Expand Down

0 comments on commit bb84ecf

Please sign in to comment.