Skip to content

fix(test): adapt unit tests for Qt6 compilation and runtime#420

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:fix/qt6-unittest-compat
Jun 2, 2026
Merged

fix(test): adapt unit tests for Qt6 compilation and runtime#420
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:fix/qt6-unittest-compat

Conversation

@pengfeixx
Copy link
Copy Markdown
Contributor

Adapt QMouseEvent constructors to Qt6 signature (QPointF, 6-param). Fix sizeof(QString) misuse causing stack overflow on Qt6. Fix null deref in bindFontBySizeAndWeight before null check. Fix AptConfigMessage while loop mid() out-of-bounds on Qt6. Initialize DebIr bool members to prevent UB from uninitialized reads.

适配单元测试到Qt6环境:修复QMouseEvent构造函数签名、
sizeof(QString)误用、空指针解引用、mid()越界及未初始化成员。

Log: 修复单元测试Qt6编译和运行兼容性
Influence: 单元测试可在Qt6环境下正常编译运行,修复了多处ASAN检测到的内存安全问题。

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

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

Adapt QMouseEvent constructors to Qt6 signature (QPointF, 6-param).
Fix sizeof(QString) misuse causing stack overflow on Qt6.
Fix null deref in bindFontBySizeAndWeight before null check.
Fix AptConfigMessage while loop mid() out-of-bounds on Qt6.
Initialize DebIr bool members to prevent UB from uninitialized reads.

适配单元测试到Qt6环境:修复QMouseEvent构造函数签名、
sizeof(QString)误用、空指针解引用、mid()越界及未初始化成员。

Log: 修复单元测试Qt6编译和运行兼容性
Influence: 单元测试可在Qt6环境下正常编译运行,修复了多处ASAN检测到的内存安全问题。
@pengfeixx pengfeixx force-pushed the fix/qt6-unittest-compat branch from 0963120 to 4618682 Compare June 2, 2026 10:51
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

你好!我是CodeGeeX。我已仔细审查了你提供的Git Diff。本次修改主要涉及版权年份更新、结构体成员默认初始化、空指针解结防护、字符串解析逻辑修复、Qt类构造函数参数适配以及sizeof误用的修复。

整体来看,这些修改都是非常有价值的,修复了多个潜在的Bug和安全隐患。以下是针对各个修改点的详细审查意见:

1. 语法与逻辑

🟢 优秀:修复 AptConfigMessage::appendTextEdit 的解析逻辑

+        if (num == -1) {
+            // 没有更多换行符,将剩余内容追加后退出
+            if (configMessage.size() > 0)
+                m_textEdit->appendText(configMessage);
+            break;
+        }

审查意见:这是一个非常关键的逻辑修复。原代码在 while(num != -1) 循环中,如果 indexOf("\\n") 返回 -1,依然会向下执行 configMessage.mid(0, -1),这会导致截取整个剩余字符串甚至引发不可预期的行为。修改后及时 break 并处理剩余内容,逻辑更加严密。

改进建议
虽然逻辑已修复,但 while 循环处理字符串的方式略显冗余。既然已经使用了 indexOfmid,其实可以考虑使用 QString::split("\\n") 直接分割,代码会更简洁。不过基于最小改动原则,当前的修复是完全可以接受的。另外,configMessage.size() > 0 可以简写为 !configMessage.isEmpty(),更符合Qt的编码规范。

🟢 优秀:修复 ut_deblistmodel.cpp 中的 sizeof 误用

-    int length = sizeof(buffer);
+    int length = buffer.size();

审查意见:非常正确的修复!bufferQString 类型,sizeof(buffer) 获取的是 QString 对象在内存中占用的字节数(通常在64位系统上是指针大小或内部结构大小,远大于字符串长度),而不是字符串的字符数。改为 buffer.size() 才是获取字符串的长度。

2. 代码质量

🟢 优秀:结构体成员默认初始化

-    bool archMatched;  // 是否与当前架构匹配
-    bool isValid;  // 包是否有效
+    bool archMatched = false;  // 是否与当前架构匹配
+    bool isValid = false;  // 包是否有效

审查意见:极好的改进!在C++中,未初始化的局部/成员内置类型(如 bool)其值是未定义的(Indeterminate Value),极易引发偶现的逻辑Bug。赋予默认值 false 是良好的编程习惯,符合现代C++规范(如C++11的类内初始值设定项)。

🟢 优秀:空指针防护前移

-    qCDebug(appLog) << "Binding font to widget:" << widget->objectName() << fontFamily << fontSize << fontWeight;
     if (nullptr == widget) {
         qCWarning(appLog) << "Cannot bind font to null widget";
         return;
     }
+    qCDebug(appLog) << "Binding font to widget:" << widget->objectName() << fontFamily << fontSize << fontWeight;

审查意见:修复了潜在的空指针解引用崩溃问题。如果 widgetnullptr,原代码在打印日志时调用 widget->objectName() 会导致程序崩溃。将日志打印移到判空检查之后,确保了安全。

3. 代码性能

本次修改对性能没有负面影响,反而有微小提升:

  • Utils::bindFontBySizeAndWeight 中,当 widget 为空时,不再执行无意义的日志字符串拼接运算,提前返回节约了开销。
  • AptConfigMessage::appendTextEdit 中,避免了无效的 mid 截取操作。

4. 代码安全

🟢 优秀:适配 Qt 6 的 QMouseEvent 构造函数

-    QMouseEvent mousePressEvent(QEvent::MouseButtonPress, QPoint(10, 10), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
+    QMouseEvent mousePressEvent(QEvent::MouseButtonPress, QPointF(10, 10), QPointF(10, 10), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);

以及:

     QMouseEvent releaseEvent(QEvent::MouseButtonRelease,
-                             QPoint(0, 0),
-                             QPoint(0, 0),
-                             QPoint(0, 0),
+                             QPointF(0, 0),
+                             QPointF(0, 0),
                              Qt::LeftButton,
                              Qt::LeftButton,
-                             Qt::NoModifier,
-                             Qt::MouseEventSynthesizedByQt);
+                             Qt::NoModifier);

审查意见:这是为了兼容 Qt 6 的重要安全/兼容性修复。

  1. 参数类型变更:Qt 6 将 QMouseEvent 的位置参数从 QPoint 升级为了 QPointF 以支持高分辨率和亚像素精度。显式传入 QPointF 可以避免隐式转换带来的精度丢失或编译警告。
  2. 构造函数重载变更:Qt 6 移除了带有 source 参数(如 Qt::MouseEventSynthesizedByQt)的 QMouseEvent 构造函数重载。原代码在 Qt 6 下会直接编译报错。删除该参数是向 Qt 6 迁移的必要步骤。

💡 综合总结与额外建议

本次Diff质量很高,精准地修复了未定义行为、空指针崩溃、字符串操作越界、API过时等多个关键问题。

额外建议
packageselectmodel.h 中,既然已经对 bool 类型进行了类内初始化,建议审视一下 DebIr 结构体中的其他成员(如 QStringList virtualPackagesQList<QApt::DependencyItem> depends)。虽然Qt的容器类默认构造就是空的,不需要显式写 = {},但保持代码风格一致性也是不错的。另外,operator== 建议标记为 const 成员函数:

-    bool operator==(const DebIr &rhs) { return this->md5 == rhs.md5; }
+    bool operator==(const DebIr &rhs) const { return this->md5 == rhs.md5; }

这符合良好的C++设计原则,允许在 const 上下文中使用该比较运算符。

@deepin-ci-robot
Copy link
Copy Markdown

[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.

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

@pengfeixx
Copy link
Copy Markdown
Contributor Author

/merge

@deepin-bot deepin-bot Bot merged commit ba9aea6 into linuxdeepin:master Jun 2, 2026
19 checks passed
@pengfeixx pengfeixx deleted the fix/qt6-unittest-compat branch June 2, 2026 11:18
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.

3 participants