Skip to content

Fix memory error in the implementation of IndexSubset. #27832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/swift/AST/IndexSubset.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class IndexSubset : public llvm::FoldingSetNode {
/// The number of bit words in the index subset.
unsigned numBitWords;

static unsigned getNumBytesNeededForCapacity(unsigned capacity) {
return getNumBitWordsNeededForCapacity(capacity) * bitWordSize;
}

BitWord *getBitWordsData() {
return reinterpret_cast<BitWord *>(this + 1);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4637,7 +4637,7 @@ IndexSubset::get(ASTContext &ctx, const SmallBitVector &indices) {
if (existing)
return existing;
auto sizeToAlloc = sizeof(IndexSubset) +
getNumBitWordsNeededForCapacity(capacity);
getNumBytesNeededForCapacity(capacity);
auto *buf = reinterpret_cast<IndexSubset *>(
ctx.Allocate(sizeToAlloc, alignof(IndexSubset)));
auto *newNode = new (buf) IndexSubset(indices);
Expand Down
12 changes: 6 additions & 6 deletions unittests/AST/IndexSubsetTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ TEST(IndexSubset, BitWordIndexAndOffset) {
std::make_pair(1u, 0u));
}

TEST(IndexSubset, /* rdar://problem/56338898 */DISABLED_Equality) {
TEST(IndexSubset, Equality) {
TestContext ctx;
EXPECT_EQ(IndexSubset::get(ctx.Ctx, /*capacity*/ 5, /*indices*/ {0}),
IndexSubset::get(ctx.Ctx, /*capacity*/ 5, /*indices*/ {0}));
Expand All @@ -60,7 +60,7 @@ TEST(IndexSubset, /* rdar://problem/56338898 */DISABLED_Equality) {
IndexSubset::get(ctx.Ctx, /*capacity*/ 5, /*indices*/ {}));
}

TEST(IndexSubset, /* rdar://problem/56338898 */DISABLED_Initializers) {
TEST(IndexSubset, Initializers) {
TestContext ctx;
// Default init.
EXPECT_EQ(IndexSubset::getDefault(ctx.Ctx, /*capacity*/ 5,
Expand Down Expand Up @@ -101,7 +101,7 @@ TEST(IndexSubset, /* rdar://problem/56338898 */DISABLED_Initializers) {
IndexSubset::get(ctx.Ctx, /*capacity*/ 0, /*indices*/ {}));
}

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

TEST(IndexSubset, /* rdar://problem/56338898 */DISABLED_Iteration) {
TEST(IndexSubset, Iteration) {
TestContext ctx;
// Test 1
{
Expand Down Expand Up @@ -164,7 +164,7 @@ TEST(IndexSubset, /* rdar://problem/56338898 */DISABLED_Iteration) {
}
}

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

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