File tree Expand file tree Collapse file tree 1 file changed +6
-1
lines changed
Expand file tree Collapse file tree 1 file changed +6
-1
lines changed Original file line number Diff line number Diff line change @@ -384,7 +384,12 @@ extension JSObject: _BridgedSwiftStackType {
384384 }
385385
386386 @_spi ( BridgeJS) public consuming func bridgeJSLowerReturn( ) -> Int32 {
387- return _swift_js_retain ( Int32 ( bitPattern: self . id) )
387+ // withExtendedLifetime is required here to prevent a use-after-free.
388+ // In a `consuming func`, Swift ARC may release `self` (and thus release
389+ // the underlying JS reference) as soon as it extracts `self.id`, which
390+ // happens *before* `_swift_js_retain` is called. `withExtendedLifetime` forces
391+ // `self` to stay alive until after `_swift_js_retain` returns.
392+ return withExtendedLifetime ( self ) { _swift_js_retain ( Int32 ( bitPattern: self . id) ) }
388393 }
389394
390395 @_spi ( BridgeJS) public consuming func bridgeJSStackPush( ) {
You can’t perform that action at this time.
0 commit comments