Skip to content

Commit 8856fb1

Browse files
committed
Use uninitialized dealloc in swift_deallocError
1 parent 361f8dd commit 8856fb1

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

stdlib/public/runtime/ErrorObjectNative.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ swift::swift_allocError(const swift::Metadata *type,
9494
void
9595
swift::swift_deallocError(SwiftError *error, const Metadata *type) {
9696
auto sizeAndAlign = _getErrorAllocatedSizeAndAlignmentMask(type);
97-
swift_deallocObject(error, sizeAndAlign.first, sizeAndAlign.second);
97+
swift_deallocUninitializedObject(error, sizeAndAlign.first, sizeAndAlign.second);
9898
}
9999

100100
void

test/stdlib/Error.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,25 @@ ErrorTests.test("unsigned raw value") {
160160
expectEqual(-1, negOne._code)
161161
}
162162

163+
ErrorTests.test("test dealloc empty error box") {
164+
struct Foo<T>: Error { let value: T }
165+
166+
func makeFoo<T>() throws -> Foo<T> {
167+
throw Foo(value: "makeFoo throw error")
168+
}
169+
170+
func makeError<T>(of: T.Type) throws -> Error {
171+
return try makeFoo() as Foo<T>
172+
}
173+
174+
do {
175+
_ = try makeError(of: Int.self)
176+
} catch let foo as Foo<String> {
177+
expectEqual(foo.value, "makeFoo throw error")
178+
} catch {
179+
expectUnreachableCatch(error)
180+
}
181+
}
182+
163183
runAllTests()
164184

0 commit comments

Comments
 (0)