Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

[iOS][Fabric] `RCTSwitchComponentView onChange:` dereferences `_eventEmitter` without a null check (EXC_BAD_ACCESS after view recycle)

クローズ 初心者向け
#59,203 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
76/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
objective-c, react-native
領域
mobile

調査の方向性

React/Fabric/Mounting/ComponentViews/Switch/RCTSwitchComponentView.mmを読み、特にonChange:と、issueで言及されている近くの再利用処理を確認してください。報告で挙げられている同種のコンポーネントビューにおけるevent emitterのガードを比較し、その後、関連するSwitchのテストを確認するか、既存のテストでこの経路が明らかになる場合は再利用されたビューのテストを追加してください。onChange:がクリア済みのemitterを安全に処理し、既存のSwitchの動作が引き続きテストでカバーされていれば完了です。

索引モデルが issue の本文から書いたものです。

説明

Needs: Author Feedback Needs: Repro

Environment

  • react-native 0.88.0 (first seen on 0.86.0; the file is byte-identical in both), New Architecture
  • iOS 26.5.2 device (production crash), same code path on iOS 27
  • Expo SDK 58 (not involved)

Description

-[RCTSwitchComponentView onChange:] (React/Fabric/Mounting/ComponentViews/Switch/RCTSwitchComponentView.mm:105-114) ends with

static_cast<const SwitchEventEmitter &>(*_eventEmitter)      // line 113
    .onChange(SwitchEventEmitter::OnChange{.value = static_cast<bool>(sender.on)});

with no check that _eventEmitter is set. RCTViewComponentView -prepareForRecycle resets it (RCTViewComponentView.mm:779, _eventEmitter.reset();), while _props stays valid, so the earlier *_props dereference does not fail.

Production crash (0.86.0, line 114 of that version), iOS 26.5.2:

EXC_BAD_ACCESS (SIGSEGV)  KERN_INVALID_ADDRESS at 0x0000000000000030
0  React     std::weak_ptr<EventDispatcher const>::lock()
1  React     EventEmitter::dispatchEvent(...)
4  React     SwitchEventEmitter::onChange(...)
5  React     -[RCTSwitchComponentView onChange:]
6  UIKitCore -[UIApplication sendAction:to:from:forEvent:]
9  UIKitCore -[UISwitchModernVisualElement sendStateChangeActions]
11 UIKitCore -[UISwitchModernVisualElement _handleLongPressWithGestureLocationInBounds:...]
12 UIKitCore -[_UIAfterCACommitBlock run]

0x30 is a null this plus the eventDispatcher_ offset in EventEmitter.

Reproduction

Intermittent, not reproduced on demand. Conditions: iOS 26+, where the redesigned UISwitch (UISwitchModernVisualElement) delivers the state-change action from a deferred _UIAfterCACommitBlock instead of synchronously on touch-up (seen when the switch is dragged or long-pressed rather than tapped). If React re-renders and recycles the Switch view between the gesture ending and that block running, prepareForRecycle has already cleared _eventEmitter and onChange: runs on the recycled view.

Root cause

Missing null guard. Sibling components in the same directory tree already have it: RCTScrollViewComponentView, RCTImageComponentView, RCTModalHostViewComponentView, RCTVirtualViewComponentView (if (!_eventEmitter) { return; }), and RCTTextInputComponentView tests _eventEmitter before use at several sites. Switch is the outlier.

Proposed fix

--- a/packages/react-native/React/Fabric/Mounting/ComponentViews/Switch/RCTSwitchComponentView.mm
+++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/Switch/RCTSwitchComponentView.mm
@@ -110,6 +110,9 @@ - (void)onChange:(UISwitch *)sender
     return;
   }
 
+  if (!_eventEmitter) {
+    return;
+  }
   static_cast<const SwitchEventEmitter &>(*_eventEmitter)
       .onChange(SwitchEventEmitter::OnChange{.value = static_cast<bool>(sender.on)});
 }

We carry this as a local patch. Dropping the event is the right behaviour: the view has been recycled and no longer represents that React node.


Disclosure: drafted with AI assistance (Claude) from a patch we carry locally; code references were checked against the published package sources. Happy to clarify or open a PR.

主要言語
C++
スター
127k
フォーク
25.3k
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

react/react-native のほかの issue

react/react-native の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。