Skip to content

Commit 9151f50

Browse files
jeongseok-metameta-codesync[bot]
authored andcommitted
Fix Array Access Safety issues (#877)
Summary: Pull Request resolved: #877 Reviewed By: cstollmeta Differential Revision: D89006772 fbshipit-source-id: 8a922058260d74e6a27d9406808b23d9d6ddbbfa
1 parent ef781f3 commit 9151f50

File tree

10 files changed

+54
-4
lines changed

10 files changed

+54
-4
lines changed

momentum/character/character_utility.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ Character removeJoints(const Character& character, std::span<const size_t> joint
655655
if (srcToResultJointsWithParents[iSrcJoint] != kInvalidIndex) {
656656
break;
657657
}
658-
srcParent = srcSkeleton.joints[srcParent].parent;
658+
srcParent = srcSkeleton.joints.at(srcParent).parent;
659659
}
660660
}
661661

momentum/io/fbx/fbx_io.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,10 @@ std::vector<::fbxsdk::FbxNode*> createMarkerNodes(
361361

362362
// Add timestamp and position for this marker
363363
const auto& index = markerNameToIndex.at(marker.name);
364+
MT_THROW_IF(
365+
index >= timestamps.size() || index >= markerPositions.size(),
366+
"Marker index {} exceeds container size",
367+
index);
364368
timestamps[index].push_back(timestamp);
365369
markerPositions[index].push_back(marker.pos);
366370
}
@@ -435,6 +439,11 @@ std::vector<::fbxsdk::FbxNode*> createMarkerNodes(
435439
curveZ->KeyModifyBegin();
436440

437441
for (size_t k = 0; k < timestamps[j].size(); ++k) {
442+
MT_THROW_IF(
443+
j >= markerPositions.size() || k >= markerPositions[j].size(),
444+
"Marker position index out of bounds: j={}, k={}",
445+
j,
446+
k);
438447
::fbxsdk::FbxTime time;
439448
time.SetSecondDouble(timestamps[j][k]);
440449

momentum/io/fbx/openfbx_loader.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,7 @@ void parseSkinnedModel(
680680
const auto indexV = shape->getIndices();
681681
MT_CHECK(numIndices == numVertices);
682682
for (int v = 0; v < numVertices; ++v) {
683+
MT_THROW_IF(v >= numIndices, "Vertex index {} exceeds numIndices {}", v, numIndices);
683684
shapes(vertexOffset * 3 + indexV[v] * 3 + 0, currentShapes + shapeIndex) = shapeV[v].x;
684685
shapes(vertexOffset * 3 + indexV[v] * 3 + 1, currentShapes + shapeIndex) = shapeV[v].y;
685686
shapes(vertexOffset * 3 + indexV[v] * 3 + 2, currentShapes + shapeIndex) = shapeV[v].z;

momentum/io/gltf/gltf_builder.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ void addActorAnimationToModel(
322322

323323
// get index
324324
const auto& index = markerNameToIndex.at(marker.name);
325+
MT_THROW_IF(
326+
index >= timestamps.size() || index >= markerPositions.size(),
327+
"Marker index {} exceeds container size",
328+
index);
325329
timestamps[index].push_back(timestamp);
326330
markerPositions[index].push_back(fromMomentumVec3f(marker.pos.cast<float>()));
327331
}
@@ -384,6 +388,11 @@ size_t addMeshToModel(
384388
const auto nodeIdx = model.nodes.size();
385389
model.nodes.emplace_back();
386390
auto& node = model.nodes.back();
391+
MT_THROW_IF(
392+
rootNodeIdx >= model.nodes.size(),
393+
"Root node index {} exceeds model.nodes size {}",
394+
rootNodeIdx,
395+
model.nodes.size());
387396
model.nodes[rootNodeIdx].children.push_back(nodeIdx);
388397

389398
// create model mesh

momentum/io/gltf/gltf_io.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ addMesh(const fx::gltf::Document& model, const fx::gltf::Primitive& primitive, M
440440
MT_CHECK(fidxDense.size() % 3 == 0, "{} % 3 = {}", fidxDense.size(), fidxDense.size() % 3);
441441
texfaces.resize(fidxDense.size() / 3);
442442
if (!fidxDense.empty()) {
443+
MT_THROW_IF(texfaces.empty(), "texfaces is empty but fidxDense is not empty");
443444
std::copy_n(fidxDense.data(), fidxDense.size(), &texfaces[0][0]);
444445
}
445446
} else {

momentum/io/marker/trc_io.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "momentum/io/marker/trc_io.h"
99

10+
#include "momentum/common/exception.h"
1011
#include "momentum/common/string.h"
1112
#include "momentum/io/common/stream_utils.h"
1213
#include "momentum/io/marker/conversions.h"
@@ -76,6 +77,8 @@ MarkerSequence loadTrc(const std::string& filename, UpVector up) {
7677
// go over all markers
7778
for (size_t i = 0; i < numMarkers; i++) {
7879
const size_t pos = i * 3 + 2;
80+
MT_THROW_IF(
81+
i >= markerNames.size() || pos + 2 >= tokens.size(), "Marker index {} out of bounds", i);
7982
markers[i].name = markerNames[i];
8083
if (tokens[pos + 0].empty() || tokens[pos + 1].empty() || tokens[pos + 2].empty()) {
8184
markers[i].occluded = true;

pymomentum/renderer/momentum_render.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,10 @@ Eigen::Matrix<int, Eigen::Dynamic, 3, Eigen::RowMajor> triangulate(
383383
const size_t numTris = triangleIndices.size() / 3;
384384
Eigen::Matrix<int, Eigen::Dynamic, 3, Eigen::RowMajor> result(numTris, 3);
385385
for (size_t iTri = 0; iTri < numTris; iTri++) {
386-
result(iTri, 0) = triangleIndices[iTri * 3 + 0];
387-
result(iTri, 1) = triangleIndices[iTri * 3 + 1];
388-
result(iTri, 2) = triangleIndices[iTri * 3 + 2];
386+
const size_t baseIdx = iTri * 3;
387+
result(iTri, 0) = triangleIndices.at(baseIdx + 0);
388+
result(iTri, 1) = triangleIndices.at(baseIdx + 1);
389+
result(iTri, 2) = triangleIndices.at(baseIdx + 2);
389390
}
390391
return result;
391392
}

pymomentum/renderer/software_rasterizer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <momentum/character/linear_skinning.h>
1515
#include <momentum/character/skeleton.h>
1616
#include <momentum/character/skeleton_state.h>
17+
#include <momentum/common/exception.h>
1718
#include <momentum/math/fwd.h>
1819
#include <momentum/math/mesh.h>
1920

@@ -911,6 +912,11 @@ void rasterizeSkeleton(
911912

912913
size_t grandparent = character.skeleton.joints[iJoint].parent;
913914
while (grandparent != momentum::kInvalidIndex && !activeJoints[grandparent]) {
915+
MT_THROW_IF(
916+
grandparent >= character.skeleton.joints.size(),
917+
"Grandparent joint index {} exceeds skeleton size {}",
918+
grandparent,
919+
character.skeleton.joints.size());
914920
grandparent = character.skeleton.joints[grandparent].parent;
915921
}
916922

pymomentum/solver/momentum_ik.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,11 @@ variable_list IKProblemAutogradFunction<T, IKFunction>::forward(
473473
// cannot save a vector of custom enum type.
474474
std::vector<int64_t> activatedErrorTypes(activeErrorFunctions.size());
475475
for (size_t i = 0; i < activeErrorFunctions.size(); ++i) {
476+
MT_THROW_IF(
477+
i >= activatedErrorTypes.size(),
478+
"Index {} exceeds activatedErrorTypes size {}",
479+
i,
480+
activatedErrorTypes.size());
476481
activatedErrorTypes[i] = static_cast<int>(activeErrorFunctions[i]);
477482
}
478483
ctx->saved_data["activeErrorFunctions"] = activatedErrorTypes;

pymomentum/tensor_ik/tensor_ik_utility.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ std::vector<std::shared_ptr<momentum::SkeletonErrorFunctionT<T>>> buildMomentumE
163163
result.push_back(errf->createErrorFunction(character, iBatch, jFrame));
164164
// weightsMap maps error type order in the enum to order in input
165165
// errorFunctionWeights.
166+
MT_THROW_IF(
167+
iErr >= weightsMap.size(),
168+
"Error function index {} exceeds weightsMap size {}",
169+
iErr,
170+
weightsMap.size());
166171
T weight = weightsMap[iErr] < 0 ? T(0) : toEigenMap<T>(weights_cur)[weightsMap[iErr]];
167172
result.back()->setWeight(weight);
168173
}
@@ -207,6 +212,11 @@ std::unique_ptr<momentum::SequenceSolverFunctionT<T>> buildSequenceSolverFunctio
207212
auto errf_momentum = errf->createErrorFunction(character, iBatch, jFrame);
208213
// weightsMap maps error type order in the enum to order in input
209214
// errorFunctionWeights.
215+
MT_THROW_IF(
216+
iErr >= weightsMap.size(),
217+
"Error function index {} exceeds weightsMap size {}",
218+
iErr,
219+
weightsMap.size());
210220
T weight = weightsMap[iErr] < 0 ? T(0) : toEigenMap<T>(weights_cur)[weightsMap[iErr]];
211221
errf_momentum->setWeight(weight);
212222

@@ -238,6 +248,11 @@ std::vector<ErrorFunctionInput<T>> buildErrorFunctionInputs(
238248
continue;
239249
}
240250

251+
MT_THROW_IF(
252+
iErrorFunction >= weightsMap.size(),
253+
"Error function index {} exceeds weightsMap size {}",
254+
iErrorFunction,
255+
weightsMap.size());
241256
if (weightsMap[iErrorFunction] < 0) {
242257
continue;
243258
}

0 commit comments

Comments
 (0)