Skip to content

fix: add GBK fallback for SSID decoding - #627

Closed
mhduiy wants to merge 1 commit into
masterfrom
agent/pms-bug-bot/6938b596f7fb
Closed

mhduiy wants to merge 1 commit into
masterfrom
agent/pms-bug-bot/6938b596f7fb

Conversation

@mhduiy

@mhduiy mhduiy commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Root Cause Analysis

KDE networkmanager-qt hardcodes QString::fromUtf8() to decode SSID raw bytes (accesspoint.cpp:206). Since the 802.11 standard does not mandate SSID encoding, GBK-encoded SSIDs fail UTF-8 decoding and produce Unicode replacement characters (U+FFFD), causing mojibake. In dde-network-core, three entry points (accesspointproxynm.cpp:58-60, devicemanagerrealize.cpp:397, hotspotcontrollernm.cpp:69) directly pass through or repeat this fromUtf8() anti-pattern without GBK fallback.

Fix

Added a decodeSsid(const QByteArray &) utility function in src/netutils.h/src/netutils.cpp that attempts UTF-8 decoding first, falls back to GBK when UTF-8 fails, and finally falls back to fromUtf8 result if GBK is also unavailable. Replaced all 3 SSID decoding entry points to use decodeSsid() instead of raw fromUtf8() / QString(QByteArray).

Change Safety Assessment

Code Safety

  • Risk Level: Low
  • All 3 modified entry points were introduced in the initial commit (not prior bug-fix products), so this change does not revert any historical fix.
  • decodeSsid() is a pure new function; UTF-8 SSIDs follow the exact same code path as before — GBK fallback only triggers when UTF-8 decoding fails.

Business Impact Scope

Affected functional modules: WiFi network list display (SSID rendering), hotspot SSID display, and device SSID query. User scenarios: connecting to or viewing WiFi networks with GBK-encoded SSIDs will now display correctly instead of showing garbled characters. UTF-8-encoded SSIDs are unaffected.

Verification Suggestion

  1. Verify WiFi list displays UTF-8-encoded SSIDs correctly (regression check).
  2. Verify WiFi list displays GBK-encoded SSIDs correctly (fix verification).
  3. Verify hotspot SSID display and device SSID query work as expected.

根因分析

KDE networkmanager-qt 在 accesspoint.cpp:206 硬编码使用 QString::fromUtf8() 解码 SSID 原始字节。由于 802.11 标准未规定 SSID 编码格式,GBK 编码的 SSID 无法通过 UTF-8 解码,产生 Unicode 替换字符(U+FFFD)导致乱码。在 dde-network-core 中,3 处入口点(accesspointproxynm.cpp:58-60、devicemanagerrealize.cpp:397、hotspotcontrollernm.cpp:69)直接透传或重复此 fromUtf8() 行为,无 GBK 回退。

修复方案

在 src/netutils.h/src/netutils.cpp 中新增 decodeSsid(const QByteArray &) 工具函数,优先 UTF-8 解码,失败时回退 GBK,GBK 不可用时回退 fromUtf8 结果。替换全部 3 处 SSID 解码入口点,使用 decodeSsid() 替代原始 fromUtf8() / QString(QByteArray)。

改动安全评估

代码安全评估

  • 风险等级: 低
  • 3 处修改入口点均在初始提交中引入(非历史 bug 修复产物),本次修改不撤销任何已有修复。
  • decodeSsid() 为纯新增函数;UTF-8 编码 SSID 走与原来完全相同的路径——GBK 回退仅在 UTF-8 解码失败时触发。

业务影响范围

受影响功能模块:WiFi 网络列表显示(SSID 渲染)、热点 SSID 显示、设备 SSID 查询。用户场景:连接或查看使用 GBK 编码 SSID 的 WiFi 网络时,名称将正常显示而非乱码。UTF-8 编码 SSID 不受影响。

验证建议

  1. 验证 UTF-8 编码 SSID 的网络列表显示正常(回归检查)。
  2. 验证 GBK 编码 SSID 的网络列表显示正常(修复验证)。
  3. 验证热点 SSID 显示和设备 SSID 查询功能正常。

Summary by Sourcery

Fix SSID encoding handling by adding UTF-8 decoding with GBK fallback and preserving raw SSID bytes across network operations.

Bug Fixes:

  • Correct SSID rendering for GBK-encoded network names while preserving existing UTF-8 behavior.
  • Preserve raw SSID bytes when configuring wireless connections.

Enhancements:

  • Centralize SSID decoding and expose raw SSID data across access-point interfaces for consistent handling.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @mhduiy, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 5 hours and 40 minutes by commenting @sourcery-ai review. Upgrade to get a review now.

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: mhduiy

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

@sourcery-ai

sourcery-ai Bot commented Sep 14, 2026

Copy link
Copy Markdown

Reviewer's Guide

Introduces a shared SSID decoder that retains UTF-8 behavior while falling back to GBK for invalid UTF-8 bytes, then applies it to access-point, device, and hotspot SSID paths so GBK-encoded names render correctly.

Flow diagram for SSID decoding with GBK fallback

flowchart TD
    RawSSID[Raw SSID bytes] --> Decode["decodeSsid(rawSsid)"]
    Decode --> UTF8{UTF-8 valid?}
    UTF8 -->|Yes| UTF8Result[Return UTF-8 text]
    UTF8 -->|No| GBK{GBK decoder valid?}
    GBK -->|Yes| GBKResult[Return GBK text]
    GBK -->|No| Fallback[Return QString::fromUtf8 result]
Loading

File-Level Changes

Change Details Files
Centralize SSID decoding with UTF-8-first and GBK fallback behavior.
  • Added a shared decoder that detects UTF-8 errors, attempts GBK decoding, and preserves the prior UTF-8 fallback when GBK is unavailable.
  • Exposed the decoder through the network utilities interface.
src/netutils.cpp
src/netutils.h
Route all affected NetworkManager SSID consumers through the shared decoder.
  • Decode raw access-point SSIDs before network list rendering.
  • Use fallback decoding for device SSID JSON output.
  • Use fallback decoding for hotspot SSID JSON output.
src/impl/networkmanager/accesspointproxynm.cpp
src/impl/networkmanager/devicemanagerrealize.cpp
src/impl/networkmanager/hotspotcontrollernm.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

…n save

1. Root cause: networkmanager-qt hardcodes fromUtf8() to decode SSID
   raw bytes, GBK-encoded SSIDs produce replacement chars causing mojibake.
   Additionally, the save path converts decoded SSID back via toUtf8(),
   mismatching the AP's original bytes and breaking auto-connect for GBK SSIDs.
2. Fix: add decodeSsid() utility in netutils with UTF-8 first and GBK
   fallback, replace 3 SSID decoding entry points. Add rawSsid() virtual
   method to AccessPointProxy to expose original SSID bytes, implement in
   AccessPointProxyNM and AccessPointsProxyInter. Change save path in
   devicemanagerrealize.cpp to use rawSsid() instead of ssid().toUtf8(),
   preserving original bytes (consistent with nmtui behavior).
3. Impact: UTF-8 SSIDs unchanged, GBK SSIDs now display correctly and
   auto-connect works, no API signature changes beyond new rawSsid() method

Log: Fix garbled WiFi network names with GBK-encoded SSIDs and auto-connect failure

Influence:
1. Test WiFi list display with UTF-8 encoded SSIDs
2. Test WiFi list display with GBK encoded SSIDs
3. Test WiFi auto-connect with GBK encoded SSIDs
4. Test hotspot SSID display and device SSID query

fix: 修复GBK编码SSID显示乱码及保存后自动连接失败问题

1. 根因:networkmanager-qt 硬编码 fromUtf8() 解码 SSID 原始字节,
   GBK编码SSID产生替换字符导致乱码。保存路径通过 toUtf8() 将解码后
   的SSID转回字节,与AP原始字节不匹配,导致GBK编码SSID自动连接失败。
2. 方案:在 netutils 中新增 decodeSsid() 工具函数,UTF-8优先解码
   失败时回退GBK,替换3处SSID解码入口点。在 AccessPointProxy 基类
   新增 rawSsid() 虚方法暴露原始SSID字节,AccessPointProxyNM 和
   AccessPointsProxyInter 分别实现。保存路径改用 rawSsid() 替代
   ssid().toUtf8(),保留原始字节(与 nmtui 行为一致)。
3. 影响:UTF-8编码SSID行为不变,GBK编码SSID正常显示且自动连接正常,
   除新增 rawSsid() 方法外不影响API签名

Log: 修复GBK编码WiFi网络名称显示乱码及保存后自动连接失败问题

Influence:
1. 测试UTF-8编码SSID的网络列表显示
2. 测试GBK编码SSID的网络列表显示
3. 测试GBK编码SSID的自动连接
4. 测试热点SSID显示和设备SSID查询

PMS: BUG-357843
@caixr23
caixr23 force-pushed the agent/pms-bug-bot/6938b596f7fb branch from 147dbeb to 6e8705f Compare September 17, 2026 04:01
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 97 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 总体评分 97 分,大于 70 分通过阈值,代码质量符合要求。本次变更通过新增 decodeSsid() 工具函数实现 SSID 解码的 GBK 回退机制,修复了 GBK 编码 SSID 乱码问题,代码逻辑正确,无安全漏洞。

🔍 详细分析

1. 语法逻辑 ✅

评价: 优秀 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: 语法正确,逻辑清晰,无需修改


2. 代码质量 ✅

评价: 良好 ✅ 通过

潜在问题:

  1. src/netutils.cpp:79 - decodeSsid() 函数缺少注释说明 UTF-8→GBK→fromUtf8 回退策略
  2. src/netutils.h:15 - 头文件中不必要的 #include ,应仅保留在 .cpp 文件中

建议: 1.为 decodeSsid() 函数添加简要注释,说明回退策略和使用场景
2.将 #include 从 netutils.h 移至 netutils.cpp


3. 代码性能 ✅

评价: 优秀 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: 性能良好,资源使用合理,decodeSsid() 时间复杂度 O(n),QStringDecoder 创建开销极小,SSID 长度通常不超过 32 字节,无性能瓶颈


4. 代码安全 🔒

评价: 优秀 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: 安全合规,无安全漏洞。decodeSsid() 通过 Qt 的 QStringDecoder API 安全处理编码转换,无缓冲区溢出风险;SSID 为公开网络标识符,不涉及敏感信息;rawSsid() 暴露的原始字节不包含敏感数据


💡 改进建议代码示例

// src/netutils.cpp - 建议为 decodeSsid 添加注释

// src/netutils.h - 移除不必要的 include
// 移除: #include <QStringDecoder>
// 仅在 netutils.cpp 中保留该 include

/**
 * 解码 SSID 原始字节
 * 优先使用 UTF-8 解码,失败时回退到 GBK 编码,
 * GBK 编解码器不可用时回退到 QString::fromUtf8() 结果。
 * 用于修复 GBK 编码 SSID 在 UTF-8 解码时产生乱码的问题。
 */
QString decodeSsid(const QByteArray &rawSsid)
{
    QStringDecoder utf8Decoder(QStringDecoder::Utf8);
    QString result = utf8Decoder.decode(rawSsid);
    if (!utf8Decoder.hasError())
        return result;

    QStringDecoder gbkDecoder("GBK");
    if (gbkDecoder.isValid())
        return gbkDecoder.decode(rawSsid);

    return QString::fromUtf8(rawSsid);
}

本报告由 AI 代码审查工具自动生成

@mhduiy

mhduiy commented Sep 21, 2026

Copy link
Copy Markdown
Contributor Author

问题不复现,中间版本已经修复了

@mhduiy mhduiy closed this Sep 21, 2026
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.

2 participants