Skip to content

Commit

Permalink
Don't crash in UsdImagingInstanceAdapter::GetScenePrimPaths when pass…
Browse files Browse the repository at this point in the history
…ing invalid instanceIndices
  • Loading branch information
mtavenrath committed Jul 6, 2022
1 parent e097b79 commit 63b2fb8
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions pxr/usdImaging/usdImaging/instanceAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2538,31 +2538,46 @@ UsdImagingInstanceAdapter::GetScenePrimPaths(
if (!instanceIndices.empty()) {
int minIdx = std::numeric_limits<int>::max();
int maxIdx = 0;
int validIndices = 0;

// determine the min/max index to determine how many bits have to be
// allocated in the requestIndicesMap.
for (size_t i = 0; i < instanceIndices.size(); i++) {
int remappedIndex = indices[instanceIndices[i]];
minIdx = std::min(minIdx, remappedIndex);
maxIdx = std::max(maxIdx, remappedIndex);
int instanceIndex = instanceIndices[i];

// skip invalid indices
if (instanceIndex < indices.size()) {
int remappedIndex = indices[instanceIndex];
minIdx = std::min(minIdx, remappedIndex);
maxIdx = std::max(maxIdx, remappedIndex);
++validIndices;
}
}

// For each requested index provide a mapping into the result vector
// Indices in the map set to std::numeric_limits<int>::max()
// are not being requested.
std::vector<int> requestedIndicesMap(
maxIdx - minIdx + 1, std::numeric_limits<int>::max());
// at least one index was valid, get the prim paths
if (validIndices > 0) {
// For each valid requested index provide a mapping into the result vector
// Indices in the map set to std::numeric_limits<int>::max()
// are not being requested.
std::vector<int> requestedIndicesMap(
maxIdx - minIdx + 1, std::numeric_limits<int>::max());

// set bits for all valid requested indices to true
for (size_t i = 0; i < instanceIndices.size(); i++) {
int instanceIndex = instanceIndices[i];

// skip invalid indices
if (instanceIndex < indices.size()) {
int remappedIndex = indices[instanceIndex];
requestedIndicesMap[remappedIndex - minIdx] = i;
}
}

// set bits for all requested indices to true
for (size_t i = 0; i < instanceIndices.size(); i++) {
int remappedIndex = indices[instanceIndices[i]];
requestedIndicesMap[remappedIndex - minIdx] = i;
result.resize(validIndices);
_GetScenePrimPathsFn primPathsFn(
this, requestedIndicesMap, minIdx, result, proto.path);
_RunForAllInstancesToDraw(instancerPrim, &primPathsFn);
}

result.resize(instanceIndices.size());
_GetScenePrimPathsFn primPathsFn(
this, requestedIndicesMap, minIdx, result, proto.path);
_RunForAllInstancesToDraw(instancerPrim, &primPathsFn);
}
return result;
} else {
Expand Down

0 comments on commit 63b2fb8

Please sign in to comment.