Skip to content

Commit d9cc662

Browse files
javachefacebook-github-bot
authored andcommitted
Fix use-after-move in AsyncCallback (#39802)
Summary: Pull Request resolved: #39802 Changelog: [Internal] Reviewed By: NickGerleman Differential Revision: D49884649 fbshipit-source-id: 63bbad19c35fcc424ae5d32b422a6826f9f30fbf
1 parent e6094f7 commit d9cc662

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

packages/react-native/ReactCommon/react/bridging/Function.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,17 @@ class AsyncCallback {
6161
const {
6262
auto wrapper = callback_->wrapper_.lock();
6363
if (wrapper) {
64+
auto& jsInvoker = wrapper->jsInvoker();
6465
auto fn = [callback = callback_,
6566
argsPtr = std::make_shared<std::tuple<Args...>>(
6667
std::make_tuple(std::forward<Args>(args)...))] {
6768
callback->apply(std::move(*argsPtr));
6869
};
6970

7071
if (priority) {
71-
wrapper->jsInvoker().invokeAsync(*priority, std::move(fn));
72+
jsInvoker.invokeAsync(*priority, std::move(fn));
7273
} else {
73-
wrapper->jsInvoker().invokeAsync(std::move(fn));
74+
jsInvoker.invokeAsync(std::move(fn));
7475
}
7576
}
7677
}
@@ -80,15 +81,16 @@ class AsyncCallback {
8081
std::function<void(jsi::Runtime&, jsi::Function&)>&& callImpl) const {
8182
auto wrapper = callback_->wrapper_.lock();
8283
if (wrapper) {
84+
auto& jsInvoker = wrapper->jsInvoker();
8385
auto fn = [wrapper = std::move(wrapper),
8486
callImpl = std::move(callImpl)]() {
8587
callImpl(wrapper->runtime(), wrapper->callback());
8688
};
8789

8890
if (priority) {
89-
wrapper->jsInvoker().invokeAsync(*priority, std::move(fn));
91+
jsInvoker.invokeAsync(*priority, std::move(fn));
9092
} else {
91-
wrapper->jsInvoker().invokeAsync(std::move(fn));
93+
jsInvoker.invokeAsync(std::move(fn));
9294
}
9395
}
9496
}

packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,18 @@ TEST_F(BridgingTest, asyncCallbackTest) {
276276
cb(func, "hello");
277277

278278
flushQueue(); // Run scheduled async work
279-
280279
EXPECT_EQ("hello"s, output);
280+
281+
// Test with lambda invocation
282+
cb.call([func, jsInvoker = invoker](jsi::Runtime& rt, jsi::Function& f) {
283+
f.call(
284+
rt,
285+
bridging::toJs(rt, func, jsInvoker),
286+
bridging::toJs(rt, "hello again", jsInvoker));
287+
});
288+
289+
flushQueue();
290+
EXPECT_EQ("hello again"s, output);
281291
}
282292

283293
TEST_F(BridgingTest, asyncCallbackImplicitBridgingTest) {

0 commit comments

Comments
 (0)