From 956b943fe5b5cd970e078fd9240146efb5422837 Mon Sep 17 00:00:00 2001 From: caixiangrong Date: Tue, 7 Apr 2026 09:41:07 +0800 Subject: [PATCH] fix: fix null pointer and improve DNS/IP configuration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed a null pointer access issue in PageSystemProxy.qml by adding a null check before accessing the visibleToApp property. This prevents potential crashes when the ignoreHosts object is not available. Refactored DNS, IPv4, and IPv6 configuration sections to use DccRepeater for dynamic item management instead of manual component creation and destruction. This simplifies the code structure and eliminates the need for separate arrays to track created items. Added helper functions addAddressData() and removeAddressData() to centralize data manipulation logic. In the IPv6 validation logic, added null checks for gateway values to prevent errors when gateway fields are empty. This ensures proper validation of IPv6 configurations. Log: Fixed system proxy page visibility issue and improved DNS/IP configuration management Influence: 1. Test system proxy settings to ensure the "Ignore hosts" option appears correctly 2. Verify DNS configuration by adding and removing DNS servers 3. Test IPv4 configuration by adding multiple IP addresses 4. Test IPv6 configuration with empty and populated gateway fields 5. Validate all network configuration changes are properly saved 6. Check that UI updates correctly when modifying network settings fix: 修复空指针问题并改进DNS/IP配置 修复了PageSystemProxy.qml中的空指针访问问题,在访问visibleToApp属性前添 加了空值检查,防止ignoreHosts对象不可用时导致程序崩溃。 重构了DNS、IPv4和IPv6配置部分,使用DccRepeater进行动态项管理,替代了手动 创建和销毁组件的方式。这简化了代码结构,并消除了单独跟踪已创建项的需要。 添加了addAddressData()和removeAddressData()辅助函数来集中数据操作逻辑。 在IPv6验证逻辑中,为网关值添加了空值检查,防止网关字段为空时出现错误。这 确保了IPv6配置的正确验证。 Log: 修复系统代理页面显示问题并改进DNS/IP配置管理 Influence: 1. 测试系统代理设置,确保"忽略主机"选项正确显示 2. 验证DNS配置,测试添加和删除DNS服务器 3. 测试IPv4配置,添加多个IP地址 4. 测试IPv6配置,包括空和已填充的网关字段 5. 验证所有网络配置更改是否正确保存 6. 检查修改网络设置时UI是否正确更新 --- dcc-network/qml/PageSystemProxy.qml | 2 +- dcc-network/qml/SectionDNS.qml | 164 ++++++++---------- dcc-network/qml/SectionIPv4.qml | 39 ++--- dcc-network/qml/SectionIPv6.qml | 56 +++--- .../networkmanagerprocesser.cpp | 2 +- 5 files changed, 112 insertions(+), 151 deletions(-) diff --git a/dcc-network/qml/PageSystemProxy.qml b/dcc-network/qml/PageSystemProxy.qml index 00e6ce26..15128c1d 100644 --- a/dcc-network/qml/PageSystemProxy.qml +++ b/dcc-network/qml/PageSystemProxy.qml @@ -249,7 +249,7 @@ DccObject { parentName: root.name + "/menu/body" displayName: qsTr("Ignore the proxy configurations for the above hosts and domains") weight: 90 - visible: ignoreHosts.visibleToApp + visible: ignoreHosts && ignoreHosts.visibleToApp pageType: DccObject.Item page: Label { text: dccObj.displayName diff --git a/dcc-network/qml/SectionDNS.qml b/dcc-network/qml/SectionDNS.qml index 0acf673f..a4775115 100644 --- a/dcc-network/qml/SectionDNS.qml +++ b/dcc-network/qml/SectionDNS.qml @@ -106,6 +106,16 @@ DccObject { console.log("[DNS-Check] All DNS entries validated successfully") return true } + function addAddressData(addr) { + config.push(addr) + configChanged() + editClicked() + } + function removeAddressData(index) { + config.splice(index, 1) + configChanged() + editClicked() + } name: "dnsTitle" displayName: qsTr("DNS") @@ -153,105 +163,81 @@ DccObject { canSearch: false pageType: DccObject.Item page: DccGroupView {} - } - Component { - id: dnsComponent - DccObject { - id: dnsItem - property int index: 0 - weight: root.weight + 30 + index - name: "dns" + index - displayName: qsTr("DNS") + (index + 1) - parentName: root.parentName + "/dnsGroup" - canSearch: false - pageType: DccObject.Editor - page: RowLayout { - D.LineEdit { - text: root.config[index] - // 移除正则验证器,改用手动验证以支持IPv6 - // 显式允许所有字符输入,包括冒号 - inputMethodHints: Qt.ImhNone - onTextChanged: { - console.log("[DNS-Input] Text changed in DNS field", index, ":", text) - if (showAlert) { - errorKey = "" + DccRepeater { + model: root.config + delegate: DccObject { + id: dnsItem + weight: root.weight + 30 + index + name: "dns" + index + displayName: qsTr("DNS") + (index + 1) + parentName: root.parentName + "/dnsGroup" + canSearch: false + pageType: DccObject.Editor + page: RowLayout { + D.LineEdit { + text: root.config[index] + // 移除正则验证器,改用手动验证以支持IPv6 + // 显式允许所有字符输入,包括冒号 + inputMethodHints: Qt.ImhNone + onTextChanged: { + console.log("[DNS-Input] Text changed in DNS field", index, ":", text) + if (showAlert) { + errorKey = "" + } + if (text !== root.config[index]) { + console.log("[DNS-Input] Updating config[" + index + "] from", root.config[index], "to", text) + root.config[index] = text + root.editClicked() + } } - if (text !== root.config[index]) { - console.log("[DNS-Input] Updating config[" + index + "] from", root.config[index], "to", text) - root.config[index] = text - root.editClicked() + showAlert: errorKey === dccObj.name + alertDuration: 2000 + alertText: qsTr("Invalid IP address") + onShowAlertChanged: { + if (showAlert) { + DccApp.showPage(dccObj) + forceActiveFocus() + } } } - showAlert: errorKey === dccObj.name - alertDuration: 2000 - alertText: qsTr("Invalid IP address") - onShowAlertChanged: { - if (showAlert) { - DccApp.showPage(dccObj) - forceActiveFocus() + D.IconLabel { + Layout.margins: 0 + Layout.maximumHeight: 16 + visible: isEdit && root.config.length < 3 + // enabled: root.config.length < 3 + icon { + name: "list_add" + width: 16 + height: 16 } - } - } - D.IconLabel { - Layout.margins: 0 - Layout.maximumHeight: 16 - visible: isEdit && root.config.length < 3 - // enabled: root.config.length < 3 - icon { - name: "list_add" - width: 16 - height: 16 - } - MouseArea { - anchors.fill: parent - acceptedButtons: Qt.LeftButton - onClicked: { - root.config.push("") - root.configChanged() + MouseArea { + anchors.fill: parent + acceptedButtons: Qt.LeftButton + onClicked: { + root.addAddressData("") + } } } - } - D.IconLabel { - Layout.margins: 0 - Layout.maximumHeight: 16 - visible: isEdit && root.config.length > 2 - // enabled: root.config.length > 2 - icon { - name: "list_delete" - width: 16 - height: 16 - } - MouseArea { - anchors.fill: parent - acceptedButtons: Qt.LeftButton - onClicked: { - root.config.splice(index, 1) - root.configChanged() + D.IconLabel { + Layout.margins: 0 + Layout.maximumHeight: 16 + visible: isEdit && root.config.length > 2 + // enabled: root.config.length > 2 + icon { + name: "list_delete" + width: 16 + height: 16 + } + MouseArea { + anchors.fill: parent + acceptedButtons: Qt.LeftButton + onClicked: { + root.removeAddressData(index) + } } } } } } } - function addIpItem() { - let dnsItem = dnsComponent.createObject(root, { - "index": dnsItems.length - }) - DccApp.addObject(dnsItem) - dnsItems.push(dnsItem) - } - function removeIpItem() { - let tmpItem = dnsItems.pop() - DccApp.removeObject(tmpItem) - tmpItem.destroy() - } - - onConfigChanged: { - while (root.config.length > dnsItems.length) { - addIpItem() - } - while (root.config.length < dnsItems.length) { - removeIpItem() - } - } } diff --git a/dcc-network/qml/SectionIPv4.qml b/dcc-network/qml/SectionIPv4.qml index 3811be6f..ec390032 100644 --- a/dcc-network/qml/SectionIPv4.qml +++ b/dcc-network/qml/SectionIPv4.qml @@ -131,6 +131,16 @@ DccObject { addressData = [] } } + function addAddressData(addr) { + addressData.push(addr) + addressDataChanged() + editClicked() + } + function removeAddressData(index) { + addressData.splice(index, 1) + addressDataChanged() + editClicked() + } name: "ipv4Title" displayName: qsTr("IPv4") @@ -246,11 +256,10 @@ DccObject { } } } - Component { - id: ipComponent - DccObject { + DccRepeater { + model: root.addressData + delegate: DccObject { id: ipv4Item - property int index: 0 weight: root.weight + 30 + index name: "ipv4_" + index displayName: "IP-" + index @@ -290,9 +299,7 @@ DccObject { anchors.fill: parent acceptedButtons: Qt.LeftButton onClicked: { - root.addressData.push(["0.0.0.0", "255.255.255.0", ""]) - root.addressDataChanged() - root.editClicked() + root.addAddressData(["0.0.0.0", "255.255.255.0", ""]) } } } @@ -309,9 +316,7 @@ DccObject { anchors.fill: parent acceptedButtons: Qt.LeftButton onClicked: { - root.addressData.splice(index, 1) - root.addressDataChanged() - root.editClicked() + root.removeAddressData(index) } } } @@ -424,18 +429,4 @@ DccObject { } } } - onAddressDataChanged: { - while (addressData.length > ipItems.length) { - let ipItem = ipComponent.createObject(root, { - "index": ipItems.length - }) - DccApp.addObject(ipItem) - ipItems.push(ipItem) - } - while (addressData.length < ipItems.length) { - let tmpItem = ipItems.pop() - DccApp.removeObject(tmpItem) - tmpItem.destroy() - } - } } diff --git a/dcc-network/qml/SectionIPv6.qml b/dcc-network/qml/SectionIPv6.qml index 0a97cddf..e810069d 100644 --- a/dcc-network/qml/SectionIPv6.qml +++ b/dcc-network/qml/SectionIPv6.qml @@ -90,7 +90,7 @@ DccObject { return false } // 检查网关 - if (gateway[k].length !== 0) { + if (gateway[k] && gateway[k].length !== 0) { gatewayCount++ if (gatewayCount >= 2) { errorKey = k + "gateway" @@ -98,7 +98,7 @@ DccObject { return false } } - if (gateway[k].length !== 0 && !NetUtils.ipv6RegExp.test(gateway[k])) { + if (gateway[k] && gateway[k].length !== 0 && !NetUtils.ipv6RegExp.test(gateway[k])) { errorKey = k + "gateway" errorMsg = qsTr("Invalid gateway") return false @@ -121,6 +121,16 @@ DccObject { addressData = [] } } + function addAddressData(addr) { + addressData.push(addr) + addressDataChanged() + editClicked() + } + function removeAddressData(index) { + addressData.splice(index, 1) + addressDataChanged() + editClicked() + } ListModel { id: allModel @@ -247,11 +257,10 @@ DccObject { } } } - Component { - id: ipComponent - DccObject { + DccRepeater { + model: root.addressData + delegate: DccObject { id: ipv6Item - property int index: 0 weight: root.weight + 30 + index name: "ipv6_" + index visible: root.visible @@ -292,12 +301,10 @@ DccObject { anchors.fill: parent acceptedButtons: Qt.LeftButton onClicked: { - root.addressData.push({ - "address": "", - "prefix": 64 - }) - root.addressDataChanged() - root.editClicked() + root.addAddressData({ + "address": "", + "prefix": 64 + }) } } } @@ -314,9 +321,7 @@ DccObject { anchors.fill: parent acceptedButtons: Qt.LeftButton onClicked: { - root.addressData.splice(index, 1) - root.addressDataChanged() - root.editClicked() + root.removeAddressData(index) } } } @@ -434,25 +439,4 @@ DccObject { } } } - function addIpItem() { - let ipItem = ipComponent.createObject(root, { - "index": ipItems.length - }) - DccApp.addObject(ipItem) - ipItems.push(ipItem) - } - function removeIpItem() { - let tmpItem = ipItems.pop() - DccApp.removeObject(tmpItem) - tmpItem.destroy() - } - - onAddressDataChanged: { - while (root.addressData.length > ipItems.length) { - addIpItem() - } - while (root.addressData.length < ipItems.length) { - removeIpItem() - } - } } diff --git a/src/impl/networkmanager/networkmanagerprocesser.cpp b/src/impl/networkmanager/networkmanagerprocesser.cpp index 3929c7f9..a8b1d0f6 100644 --- a/src/impl/networkmanager/networkmanagerprocesser.cpp +++ b/src/impl/networkmanager/networkmanagerprocesser.cpp @@ -381,7 +381,7 @@ void NetworkManagerProcesser::checkConnectivityFinished(quint32 conntity) void NetworkManagerProcesser::onConnectivityChanged(NetworkManager::Connectivity conntity) { - dde::network::Connectivity ctity; + dde::network::Connectivity ctity = m_connectivity; switch (conntity) { case NetworkManager::Connectivity::Full: { ctity = dde::network::Connectivity::Full;