test(editor): fix heap-buffer-overflow crash in UT_Textedit_MoveText - #518
Conversation
Stub Window::updateModifyStatus in three MoveText tests to avoid invalid downcast in EditWrapper::window() when EditWrapper has no Window parent, which caused heap-buffer-overflow in Tabbar::truePathAt. Also replace QTest::qWait/processEvents with direct timer meta-object invocation in startmanager/window/editorapplication/themepanel tests, add __gcov_dump() in ut_main to ensure coverage data flush, and add new cases for LineBar/WarningNotices. 修复 UT_Textedit_MoveText 三个用例的堆缓冲区溢出崩溃:当 EditWrapper 无 Window 父对象时 EditWrapper::window() 的 static_cast 误转导致越界, 通过 stub Window::updateModifyStatus 规避。同时将多个测试中的 qWait/processEvents 替换为直接通过 meta-object 触发 timer lambda, 在 ut_main 增加 __gcov_dump 确保覆盖率数据落盘,并新增 LineBar/WarningNotices 用例。 Log: 修复单测崩溃并改进测试基础设施 Influence: 仅影响单元测试代码,不改动业务源码;消除 ASan 下的崩溃与 UB。
There was a problem hiding this comment.
Sorry @pengfeixx, you have reached your weekly rate limit of 500000 diff characters.
Please try again later or upgrade to continue using Sourcery
Reviewer's GuideThis PR fixes a heap-buffer-overflow in UT_Textedit_MoveText by stubbing a problematic Window method, and systematically refactors several unit tests to trigger QTimer/QPropertyAnimation lambdas and other callbacks directly instead of via QTest::qWait/processEvents, adds coverage-flushing in the test main, and introduces new tests for LineBar and WarningNotices behavior. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
deepin pr auto review★ 总体评分:87分■ 【总体评价】
■ 【详细分析】
■ 【改进建议代码示例】 // 建议在公共测试头文件(如 test_utils.h)中提取重复的定时器触发逻辑
#ifndef TEST_UTILS_H
#define TEST_UTILS_H
#include <QObject>
#include <QTimer>
#include <QMetaObject>
namespace TestHelper {
// 提取公共方法:查找并触发对象树中的第一个单次定时器
inline void triggerFirstSingleShotTimer(QObject *parent) {
for (QTimer *t : parent->findChildren<QTimer *>()) {
if (t->isSingleShot()) {
QMetaObject::invokeMethod(t, "timeout", Qt::DirectConnection);
break;
}
}
}
} // namespace TestHelper
#endif // TEST_UTILS_H// ut_textedit.cpp 中的修改示例
#include "test_utils.h"
// 删除未使用的 TE_DeferredDeleteBlocker 类
// highlight() internal QTimer::singleShot(0,...) lambda
TEST(UT_test_textedit_highlight, highlight_TriggersLambda)
{
TextEdit *edit = new TextEdit;
EditWrapper *wra = new EditWrapper;
edit->m_wrapper = wra;
edit->highlight();
// 使用提取的公共方法替代重复代码
TestHelper::triggerFirstSingleShotTimer(edit);
edit->deleteLater();
wra->deleteLater();
}
// eventFilter color mark menu Tab key: QTimer::singleShot(0,...) lambda
TEST(UT_test_textedit_eventFilter, eventFilter_ColorMarkMenuTabLambda)
{
TextEdit *edit = new TextEdit;
EditWrapper *wra = new EditWrapper;
edit->m_wrapper = wra;
edit->m_colorMarkMenu = new QMenu;
// 修复:改为栈上分配,避免手动管理内存
QKeyEvent e(QEvent::KeyRelease, Qt::Key_Tab, Qt::NoModifier);
edit->eventFilter(edit->m_colorMarkMenu, &e);
// 使用提取的公共方法替代重复代码
TestHelper::triggerFirstSingleShotTimer(edit);
delete edit->m_colorMarkMenu;
edit->deleteLater();
wra->deleteLater();
} |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: lzwind, pengfeixx The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Stub Window::updateModifyStatus in three MoveText tests to avoid invalid downcast in EditWrapper::window() when EditWrapper has no Window parent, which caused heap-buffer-overflow in Tabbar::truePathAt. Also replace QTest::qWait/processEvents with direct timer meta-object invocation in startmanager/window/editorapplication/themepanel tests, add __gcov_dump() in ut_main to ensure coverage data flush, and add new cases for LineBar/WarningNotices.
修复 UT_Textedit_MoveText 三个用例的堆缓冲区溢出崩溃:当 EditWrapper 无 Window 父对象时 EditWrapper::window() 的 static_cast 误转导致越界, 通过 stub Window::updateModifyStatus 规避。同时将多个测试中的
qWait/processEvents 替换为直接通过 meta-object 触发 timer lambda, 在 ut_main 增加 __gcov_dump 确保覆盖率数据落盘,并新增
LineBar/WarningNotices 用例。
Log: 修复单测崩溃并改进测试基础设施
Influence: 仅影响单元测试代码,不改动业务源码;消除 ASan 下的崩溃与 UB。
Summary by Sourcery
Stabilize editor-related unit tests by avoiding unsafe event processing and ensuring coverage data is flushed on test exit.
Bug Fixes:
Enhancements:
Tests:
Chores: