Skip to content

Commit d5aa3ff

Browse files
authored
[NFC] Fix asan error in test case (microsoft#4668)
This test relies on a stack allocation remaining live after the scope of an if block that it is allocated in... Clearly no good.
1 parent 81ef064 commit d5aa3ff

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

tools/clang/unittests/HLSL/AllocatorTest.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ bool Align(unsigned &pos, unsigned end, unsigned align) {
102102
}
103103

104104
struct Element {
105+
Element() = default;
106+
Element(const Element&) = default;
105107
Element(unsigned id, unsigned start, unsigned end) : id(id), start(start), end(end) {}
106108
bool operator<(const Element &other) { return id < other.id; }
107109
unsigned id; // index in original ordered vector
@@ -663,7 +665,7 @@ TEST_F(AllocatorTest, GapFilling) {
663665

664666
TEST_F(AllocatorTest, Allocate) {
665667
WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
666-
for (auto &&scenario : m_Scenarios) {
668+
for (auto &scenario : m_Scenarios) {
667669

668670
// Test for alignment 1 (no alignment), then alignment 4
669671
unsigned alignment = 1;
@@ -678,11 +680,14 @@ TEST_F(AllocatorTest, Allocate) {
678680
Allocator alloc(scenario.Min, scenario.Max);
679681
VERIFY_IS_TRUE(scenario.InsertSpans(alloc));
680682

683+
// This needs to be allocated outside the control flow because we need the
684+
// stack allocation to remain valid until the spans are verified.
685+
Element e;
681686
if (!largestGap || // no gaps
682687
(sizeLess1 < UINT_MAX && sizeLess1 > largestGap->sizeLess1) || // not unbounded and size too large
683688
(sizeLess1 == UINT_MAX && !pEndGap)) { // unbounded and no end gap
684689
// no large enough gap, should fail to allocate
685-
Element e(UINT_MAX, 0, 0);
690+
e = Element(UINT_MAX, 0, 0);
686691
unsigned pos = 0xFEFEFEFE;
687692
if (sizeLess1 == UINT_MAX) {
688693
VERIFY_IS_FALSE(alloc.AllocateUnbounded(&e, pos, alignment));
@@ -699,7 +704,7 @@ TEST_F(AllocatorTest, Allocate) {
699704
DXASSERT_NOMSG(expectedGap);
700705
unsigned start = expectedGap->start;
701706
unsigned end = expectedGap->start + sizeLess1;
702-
Element e(UINT_MAX, start, end);
707+
e = Element(UINT_MAX, start, end);
703708
unsigned pos = 0xFEFEFEFE;
704709
if (sizeLess1 == UINT_MAX) {
705710
e.end = expectedGap->end;

0 commit comments

Comments
 (0)