Skip to content

Commit 6ff9265

Browse files
author
Florent Bruneau
committed
ClangImporter: fix crash when importing type containing bitfields.
Following PR swiftlang#6531 there is a position mismatch in the final loop between the position of the member in the members arrays and the position in the valueParameters array. As a consequence, a structure containing indirect fields before a computed properties (like a bitfield) caused an invalid access in the valueParameters array resulting in a crash of the compiler. This patch maintains a separate position for accessing valueParameters. A non-regression test is also added. Signed-off-by: Florent Bruneau <[email protected]>
1 parent 5e395bc commit 6ff9265

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,14 +1133,18 @@ createValueConstructor(ClangImporter::Implementation &Impl,
11331133

11341134
// To keep DI happy, initialize stored properties before computed.
11351135
for (unsigned pass = 0; pass < 2; pass++) {
1136+
unsigned paramPos = 0;
1137+
11361138
for (unsigned i = 0, e = members.size(); i < e; i++) {
11371139
auto var = members[i];
11381140

11391141
if (var->hasClangNode() && isa<clang::IndirectFieldDecl>(var->getClangDecl()))
11401142
continue;
11411143

1142-
if (var->hasStorage() == (pass != 0))
1144+
if (var->hasStorage() == (pass != 0)) {
1145+
paramPos++;
11431146
continue;
1147+
}
11441148

11451149
// Construct left-hand side.
11461150
Expr *lhs = new (context) DeclRefExpr(selfDecl, DeclNameLoc(),
@@ -1149,12 +1153,14 @@ createValueConstructor(ClangImporter::Implementation &Impl,
11491153
/*Implicit=*/true);
11501154

11511155
// Construct right-hand side.
1152-
auto rhs = new (context) DeclRefExpr(valueParameters[i], DeclNameLoc(),
1156+
auto rhs = new (context) DeclRefExpr(valueParameters[paramPos],
1157+
DeclNameLoc(),
11531158
/*Implicit=*/true);
11541159

11551160
// Add assignment.
11561161
stmts.push_back(new (context) AssignExpr(lhs, SourceLoc(), rhs,
11571162
/*Implicit=*/true));
1163+
paramPos++;
11581164
}
11591165
}
11601166

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
struct StructWithIndirectField {
2+
union {
3+
int a;
4+
int b;
5+
};
6+
int c;
7+
int d : 3; /* Imported as a computed property */
8+
};
9+
10+
union UnionWithIndirectField {
11+
struct {
12+
int a;
13+
int b;
14+
};
15+
int c;
16+
};

test/ClangImporter/Inputs/custom-modules/module.map

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,7 @@ module MacrosRedefA {
150150
module MacrosRedefB {
151151
header "MacrosRedefB.h"
152152
}
153+
154+
module IndirectFields {
155+
header "IndirectFields.h"
156+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %target-swift-frontend -typecheck -sdk "" -I %t -I %S/Inputs/custom-modules %s -verify
2+
3+
import IndirectFields
4+
5+
func build_struct(a: Int32, c: Int32, d: Int32) -> StructWithIndirectField {
6+
return StructWithIndirectField(__Anonymous_field0: .init(a: a), c: c, d: d)
7+
}
8+
9+
func build_struct(b: Int32, c: Int32, d: Int32) -> StructWithIndirectField {
10+
return StructWithIndirectField(__Anonymous_field0: .init(b: b), c: c, d: d)
11+
}
12+
13+
func build_union(a: Int32, b: Int32) -> UnionWithIndirectField {
14+
return UnionWithIndirectField(__Anonymous_field0: .init(a: a, b: b))
15+
}
16+
17+
func build_union(c: Int32) -> UnionWithIndirectField {
18+
return UnionWithIndirectField(c: c)
19+
}

0 commit comments

Comments
 (0)