Skip to content

Commit eb0b307

Browse files
authored
Merge pull request #27832 from bgogul/fix_asan_errors
Fix memory error in the implementation of IndexSubset.
2 parents d5b232c + 68185cf commit eb0b307

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

include/swift/AST/IndexSubset.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ class IndexSubset : public llvm::FoldingSetNode {
5757
/// The number of bit words in the index subset.
5858
unsigned numBitWords;
5959

60+
static unsigned getNumBytesNeededForCapacity(unsigned capacity) {
61+
return getNumBitWordsNeededForCapacity(capacity) * bitWordSize;
62+
}
63+
6064
BitWord *getBitWordsData() {
6165
return reinterpret_cast<BitWord *>(this + 1);
6266
}

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4637,7 +4637,7 @@ IndexSubset::get(ASTContext &ctx, const SmallBitVector &indices) {
46374637
if (existing)
46384638
return existing;
46394639
auto sizeToAlloc = sizeof(IndexSubset) +
4640-
getNumBitWordsNeededForCapacity(capacity);
4640+
getNumBytesNeededForCapacity(capacity);
46414641
auto *buf = reinterpret_cast<IndexSubset *>(
46424642
ctx.Allocate(sizeToAlloc, alignof(IndexSubset)));
46434643
auto *newNode = new (buf) IndexSubset(indices);

unittests/AST/IndexSubsetTests.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ TEST(IndexSubset, BitWordIndexAndOffset) {
4646
std::make_pair(1u, 0u));
4747
}
4848

49-
TEST(IndexSubset, /* rdar://problem/56338898 */DISABLED_Equality) {
49+
TEST(IndexSubset, Equality) {
5050
TestContext ctx;
5151
EXPECT_EQ(IndexSubset::get(ctx.Ctx, /*capacity*/ 5, /*indices*/ {0}),
5252
IndexSubset::get(ctx.Ctx, /*capacity*/ 5, /*indices*/ {0}));
@@ -60,7 +60,7 @@ TEST(IndexSubset, /* rdar://problem/56338898 */DISABLED_Equality) {
6060
IndexSubset::get(ctx.Ctx, /*capacity*/ 5, /*indices*/ {}));
6161
}
6262

63-
TEST(IndexSubset, /* rdar://problem/56338898 */DISABLED_Initializers) {
63+
TEST(IndexSubset, Initializers) {
6464
TestContext ctx;
6565
// Default init.
6666
EXPECT_EQ(IndexSubset::getDefault(ctx.Ctx, /*capacity*/ 5,
@@ -101,7 +101,7 @@ TEST(IndexSubset, /* rdar://problem/56338898 */DISABLED_Initializers) {
101101
IndexSubset::get(ctx.Ctx, /*capacity*/ 0, /*indices*/ {}));
102102
}
103103

104-
TEST(IndexSubset, /* rdar://problem/56338898 */DISABLED_Bits) {
104+
TEST(IndexSubset, Bits) {
105105
TestContext ctx;
106106
auto *indices1 = IndexSubset::get(ctx.Ctx, /*capacity*/ 5,
107107
/*indices*/ {0, 2, 4});
@@ -124,7 +124,7 @@ TEST(IndexSubset, /* rdar://problem/56338898 */DISABLED_Bits) {
124124
EXPECT_FALSE(indices2->contains(4));
125125
}
126126

127-
TEST(IndexSubset, /* rdar://problem/56338898 */DISABLED_Iteration) {
127+
TEST(IndexSubset, Iteration) {
128128
TestContext ctx;
129129
// Test 1
130130
{
@@ -164,7 +164,7 @@ TEST(IndexSubset, /* rdar://problem/56338898 */DISABLED_Iteration) {
164164
}
165165
}
166166

167-
TEST(IndexSubset, /* rdar://problem/56338898 */DISABLED_SupersetAndSubset) {
167+
TEST(IndexSubset, SupersetAndSubset) {
168168
TestContext ctx;
169169
auto *indices1 = IndexSubset::get(ctx.Ctx, /*capacity*/ 5,
170170
/*indices*/ {0, 2, 4});
@@ -178,7 +178,7 @@ TEST(IndexSubset, /* rdar://problem/56338898 */DISABLED_SupersetAndSubset) {
178178
EXPECT_TRUE(indices2->isSubsetOf(indices1));
179179
}
180180

181-
TEST(IndexSubset, /* rdar://problem/56338898 */DISABLED_Insertion) {
181+
TEST(IndexSubset, Insertion) {
182182
TestContext ctx;
183183
auto *indices1 = IndexSubset::get(ctx.Ctx, 5, {0, 2, 4});
184184
EXPECT_EQ(indices1->adding(0, ctx.Ctx), indices1);

0 commit comments

Comments
 (0)