fix: prevent crash when removing tray items by PID#438
fix: prevent crash when removing tray items by PID#43818202781743 merged 1 commit intolinuxdeepin:masterfrom
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's GuideFixes a crash in XembedProtocol::onRemoveItemByPid() by iterating over a stable copy of the registered tray item keys instead of the live key set, avoiding iterator invalidation when removing items during PID-based tray icon removal. Sequence diagram for PID-based tray item removal in XembedProtocol::onRemoveItemByPidsequenceDiagram
actor User
participant TrayManager
participant XembedProtocol
participant m_registedItem
participant m_item2Pid
User->>TrayManager: Request tray item removal by PID
TrayManager->>XembedProtocol: onRemoveItemByPid(pid)
XembedProtocol->>m_registedItem: keys() // returns key list copy
m_registedItem-->>XembedProtocol: keys
XembedProtocol->>XembedProtocol: find_if over keys
alt matching key found
XembedProtocol->>m_item2Pid: remove(foundKey)
XembedProtocol->>m_registedItem: remove(foundKey)
else no matching key
XembedProtocol->>XembedProtocol: do nothing
end
XembedProtocol-->>TrayManager: Return
TrayManager-->>User: Tray item removed safely (no crash)
Updated class diagram for XembedProtocol tray item removal logicclassDiagram
class XembedProtocol {
- QMap~uint, TrayItem~ m_registedItem
- QHash~uint, uint~ m_item2Pid
+ void onTrayIconsChanged()
+ void onRemoveItemByPid(uint pid)
}
class QMap_uint_TrayItem_ {
+ QList~uint~ keys()
+ void remove(uint key)
}
class QHash_uint_uint_ {
+ void remove(uint key)
}
class QList_uint_ {
+ iterator begin()
+ iterator end()
}
XembedProtocol --> QMap_uint_TrayItem_ : uses m_registedItem
XembedProtocol --> QHash_uint_uint_ : uses m_item2Pid
QMap_uint_TrayItem_ --> QList_uint_ : returns keys() as QList
class TrayItem {
+ uint id
}
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- In the
std::find_ifpredicate, usingm_item2Pid[id]will insert a default entry if the id is missing; consider using a lookup that doesn’t mutate the map (e.g.,m_item2Pid.value(id)or checkingcontainsbefore accessing) to avoid side effects during the search. - Since
m_registedItem.keys()is now copied tokeys, you might prefer a range-based loop overkeysrather thanstd::find_ifto keep the control flow simple and avoid any confusion about which container the iterators belong to.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In the `std::find_if` predicate, using `m_item2Pid[id]` will insert a default entry if the id is missing; consider using a lookup that doesn’t mutate the map (e.g., `m_item2Pid.value(id)` or checking `contains` before accessing) to avoid side effects during the search.
- Since `m_registedItem.keys()` is now copied to `keys`, you might prefer a range-based loop over `keys` rather than `std::find_if` to keep the control flow simple and avoid any confusion about which container the iterators belong to.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
1. Fixed a potential dangling pointer issue by storing m_registedItem.keys() in a local variable 2. Previously used m_registedItem.keys() directly which could become invalid after container modification 3. Now using a const copy of keys to ensure stable iteration during item removal 4. This prevents crashes when removing items from the tray icon registry Influence: 1. Test tray icon removal functionality when applications exit 2. Verify no crashes occur during dynamic tray icon updates 3. Test with multiple tray icons being added and removed simultaneously 4. Verify tray icon registry remains stable during container modifications fix: 修复 XembedProtocol::onRemoveItemByPid 中的野指针问题 1. 通过将 m_registedItem.keys() 存储在局部变量中修复了潜在的野指针问题 2. 之前直接使用 m_registedItem.keys(),在容器修改后可能变为无效 3. 现在使用 keys 的常量副本以确保在移除项目期间迭代的稳定性 4. 这防止了从托盘图标注册表中移除项目时发生崩溃 Influence: 1. 测试应用程序退出时的托盘图标移除功能 2. 验证在动态托盘图标更新期间不会发生崩溃 3. 测试多个托盘图标同时添加和移除的情况 4. 验证托盘图标注册表在容器修改期间保持稳定
deepin pr auto review这段代码的修改主要涉及版权年份更新和一个函数逻辑的修改。下面我将从语法逻辑、代码质量、代码性能和代码安全四个方面进行详细审查。 1. 语法逻辑
2. 代码质量
3. 代码性能
4. 代码安全
总结与建议
改进后的代码示例void XembedProtocol::onRemoveItemByPid(uint pid)
{
// 直接遍历 m_item2Pid 查找匹配的 PID
auto it = std::find_if(m_item2Pid.constBegin(), m_item2Pid.constEnd(),
[pid](const auto &item) { return item.value() == pid; });
if (it != m_item2Pid.constEnd()) {
uint id = it.key();
m_item2Pid.remove(id);
m_registedItem.remove(id);
}
}或者使用 C++17 结构化绑定(如果编译器支持): void XembedProtocol::onRemoveItemByPid(uint pid)
{
for (auto [id, existingPid] : m_item2Pid.asKeyValueRange()) {
if (existingPid == pid) {
m_item2Pid.remove(id);
m_registedItem.remove(id);
break;
}
}
}注意: 1. 语法逻辑
2. 代码质量
3. 代码性能
4. 代码安全
总结与建议
改进后的代码示例基于上述分析,这里提供一个性能更好且逻辑更清晰的版本: void XembedProtocol::onRemoveItemByPid(uint pid)
{
// 直接遍历 m_item2Pid 查找匹配的 PID
// 假设 m_item2Pid 是 QMap<uint, uint>,其中 Key 是 ID,Value 是 PID
auto it = std::find_if(m_item2Pid.constBegin(), m_item2Pid.constEnd(),
[pid](const uint &existingPid) { return existingPid == pid; });
if (it != m_item2Pid.constEnd()) {
uint id = it.key();
m_item2Pid.erase(it);
m_registedItem.remove(id);
}
}或者使用 C++17 结构化绑定(如果编译器支持): void XembedProtocol::onRemoveItemByPid(uint pid)
{
for (auto [id, existingPid] : m_item2Pid.asKeyValueRange()) {
if (existingPid == pid) {
m_item2Pid.remove(id);
m_registedItem.remove(id);
break;
}
}
}注意: |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: 18202781743, BLumia 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 |
Fixed a potential crash in XembedProtocol::onRemoveItemByPid() by
storing a copy of the keys before iteration.
Previously, the code directly used m_registedItem.keys() in the find_if
algorithm while potentially modifying the container during iteration.
This could lead to iterator invalidation and crashes when items were
removed from the map.
The fix creates a temporary copy of the keys to ensure stable iteration.
Influence:
removed
fix: 修复通过PID移除托盘项时的崩溃问题
修复了XembedProtocol::onRemoveItemByPid()中潜在的崩溃问题,通过在迭代前
存储键的副本。
之前代码直接在find_if算法中使用m_registedItem.keys(),同时在迭代过程中可
能修改容器。
这可能导致迭代器失效,并在从映射中移除项目时引发崩溃。
修复通过创建键的临时副本来确保稳定的迭代。
Influence:
Summary by Sourcery
Bug Fixes: