Skip to content

fix: 防止无障碍事件源节点索引越界导致崩溃 - #573

Closed
DeanTMaxim wants to merge 1366 commits into
SuperMonster003:masterfrom
DeanTMaxim:fix/accessibility-event-wrapper-index
Closed

DeanTMaxim wants to merge 1366 commits into
SuperMonster003:masterfrom
DeanTMaxim:fix/accessibility-event-wrapper-index

Conversation

@DeanTMaxim

Copy link
Copy Markdown
Contributor

Closes #572

问题

脚本通过 auto.registerEvent 注册窗口变化事件后,AccessibilityService 会在调用脚本回调前构造 AccessibilityEventWrapper。

原 getIndexInParent() 会持续递增索引,直到 parent.getChild(index) 等于事件源节点,但没有按 parent.childCount 限界。页面切换期间节点树可能变化,事件源节点可能已经从父节点移除;此时循环会访问越界索引,并由 Android 框架抛出 ArrayIndexOutOfBoundsException,导致 AutoJs6 进程、无障碍服务和当前脚本一起退出。

修改

  • 将父节点索引查找限制在 0 until childCount 范围内。
  • 父节点为空、目标节点未找到或节点读取抛出 RuntimeException 时返回安全哨兵 -1。
  • 提取不依赖 Android 运行时的 findIndexInParent(),以便用本地单元测试稳定复现节点消失和节点失效场景。
  • 保持 getDepth() 不变:当前崩溃证据只指向 getIndexInParent(),本次不加入任意深度上限或扩大重构范围。

选择 -1 是因为它与 UiObject 构造器的缺省 indexInParent 一致,用来表示没有可用的父级索引;本次不改变 UiObject 的其他导航行为。

测试

新增 AccessibilityNodeIndexTest,覆盖:

  1. childCount = 1 且目标节点已消失时,不读取索引 1,返回 -1。
  2. 读取子节点抛出 RuntimeException 时返回 -1。
  3. 目标节点正常存在时返回正确索引。

TDD 证据:

  • 修复前,第一个用例以 IndexOutOfBoundsException: Index: 1, Size: 1 失败。
  • 加入边界后,第二个用例以 IllegalStateException: stale node 失败。
  • 临时将实现改为恒返 -1 时,正常路径用例以 expected:<1> but was:<-1> 失败。
  • 恢复最终实现后,独立 JUnit 4.13.2 运行结果为 OK (3 tests)。
  • AccessibilityEventWrapper.kt、AccessibilityNodeIndex.kt 连同 Android 36 android.jar 的窄化 Kotlin 编译通过。
  • git diff --check 无空白错误。

提交前再次运行完整 Gradle 任务 :app:testAppDebugUnitTest --no-daemon --offline:

BUILD SUCCESSFUL in 14s
tests=3, skipped=0, failures=0, errors=0

当前 origin/master@ed3eb10e 首先存在以下资源链接错误;测试时仅在本地临时补充了两个颜色资源别名:

bottom_sheet_log.xml: resource color/console_debug not found
bottom_sheet_log.xml: resource color/console_verbose not found

资源链接通过后暴露出同一上游日志面板变更中的第二组既存错误:

LogBottomSheet.kt:82:29 Unresolved reference 'getInstance'
LogBottomSheet.kt:84:47 Unresolved reference 'globalConsole'
LogBottomSheet.kt:101:20 Unresolved reference 'getInstance'

测试时仅在本地把其中的 AutoJs.getInstance() 调整为项目现有的 AutoJs.instance 写法,随后完整 Gradle 目标测试通过。

上述两项临时基线补丁均已删除,未包含在本 PR 中;本 PR 只包含事件包装修复及其回归测试。

…03#416); 移除 UiObject#hashCode/equals 覆写; 新增 UiObject#isShifted 方法
SuperMonster003 and others added 24 commits March 3, 2026 23:41
适配 Android 12 及以上蓝牙权限模型变更
…yout, Expandable RecyclerView, Recyclerview Flexible Divider
Fix ConcatReader.close() to close all readers on failure
- 新增 LogBottomSheet 底部日志面板
- ConsoleView 支持堆栈帧点击跳转
- EditorView 添加 LogPanelCallback 接口
- EditActivity 集成日志面板与代码跳转
- 添加日志菜单项和图标资源
feat: 添加底部日志面板功能
SuperMonster003 added a commit that referenced this pull request Nov 3, 2026
…nd the contributor table

Add the fix entries of the ported PRs #571 (DeanTMaxim, reported in #570), #558 and #557 (djbclark) to the 6.8.0 notes in all ten languages, and add both contributors to the README contributor table by hand, sorted by the date of their latest pull request (2026/08/21 and 2026/07/14), with the counts of all their pull requests, including #556 and #573 whose fixes were already in place. Regenerate the changelog and README files, record the translation baseline, and set VERSION_BUILD to 4982.
@SuperMonster003

Copy link
Copy Markdown
Owner

感谢! 这个越界问题已在当前代码中以相同思路修复 (77602b9, AccessibilityEventWrapper.getIndexInParent 改为在 childCount 范围内有界查找, 失败时返回 -1), 将随 6.8.0 发布. 本 PR 因今天的历史改写 (强制推送) 被 GitHub 自动关闭, 不再单独合并, 你的贡献已计入 README 贡献者列表. #572 将在 6.8.0 发布后关闭.

Thanks! The same bounded lookup is already in the current code (77602b9) and ships with 6.8.0. This pull request was closed automatically after today's history rewrite (force push), so it is not merged separately; your contribution is counted in the README contributor table. #572 will be closed once 6.8.0 is released.

SuperMonster003 added a commit that referenced this pull request Nov 3, 2026
The contributor table scraper rebuilt the table from merged pull requests only, so it would have dropped the rows added by hand for the ported pull requests. Pull requests #556, #557, #558, #571 and #573 are now recorded with the author's commit count and latest commit time as they were in each pull request (their GitHub commit lists span the whole old history after the 2026-11-03 rewrite) and are counted like merged ones; authors with such pull requests link to all their pull requests instead of merged ones only. Running the scraper corrects the commit count of djbclark from 3 to 5 (#556 and #558 have two commits each).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

auto.registerEvent 包装无障碍事件时可能因子节点索引越界导致应用崩溃

6 participants