Skip to content

Commit

Permalink
Fix use-after-move in AsyncCallback (#39802)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #39802

Changelog: [Internal]

Reviewed By: NickGerleman

Differential Revision: D49884649

fbshipit-source-id: 63bbad19c35fcc424ae5d32b422a6826f9f30fbf
  • Loading branch information
javache authored and facebook-github-bot committed Oct 4, 2023
1 parent e6094f7 commit d9cc662
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
10 changes: 6 additions & 4 deletions packages/react-native/ReactCommon/react/bridging/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,17 @@ class AsyncCallback {
const {
auto wrapper = callback_->wrapper_.lock();
if (wrapper) {
auto& jsInvoker = wrapper->jsInvoker();
auto fn = [callback = callback_,
argsPtr = std::make_shared<std::tuple<Args...>>(
std::make_tuple(std::forward<Args>(args)...))] {
callback->apply(std::move(*argsPtr));
};

if (priority) {
wrapper->jsInvoker().invokeAsync(*priority, std::move(fn));
jsInvoker.invokeAsync(*priority, std::move(fn));
} else {
wrapper->jsInvoker().invokeAsync(std::move(fn));
jsInvoker.invokeAsync(std::move(fn));
}
}
}
Expand All @@ -80,15 +81,16 @@ class AsyncCallback {
std::function<void(jsi::Runtime&, jsi::Function&)>&& callImpl) const {
auto wrapper = callback_->wrapper_.lock();
if (wrapper) {
auto& jsInvoker = wrapper->jsInvoker();
auto fn = [wrapper = std::move(wrapper),
callImpl = std::move(callImpl)]() {
callImpl(wrapper->runtime(), wrapper->callback());
};

if (priority) {
wrapper->jsInvoker().invokeAsync(*priority, std::move(fn));
jsInvoker.invokeAsync(*priority, std::move(fn));
} else {
wrapper->jsInvoker().invokeAsync(std::move(fn));
jsInvoker.invokeAsync(std::move(fn));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,18 @@ TEST_F(BridgingTest, asyncCallbackTest) {
cb(func, "hello");

flushQueue(); // Run scheduled async work

EXPECT_EQ("hello"s, output);

// Test with lambda invocation
cb.call([func, jsInvoker = invoker](jsi::Runtime& rt, jsi::Function& f) {
f.call(
rt,
bridging::toJs(rt, func, jsInvoker),
bridging::toJs(rt, "hello again", jsInvoker));
});

flushQueue();
EXPECT_EQ("hello again"s, output);
}

TEST_F(BridgingTest, asyncCallbackImplicitBridgingTest) {
Expand Down

0 comments on commit d9cc662

Please sign in to comment.