Skip to content

fix(test): resolve ASAN heap-buffer-overflow in textedit unit tests - #504

Closed
pengfeixx wants to merge 1 commit into
linuxdeepin:masterfrom
pengfeixx:fix/ut-textedit-heap-buffer-overflow
Closed

fix(test): resolve ASAN heap-buffer-overflow in textedit unit tests#504
pengfeixx wants to merge 1 commit into
linuxdeepin:masterfrom
pengfeixx:fix/ut-textedit-heap-buffer-overflow

Conversation

@pengfeixx

@pengfeixx pengfeixx commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Add stubs for insertMultiTextEx, deleteMultiTextEx and slotCanUndoChanged to prevent unsafe Window pointer access via EditWrapper::window() when no real Window parent exists.

修复单元测试中因 EditWrapper 缺少 Window 父对象导致的堆越界崩溃,
通过 stub 隔离 undo 栈信号链中的 Window 访问路径。

同时修复源码中 slotCanUndoChanged/slotCanRedoChanged 的空指针 解引用风险,使用 dynamic_cast 替代直接调用,增加 m_wrapper 空检查。
修复 Tabbar::eventFilter 事件过滤范围过宽的问题。
修复 Qt6 API 兼容性(QMouseEvent/QHoverEvent 构造函数变更)。
修复 resizeEvent lambda 中的悬空指针风险(使用 QPointer 保护)。
修复 Window::checkBlockShutdown 空标签文本的越界访问。

Log: 修复单元测试ASAN堆越界崩溃及多处空指针/类型安全问题
Influence: 修复后单元测试不再因类型混淆和堆越界崩溃,提升测试稳定性;源码修复消除了 slotCanUndoChanged 等路径中的空指针解引用和悬空指针风险。

Summary by Sourcery

Fix text editor tests and core components to address ASAN heap-buffer-overflow, null-pointer risks, and Qt6 API compatibility issues.

Bug Fixes:

  • Prevent unsafe access to EditWrapper::window() in unit tests by stubbing multi-text insertion/deletion and undo-related slots.
  • Guard TextEdit undo/redo handlers and text-changed callbacks against null wrappers and non-Window parents, avoiding null dereferences and type confusion.
  • Avoid dangling pointer use in TextEdit::resizeEvent by protecting the delayed lambda with QPointer.
  • Fix Window::checkBlockShutdown to safely handle tabs with null or empty titles before checking the first character.
  • Ensure Window printing tests do not delete null print document or preview pointers.
  • Restrict Tabbar::eventFilter handling of ApplicationFontChange to the tab bar itself and narrow handled event types to reduce unintended side effects and logging noise.
  • Update tests to use Qt6-compatible QMouseEvent, QHoverEvent, and QDropEvent constructors and remove a dangerous QString::size stub in encoding detection tests by feeding real XML data.

Enhancements:

  • Extend test coverage for TextEdit multi-text comment operations and mid-button insert behavior via new stubs and safer undo signal handling.

Build:

  • Enable building and running tests in both Debug and Release CMake build types.

Documentation:

  • Update SPDX copyright years across several test files to include 2026.

Tests:

  • Adjust multiple editor, widget, tab bar, and utility tests for Qt6 event API changes and safer test data setup, improving stability under sanitizers.

Add stubs for insertMultiTextEx, deleteMultiTextEx and
slotCanUndoChanged to prevent unsafe Window pointer access via
EditWrapper::window() when no real Window parent exists.

修复单元测试中因 EditWrapper 缺少 Window 父对象导致的堆越界崩溃,
通过 stub 隔离 undo 栈信号链中的 Window 访问路径。

同时修复源码中 slotCanUndoChanged/slotCanRedoChanged 的空指针
解引用风险,使用 dynamic_cast 替代直接调用,增加 m_wrapper 空检查。
修复 Tabbar::eventFilter 事件过滤范围过宽的问题。
修复 Qt6 API 兼容性(QMouseEvent/QHoverEvent 构造函数变更)。
修复 resizeEvent lambda 中的悬空指针风险(使用 QPointer 保护)。
修复 Window::checkBlockShutdown 空标签文本的越界访问。

Log: 修复单元测试ASAN堆越界崩溃及多处空指针/类型安全问题
Influence: 修复后单元测试不再因类型混淆和堆越界崩溃,提升测试稳定性;源码修复消除了 slotCanUndoChanged 等路径中的空指针解引用和悬空指针风险。

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @pengfeixx, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: pengfeixx

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sourcery-ai

sourcery-ai Bot commented Aug 3, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adds safer undo/redo handling and Qt6-compatible test code to eliminate ASAN heap-buffer-overflow and null/dangling pointer risks in TextEdit, Tabbar, Window, and related unit tests.

Sequence diagram for safer TextEdit undo/redo modify-status updates

sequenceDiagram
    participant UndoStack
    participant TextEdit
    participant EditWrapper
    participant Window

    UndoStack->>TextEdit: canUndoChanged(bool)
    TextEdit->>TextEdit: slotCanUndoChanged(bool)
    TextEdit->>TextEdit: [check m_wrapper != nullptr]
    TextEdit->>EditWrapper: isTemFile()
    TextEdit->>UndoStack: canUndo()
    TextEdit->>UndoStack: index()
    TextEdit->>TextEdit: compute isModified
    TextEdit->>EditWrapper: window()
    TextEdit->>TextEdit: [cast to Window succeeds]
    TextEdit->>Window: updateModifyStatus(m_sFilePath, isModified)
    TextEdit->>EditWrapper: OnUpdateHighlighter()

    UndoStack->>TextEdit: canRedoChanged(bool)
    TextEdit->>TextEdit: slotCanRedoChanged(bool)
    TextEdit->>TextEdit: [check m_wrapper != nullptr]
    TextEdit->>EditWrapper: isTemFile()
    TextEdit->>UndoStack: canUndo()
    TextEdit->>UndoStack: index()
    TextEdit->>TextEdit: compute isModified
    TextEdit->>EditWrapper: window()
    TextEdit->>TextEdit: [cast to Window succeeds]
    TextEdit->>Window: updateModifyStatus(m_sFilePath, isModified)
    TextEdit->>EditWrapper: OnUpdateHighlighter()
Loading

File-Level Changes

Change Details Files
Stub out TextEdit multi-insert/delete and undo-related slots in tests to avoid accessing non-existent Window parents and to stabilize text edit unit tests.
  • Introduce no-op stubs for insertMultiTextEx, deleteMultiTextEx, and slotCanUndoChanged in ut_textedit tests.
  • Wire new stubs into setComment/removeComment and mid-button insert tests via Stub::set calls to intercept TextEdit methods.
  • Update test copyright headers and ensure EditWrapper-based tests no longer dereference missing Window instances.
tests/src/editor/ut_textedit.cpp
Update tests for Qt6 event API changes and safer behavior around events, printing and drops.
  • Adjust QMouseEvent, QHoverEvent, QContextMenuEvent, and QDropEvent constructors in editor, widgets, controls, and window tests to include global positions and new parameters required by Qt6.
  • Guard Window print-preview cleanup in tests with null checks before deleteLater.
  • Modernize encoding detection tests by replacing risky QString::size stubs with concrete XML payloads per script.
  • Enable building tests in both Debug and Release configurations via CMake change.
tests/src/editor/ut_textedit.cpp
tests/src/common/ut_utils.cpp
tests/src/widgets/ut_colorselectwidget.cpp
tests/src/widgets/ut_window.cpp
tests/src/controls/ut_tabbar.cpp
CMakeLists.txt
Harden TextEdit undo/redo signal handling and resize behavior against null and dangling pointers.
  • Add null checks for m_wrapper before using it in textChanged connection and undo/redo slots.
  • Use dynamic_cast<Window*> when accessing m_wrapper->window() and only call updateModifyStatus when the cast succeeds.
  • Protect the resizeEvent QTimer lambda with QPointer and route documentSizeChanged and scroll updates via the guarded pointer.
src/editor/dtextedit.cpp
Narrow Tabbar event filtering to relevant objects and events and fix potential out-of-range access in Window::checkBlockShutdown.
  • Change Tabbar::eventFilter signature to use the watched QObject and restrict ApplicationFontChange handling to the Tabbar instance itself.
  • Early-return from Tabbar::eventFilter for non-mouse/non-drag events and remove an extra debug log line.
  • Cache tab text in Window::checkBlockShutdown, treat null or empty text as non-blocking, and avoid direct at(0) on possibly empty strings.
src/controls/tabbar.cpp
src/widgets/window.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@pengfeixx pengfeixx closed this Aug 3, 2026
@pengfeixx
pengfeixx deleted the fix/ut-textedit-heap-buffer-overflow branch August 3, 2026 06:27
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.

2 participants