Skip to content

Commit 8a385c8

Browse files
[vt, sdf, usd] fix array-bounds warnings on GCC 13
Five false-positive warning sites caused by GCC 13 losing bound information during aggressive inlining. pxr/base/vt/streamOut.cpp Change dimension == shape.GetRank() - 1 to dimension + 1 >= shape.GetRank() in _StreamArrayRecursive. GCC can see GetRank() returns at most 4 and proves dimension <= 2 < NumOtherDims in the else branch. pxr/usd/sdf/schema.cpp Add static_cast<size_t>(level) < std::size(valueTupleDimensions.d) to the else-if in _AddValuesToValueContext. GCC cannot bound the plain size_t size member but can see std::size(d) == 2. pxr/usd/usd/clip.cpp Introduce bracketingTimesEnd as begin + std::min(numTimes, size()) in GetBracketingTimeSamplesForPath. Restores the upper bound lost after std::distance, preventing std::sort's _S_threshold==16 path from appearing reachable on the 5-element array. pxr/usd/sdf/predicateLibrary.h Replace std::vector<bool> with std::vector<uint8_t> for boundArgs. The vector<bool> bit-packing specialisation triggers -Warray-bounds and -Wstringop-overflow when its word-level memmove is inlined into _TryBindArgs; uint8_t uses the standard path GCC analyses correctly.
1 parent 15a2bff commit 8a385c8

4 files changed

Lines changed: 15 additions & 12 deletions

File tree

pxr/base/vt/streamOut.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ _StreamArrayRecursive(
7777
size_t dimension)
7878
{
7979
out << '[';
80-
if (dimension == shape.GetRank() - 1) {
80+
if (dimension + 1 >= shape.GetRank()) {
8181
for (size_t j = 0; j < lastDimSize; ++j) {
8282
if (j) { out << ", "; }
8383
streamNextElem(out);

pxr/usd/sdf/predicateLibrary.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ class SdfPredicateLibrary
459459
ParamType &param,
460460
bool &boundAllParams,
461461
std::vector<SdfPredicateExpression::FnArg> const &args,
462-
std::vector<bool> &boundArgs,
462+
std::vector<uint8_t> &boundArgs,
463463
NamesAndDefaults const &namesAndDefaults) {
464464

465465
// Bind the index-th 'param' from 'args' &
@@ -496,7 +496,7 @@ class SdfPredicateLibrary
496496
VtValue cast = VtValue::Cast<ParamType>(val);
497497
if (!cast.IsEmpty()) {
498498
param = cast.UncheckedRemove<ParamType>();
499-
boundArgs[argIndex] = true;
499+
boundArgs[argIndex] = 1;
500500
return true;
501501
}
502502
boundAllParams = false;
@@ -550,12 +550,12 @@ class SdfPredicateLibrary
550550
std::vector<SdfPredicateExpression::FnArg> const &args,
551551
NamesAndDefaults const &namesAndDefaults,
552552
std::index_sequence<I...>,
553-
std::vector<bool> &boundArgs) {
553+
std::vector<uint8_t> &boundArgs) {
554554

555555
// A fold expression would let us just do &&, but that's '17, so we just
556556
// do all of them and set a bool.
557557
bool bound = true;
558-
boundArgs.assign(args.size(), false);
558+
boundArgs.assign(args.size(), 0);
559559
// Need a unused array so we can use an initializer list to invoke
560560
// _TryBindOne N times.
561561
int unused[] = {
@@ -572,7 +572,7 @@ class SdfPredicateLibrary
572572
static void
573573
_FillArbitraryArgs(std::true_type,
574574
std::vector<SdfPredicateExpression::FnArg> const &args,
575-
std::vector<bool> const &boundArgs,
575+
std::vector<uint8_t> const &boundArgs,
576576
Tuple &typedArgs) {
577577
std::vector<SdfPredicateExpression::FnArg> &rest =
578578
std::get<std::tuple_size<Tuple>::value-1>(typedArgs);
@@ -591,7 +591,7 @@ class SdfPredicateLibrary
591591
static void
592592
_FillArbitraryArgs(std::false_type,
593593
std::vector<SdfPredicateExpression::FnArg> const &,
594-
std::vector<bool> const &,
594+
std::vector<uint8_t> const &,
595595
T const &) {
596596
// Do nothing.
597597
}
@@ -657,7 +657,7 @@ class SdfPredicateLibrary
657657
}
658658

659659
ParamsTuple typedArgs;
660-
std::vector<bool> boundArgs;
660+
std::vector<uint8_t> boundArgs;
661661
if (_TryBindArgs(typedArgs, args, namesAndDefaults,
662662
std::make_index_sequence<NumBindableArgs> {},
663663
boundArgs)) {

pxr/usd/sdf/schema.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "pxr/base/vt/dictionary.h"
3232

3333
#include <deque>
34+
#include <iterator>
3435
#include <map>
3536
#include <set>
3637
#include <vector>
@@ -1538,7 +1539,8 @@ _AddValuesToValueContext(std::deque<Value> *values, Sdf_ParserValueContext *cont
15381539
context->AppendValue(values->front());
15391540
values->pop_front();
15401541
}
1541-
} else if (static_cast<size_t>(level) < context->valueTupleDimensions.size) {
1542+
} else if (static_cast<size_t>(level) < context->valueTupleDimensions.size &&
1543+
static_cast<size_t>(level) < std::size(context->valueTupleDimensions.d)) {
15421544
context->BeginTuple();
15431545
for (size_t i = 0; i < context->valueTupleDimensions.d[level]; i++) {
15441546
_AddValuesToValueContext(values, context, level + 1);

pxr/usd/usd/clip.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,10 @@ Usd_Clip::GetBracketingTimeSamplesForPath(
430430
return true;
431431
}
432432

433-
std::sort(bracketingTimes.begin(), bracketingTimes.begin() + numTimes);
434-
auto uniqueIt = std::unique(
435-
bracketingTimes.begin(), bracketingTimes.begin() + numTimes);
433+
const auto bracketingTimesEnd =
434+
bracketingTimes.begin() + std::min(numTimes, bracketingTimes.size());
435+
std::sort(bracketingTimes.begin(), bracketingTimesEnd);
436+
auto uniqueIt = std::unique(bracketingTimes.begin(), bracketingTimesEnd);
436437
return _GetBracketingTimeSamples(
437438
bracketingTimes.begin(), uniqueIt, time, tLower, tUpper);
438439
}

0 commit comments

Comments
 (0)