fix: fix SSID encoding issues with GBK/UTF-8 detection#549
fix: fix SSID encoding issues with GBK/UTF-8 detection#549caixr23 wants to merge 1 commit intolinuxdeepin:masterfrom
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: caixr23 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 |
Reviewer's GuideIntroduces a shared decodeByteArray() utility to robustly decode SSIDs from raw byte arrays using UTF-8 with GBK fallback, and wires it into the NetworkManager-based Wi‑Fi backend wherever SSIDs are read or compared, while also updating copyright years. Sequence diagram for SSID retrieval with UTF-8/GBK decodingsequenceDiagram
actor User
participant UI as WifiUI
participant WDM as WirelessDeviceManagerRealize
participant WD as NetworkManager_WirelessDevice
participant AP as NetworkManager_AccessPointPtr
participant NU as NetUtils
User ->> UI: Open wifi list
UI ->> WDM: activeAp()
WDM ->> WD: activeAccessPoint()
WD -->> WDM: AP
alt access point exists
WDM ->> AP: rawSsid()
AP -->> WDM: QByteArray rawSsid
WDM ->> NU: decodeByteArray(rawSsid)
NU -->> WDM: QString decodedSsid
WDM -->> UI: decodedSsid
UI -->> User: Display correct SSID
else no access point
WDM -->> UI: empty QString
UI -->> User: No active wifi
end
Class diagram for SSID decoding integrationclassDiagram
class NetUtils {
+QString decodeByteArray(QByteArray data)
+Connectivity connectivityValue(uint sourceConnectivity)
+DeviceStatus convertDeviceStatus(int sourceDeviceStatus)
+ConnectionStatus convertConnectionStatus(int sourceConnectionStatus)
+ConnectionStatus convertStateFromNetworkManager(int state)
}
class AccessPointProxyNM {
+QString ssid() const
+bool contains(QString uni) const
+void initState()
+void updateHiddenInfo()
-NetworkManager_WirelessNetworkPtr m_network
}
class NetworkDetailNMRealize {
+void initProperties()
-QString ssid
}
class WirelessDeviceManagerRealize {
+QString activeAp() const
+bool hotspotEnabled()
+void addNetwork(NetworkManager_WirelessNetworkPtr network)
-QList~AccessPointInfo_ptr~ m_accessPointInfos
-QSharedPointer~NetworkManager_WirelessDevice~ m_device
}
class NetworkManager_WirelessNetworkPtr {
+QString ssid() const
+NetworkManager_AccessPointPtr referenceAccessPoint() const
}
class NetworkManager_AccessPointPtr {
+QByteArray rawSsid() const
+QString ssid() const
+int frequency() const
}
class NetworkManager_WirelessDevice {
+NetworkManager_AccessPointPtr activeAccessPoint() const
}
class AccessPointInfo {
+NetworkManager_AccessPointPtr accessPoint() const
}
NetUtils <.. AccessPointProxyNM : uses
NetUtils <.. NetworkDetailNMRealize : uses
NetUtils <.. WirelessDeviceManagerRealize : uses
AccessPointProxyNM --> NetworkManager_WirelessNetworkPtr : m_network
NetworkManager_WirelessNetworkPtr --> NetworkManager_AccessPointPtr : referenceAccessPoint
WirelessDeviceManagerRealize --> NetworkManager_WirelessDevice : m_device
WirelessDeviceManagerRealize --> AccessPointInfo : manages
AccessPointInfo --> NetworkManager_AccessPointPtr : accessPoint
NetworkDetailNMRealize --> NetworkManager_WirelessDevice : uses
NetworkDetailNMRealize --> NetworkManager_AccessPointPtr : uses
Flow diagram for decodeByteArray SSID decoding logicflowchart TD
A[Start decodeByteArray] --> B{data is empty?}
B -- Yes --> C[Return empty QString]
B -- No --> D[Create utf8Decoder]
D --> E[Check utf8Decoder isValid]
E --> F[Decode data with utf8Decoder to utf8Result]
F --> G{isValidUtf8 AND utf8Result has no U+FFFD?}
G -- Yes --> H[Return utf8Result]
G -- No --> I[Create gbkDecoder with GBK]
I --> J[Decode data with gbkDecoder]
J --> K[Return GBK decoded QString]
C --> L[End]
H --> L
K --> L
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 found 2 issues, and left some high level feedback:
- In decodeByteArray(),
isValidUtf8is captured before any decoding is done, so the validity check never reflects the UTF-8 decode result; consider checkingutf8Decoder.isValid()after calling the decoder (and only then inspecting the replacement-character heuristic) before deciding to fall back to GBK. - When constructing the GBK decoder (
QStringDecoder gbkDecoder("GBK")), it would be safer to verifygbkDecoder.isValid()and define a fallback (e.g.QString::fromLatin1or returning the UTF-8 result) in case the GBK codec is not available, to avoid returning an empty or invalid string. - In WirelessDeviceManagerRealize::addNetwork(), the comparison mixes
accessPoint()->ssid()(still using NetworkManager’s decoding) withdecodeByteArray(network->referenceAccessPoint()->rawSsid()); for consistency with the rest of the changes, consider normalizing both sides through the same decoding path or exposing a helper that returns a consistently decoded SSID.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In decodeByteArray(), `isValidUtf8` is captured before any decoding is done, so the validity check never reflects the UTF-8 decode result; consider checking `utf8Decoder.isValid()` after calling the decoder (and only then inspecting the replacement-character heuristic) before deciding to fall back to GBK.
- When constructing the GBK decoder (`QStringDecoder gbkDecoder("GBK")`), it would be safer to verify `gbkDecoder.isValid()` and define a fallback (e.g. `QString::fromLatin1` or returning the UTF-8 result) in case the GBK codec is not available, to avoid returning an empty or invalid string.
- In WirelessDeviceManagerRealize::addNetwork(), the comparison mixes `accessPoint()->ssid()` (still using NetworkManager’s decoding) with `decodeByteArray(network->referenceAccessPoint()->rawSsid())`; for consistency with the rest of the changes, consider normalizing both sides through the same decoding path or exposing a helper that returns a consistently decoded SSID.
## Individual Comments
### Comment 1
<location path="src/netutils.cpp" line_range="85-87" />
<code_context>
+ return QString();
+ }
+
+ QStringDecoder utf8Decoder(QStringDecoder::Utf8);
+ bool isValidUtf8 = utf8Decoder.isValid();
+ QString utf8Result = utf8Decoder(data);
+ if (isValidUtf8 && !utf8Result.contains(QChar(0xFFFD))) {
+ return utf8Result;
</code_context>
<issue_to_address>
**issue (bug_risk):** UTF-8 validity check is done before decoding, which doesn’t reflect whether the input bytes are valid.
`utf8Decoder.isValid()` right after construction only reports that the codec exists, not that `data` is valid UTF‑8. To actually detect invalid input, you need to inspect the decoder after decoding (e.g., call `isValid()` after `utf8Decoder(data)` or use another Qt API for error reporting). As written, `isValidUtf8` is effectively constant and doesn’t help detect bad input.
</issue_to_address>
### Comment 2
<location path="src/netutils.cpp" line_range="91-92" />
<code_context>
+ if (isValidUtf8 && !utf8Result.contains(QChar(0xFFFD))) {
+ return utf8Result;
+ }
+ QStringDecoder gbkDecoder("GBK");
+ return gbkDecoder.decode(data);
+}
}
</code_context>
<issue_to_address>
**suggestion:** GBK decoder is not checked for validity or given a fallback if the codec is unavailable.
`QStringDecoder("GBK")` can be invalid if the codec isn’t available, in which case `decode()` may yield empty or corrupted output. Please guard with `gbkDecoder.isValid()` and, if false, fall back to a safer alternative such as `QString::fromLatin1(data)` or returning the UTF‑8 result instead.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| QStringDecoder utf8Decoder(QStringDecoder::Utf8); | ||
| bool isValidUtf8 = utf8Decoder.isValid(); | ||
| QString utf8Result = utf8Decoder(data); |
There was a problem hiding this comment.
issue (bug_risk): UTF-8 validity check is done before decoding, which doesn’t reflect whether the input bytes are valid.
utf8Decoder.isValid() right after construction only reports that the codec exists, not that data is valid UTF‑8. To actually detect invalid input, you need to inspect the decoder after decoding (e.g., call isValid() after utf8Decoder(data) or use another Qt API for error reporting). As written, isValidUtf8 is effectively constant and doesn’t help detect bad input.
| QStringDecoder gbkDecoder("GBK"); | ||
| return gbkDecoder.decode(data); |
There was a problem hiding this comment.
suggestion: GBK decoder is not checked for validity or given a fallback if the codec is unavailable.
QStringDecoder("GBK") can be invalid if the codec isn’t available, in which case decode() may yield empty or corrupted output. Please guard with gbkDecoder.isValid() and, if false, fall back to a safer alternative such as QString::fromLatin1(data) or returning the UTF‑8 result instead.
413cf23 to
ff6e6c5
Compare
1. Add decodeByteArray() utility function to handle mixed GBK and UTF-8 encoded SSIDs 2. Replace direct ssid() calls with decodeByteArray(rawSsid()) in NetworkManager backend 3. Update copyright years to 2026 in affected files 4. Fix potential SSID mismatch issues when comparing wireless settings with network SSIDs The NetworkManager Qt API's ssid() method may return incorrectly decoded strings when access points use GBK encoding (common with Chinese routers). This change introduces intelligent encoding detection that tries UTF-8 first, then falls back to GBK if invalid characters are detected, ensuring proper display and matching of SSIDs with non-ASCII characters. Influence: 1. Test WiFi scanning with access points using Chinese SSID names (GBK encoded) 2. Test WiFi scanning with access points using UTF-8 encoded SSIDs 3. Verify SSID matching for saved connections with non-ASCII characters 4. Test hidden network detection with various SSID encodings 5. Verify active connection status display shows correct SSID names 6. Test hotspot functionality with non-ASCII SSID names fix: 修复 SSID 编码问题,支持 GBK/UTF-8 自动检测 1. 添加 decodeByteArray() 工具函数处理混合 GBK 和 UTF-8 编码的 SSID 2. 在 NetworkManager 后端中将直接调用 ssid() 替换为 decodeByteArray(rawSsid()) 3. 更新受影响文件的版权年份至 2026 4. 修复无线设置与网络 SSID 比较时可能出现的编码不匹配问题 NetworkManager Qt API 的 ssid() 方法在接入点使用 GBK 编码时(常见于中文 路由器) 可能返回错误解码的字符串。此变更引入智能编码检测,优先尝试 UTF-8 解码, 检测到无效字符时回退到 GBK 解码,确保非 ASCII 字符的 SSID 正确显示和 匹配。 Influence: 1. 测试使用中文 SSID 名称(GBK 编码)的 WiFi 扫描功能 2. 测试使用 UTF-8 编码 SSID 的 WiFi 扫描功能 3. 验证非 ASCII 字符的已保存连接 SSID 匹配功能 4. 测试各种 SSID 编码下的隐藏网络检测功能 5. 验证活动连接状态显示正确的 SSID 名称 6. 测试使用非 ASCII SSID 名称的热点功能 PMS: BUG-357843
deepin pr auto review这段代码主要针对无线网络SSID的显示进行了改进,从直接使用NetworkManager返回的字符串改为手动解码原始字节数组,以支持非UTF-8编码(如GBK)的SSID。 以下是对代码的详细审查意见: 1. 语法逻辑
2. 代码质量
3. 代码性能
4. 代码安全
总结与改进建议总体评价: 具体改进建议:
|
|
TAG Bot New tag: 2.0.89 |
The NetworkManager Qt API's ssid() method may return incorrectly decoded strings when
access points use GBK encoding (common with Chinese routers). This change introduces
intelligent encoding detection that tries UTF-8 first, then falls back to GBK if
invalid characters are detected, ensuring proper display and matching of SSIDs
with non-ASCII characters.
Influence:
fix: 修复 SSID 编码问题,支持 GBK/UTF-8 自动检测
NetworkManager Qt API 的 ssid() 方法在接入点使用 GBK 编码时(常见于中文 路由器)
可能返回错误解码的字符串。此变更引入智能编码检测,优先尝试 UTF-8 解码,
检测到无效字符时回退到 GBK 解码,确保非 ASCII 字符的 SSID 正确显示和
匹配。
Influence:
PMS: BUG-357843
Summary by Sourcery
Improve SSID handling by introducing a shared decoding utility that correctly interprets mixed GBK/UTF-8 SSIDs and applying it across the NetworkManager backend.
New Features:
Bug Fixes:
Enhancements:
Chores: