Skip to content

Commit 737507e

Browse files
authored
[cxx-interop] Fix a potential dangling pointer in String(cxxString:)
`cxxString` might get deallocated immediately after `c_str()` call, which would mean that the pointer passed to `String(cString:)` points to invalid memory. We haven't actually seen this, but let's preemptively add an explicit `withExtendedLifetime` call to avoid running into this in the future.
1 parent 8b77860 commit 737507e

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

stdlib/public/Cxx/String.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ extension std.string {
2222
extension String {
2323
public init(cxxString: std.string) {
2424
self.init(cString: cxxString.c_str())
25+
withExtendedLifetime(cxxString) {}
2526
}
2627
}

0 commit comments

Comments
 (0)