fix: add null pointer checks for D-Bus proxy member variables#3255
fix: add null pointer checks for D-Bus proxy member variables#3255xionglinlin wants to merge 1 commit into
Conversation
1. Add null pointer checks before accessing properties on m_defaultSink, m_defaultSource, and m_sourceMeter 2. Return safe default values (false, 0.0, 0, empty AudioPort, empty QDBusObjectPath) when pointers are null 3. Prevent potential crashes caused by dereferencing null pointers when D-Bus services are unavailable or not properly initialized Log: null pointer checks added for sound D-Bus proxy members Influence: 1. Test sound settings panel with audio device disconnected or unavailable 2. Verify mute, volume, balance, base volume, active port, and card functions return safe defaults 3. Test source and meter related functions with null D-Bus object 4. Ensure no application crashes occur when audio D-Bus services are not ready 5. Validate audio state transitions between device plugged and unplugged scenarios fix: 为 D-Bus 代理成员变量添加空指针检查 1. 在访问 m_defaultSink、m_defaultSource 和 m_sourceMeter 的属性之前添加 空指针检查 2. 当指针为空时返回安全默认值(false、0.0、0、空 AudioPort、空 QDBusObjectPath) 3. 防止因 D-Bus 服务不可用或未正确初始化时解引用空指针导致程序崩溃 Log: 为声音 D-Bus 代理成员添加空指针检查 Influence: 1. 在音频设备断开或不可用时测试声音设置面板 2. 验证静音、音量、平衡、基础音量、活动端口和卡片功能返回安全默认值 3. 在 D-Bus 对象为空时测试源和仪表相关函数 4. 确保音频 D-Bus 服务未就绪时应用程序不会崩溃 5. 验证设备插入和拔出场景下音频状态的正确转换 Change-Id: Idb74020cc57cbfe5cec931abb422609de16f60b8
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: xionglinlin 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 |
There was a problem hiding this comment.
Sorry @xionglinlin, you have reached your weekly rate limit of 500000 diff characters.
Please try again later or upgrade to continue using Sourcery
deepin pr auto review你好!我是CodeGeeX。我已经仔细审查了你提供的 这是一个非常好的防御性编程改进!不过,在语法逻辑、代码质量、性能和安全方面,我还有以下几点改进建议: 1. 语法与逻辑
2. 代码质量
3. 代码性能
4. 代码安全
综合改进示例代码结合以上建议,这里给出一个优化后的代码示例: // 在头文件中增加模板辅助函数(private)
template<typename T>
T getDbusPropertySafe(QDBusAbstractInterface* interface, const QString& propName, T defaultValue = T()) {
if (!interface) {
qCWarning(ddcSoundLog) << "DBus interface is null when querying property:" << propName;
return defaultValue;
}
return qvariant_cast<T>(interface->property(propName.toUtf8().constData()));
}
// CPP 文件实现
bool SoundDBusProxy::muteSink()
{
return getDbusPropertySafe<bool>(m_defaultSink, "MuteSink", false);
}
double SoundDBusProxy::balanceSink()
{
return getDbusPropertySafe<double>(m_defaultSink, "BalanceSink", 0.0);
}
QDBusObjectPath SoundDBusProxy::GetMeter()
{
if (m_defaultSource) {
QList<QVariant> argumentList;
return QDBusPendingReply<QDBusObjectPath>(m_defaultSource->asyncCallWithArgumentList(QStringLiteral("GetMeter"), argumentList));
}
qCWarning(ddcSoundLog) << "m_defaultSource is null, cannot call GetMeter";
return QDBusObjectPath();
}总结:你的核心修改(增加判空)是非常正确且必要的,能够有效提升程序的健壮性。如果能在代码可读性(减少重复代码)、可观测性(增加日志)上再做些优化,这段代码的质量会更高。 |
Log: null pointer checks added for sound D-Bus proxy members
Influence:
fix: 为 D-Bus 代理成员变量添加空指针检查
Log: 为声音 D-Bus 代理成员添加空指针检查
Influence:
Change-Id: Idb74020cc57cbfe5cec931abb422609de16f60b8