Skip to content

[Stdlib] Fix an overrelease in -[__SwiftNativeNSError description]. #29904

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
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
10 changes: 8 additions & 2 deletions stdlib/public/runtime/ErrorObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,14 @@ - (void)dealloc {

- (id /* NSString */)description {
auto error = (const SwiftError *)self;
return getDescription(const_cast<OpaqueValue *>(error->getValue()),
error->type);
auto value = error->getValue();

// Copy the value, since it will be consumed by getDescription.
ValueBuffer copyBuf;
auto copy = error->type->allocateBufferIn(&copyBuf);
error->type->vw_initializeWithCopy(copy, const_cast<OpaqueValue *>(value));

return getDescription(copy, error->type);
}

- (NSInteger)code {
Expand Down
31 changes: 31 additions & 0 deletions test/stdlib/ErrorBridged.swift
Original file line number Diff line number Diff line change
Expand Up @@ -854,4 +854,35 @@ ErrorBridgingTests.test("Swift Error bridged to NSError description") {
}
}

struct SwiftError2: Error, CustomStringConvertible {
var description: String
}

ErrorBridgingTests.test("Swift Error description memory management") {
func checkDescription() {
// Generate a non-small, non-constant NSString bridged to String.
let str = (["""
There once was a gigantic genie
Who turned out to be a real meanie
I wished for flight
And with all his might
He gave me a propellor beanie
"""] as NSArray).description
let error = SwiftError2(description: str)
let bridgedError = error as NSError

// Ensure that the bridged NSError description method doesn't overrelease
// the error value.
for _ in 0 ..< 10 {
autoreleasepool {
expectEqual(str, bridgedError.description)
}
}
}

if #available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *) {
checkDescription()
}
}

runAllTests()