Skip to content

fix(storage): compare size by raw bytes instead of localized float - #715

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:develop/eaglefrom
add-uos:develop/eagle
Jul 28, 2026
Merged

fix(storage): compare size by raw bytes instead of localized float#715
deepin-bot[bot] merged 1 commit into
linuxdeepin:develop/eaglefrom
add-uos:develop/eagle

Conversation

@add-uos

@add-uos add-uos commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Extract raw byte count before brackets for accurate comparison across locales with different thousand separators (comma/dot).

提取方括号前的原始字节数进行比较,解决不同locale下千分位
分隔符(逗号/点)导致的大小比较错误。

Log: 修复存储设备大小比较在非中文locale下出错的问题
PMS: 371721
Influence: 存储设备大小比较逻辑,影响smartctl解析的设备容量显示

Summary by Sourcery

Bug Fixes:

  • Fix incorrect storage size comparisons under non-Chinese locales by parsing the raw byte count before the bracket, independent of thousand separators.

Extract raw byte count before brackets for accurate comparison
across locales with different thousand separators (comma/dot).

提取方括号前的原始字节数进行比较,解决不同locale下千分位
分隔符(逗号/点)导致的大小比较错误。

Log: 修复存储设备大小比较在非中文locale下出错的问题
PMS: 371721
Influence: 存储设备大小比较逻辑,影响smartctl解析的设备容量显示
@sourcery-ai

sourcery-ai Bot commented Jul 28, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts storage size comparison to extract and compare raw byte counts from localized strings, avoiding float parsing and locale-dependent thousand separators.

File-Level Changes

Change Details Files
Compare storage capacities using integer raw byte counts extracted from localized strings instead of localized floats within brackets.
  • Replace previous QRegExp that extracted a float from the bracketed capacity with a new QRegExp that captures the leading raw byte count before the '[' character, optionally followed by the word 'bytes'
  • Normalize the captured raw byte count by removing both comma and dot thousand separators and converting the result to an unsigned 64-bit integer
  • Introduce a local lambda helper extractBytes to encapsulate the parsing and normalization logic
  • Change comparison logic from a float-based epsilon comparison to a straightforward integer greater-than comparison using the extracted byte counts
deepin-devicemanager/src/DeviceManager/DeviceStorage.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

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:95分

■ 【总体评价】

代码通过提取原始字节数并转为整型比较,修复了多语言环境下的正则匹配失效和浮点数精度丢失问题
逻辑正确且代码质量良好,仅因使用已过时的QRegExp类扣5分

■ 【详细分析】

  • 1.语法逻辑(完全正确)✓

DeviceStorage::compareSize 函数中,新代码使用 lambda extractBytes 提取方括号前的数字,通过移除逗号和点号后转为 quint64 进行比较,正确处理了 de_DEzh_CN 等不同语言环境下的千分位分隔符问题,且避免了 float 类型的精度丢失。
潜在问题:如果原始字节数据中存在实际的小数(如 "1.5 bytes"),移除点号会导致数值错误放大,但根据注释中的实际数据格式,字节数均为整数,此风险极低。
建议:如果未来可能处理带小数的字节数,需增加对小数点的特殊处理逻辑;当前逻辑已满足现有需求。

  • 2.代码质量(良好)✓

代码结构清晰,使用 lambda 表达式封装了重复的提取逻辑,减少了代码冗余。注释详细说明了不同语言环境下的数据格式,有助于后续维护。使用 quint64 替代 float 更符合存储容量字节数的数据类型特征。
潜在问题:使用了 QRegExp 类,该类在 Qt6 中已被废弃,推荐使用 QRegularExpression
建议:将 QRegExp 替换为 QRegularExpression,以兼容 Qt6 并提升正则匹配效率。

  • 3.代码性能(无性能问题)✓

正则表达式匹配和字符串操作仅在比较时执行两次,开销极小。整型比较比浮点数比较效率更高,无性能瓶颈。
建议:无需优化。

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
本次代码修改仅涉及字符串解析和数值比较,无外部输入注入、内存越界等安全风险。

  • 建议:无需修复。

■ 【改进建议代码示例】

// 建议使用 QRegularExpression 替代 QRegExp 以兼容 Qt6
    // 提取原始字节数(方括号前的数字,千分位逗号/点)比较
    // zh_CN: "Total NVM Capacity:  1,024,209,543,168 [1.02 TB]"
    // de_DE: "Total NVM Capacity:  1.024.209.543.168 [1,02 TB]"
    // 格式2: "Total NVM Capacity:  240,057,409,536 bytes [240 GB]"
    auto extractBytes = [](const QString &str) -> quint64 {
        QRegularExpression reg("(\\d[\\d,.]*)\\s*(?:bytes\\s*)?\\[");
        QRegularExpressionMatch match = reg.match(str);
        if (match.hasMatch()) {
            QString raw = match.captured(1);
            raw.remove(",");
            raw.remove(".");
            return raw.toULongLong();
        }
        return 0;
    };

    quint64 bytes1 = extractBytes(size1);
    quint64 bytes2 = extractBytes(size2);

    // 返回较大值
    if (bytes1 > bytes2)
        return size1;
    else
        return size2;

@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.

Hey - I've left some high level feedback:

  • When extractBytes fails to find a match it returns 0, which can silently change comparison behavior; consider falling back to the original float-in-brackets logic or the original strings in that case instead of treating them as 0.
  • The regex (\d[\d,.]*)\s*(?:bytes\s*)?\[ is quite permissive and may match unintended numbers if the line format changes; consider anchoring it more tightly to the expected Total NVM Capacity/bytes patterns or at least limiting it to the last numeric block before the bracket.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- When `extractBytes` fails to find a match it returns 0, which can silently change comparison behavior; consider falling back to the original float-in-brackets logic or the original strings in that case instead of treating them as 0.
- The regex `(\d[\d,.]*)\s*(?:bytes\s*)?\[` is quite permissive and may match unintended numbers if the line format changes; consider anchoring it more tightly to the expected `Total NVM Capacity`/`bytes` patterns or at least limiting it to the last numeric block before the bracket.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: add-uos, lzwind

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

@add-uos

add-uos commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot

deepin-bot Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

This pr force merged! (status: unstable)

@deepin-bot
deepin-bot Bot merged commit a9a1cfa into linuxdeepin:develop/eagle Jul 28, 2026
17 of 20 checks passed
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.

3 participants