fix(device): correct disk size unit conversion from KB to MB in Comma…#118
Conversation
There was a problem hiding this comment.
Sorry @pppanghu77, you have reached your weekly rate limit of 500000 diff characters.
Please try again later or upgrade to continue using Sourcery
…ndLsblkParse() - Fix disk size calculation by dividing by 1024 twice (KB to MB) instead of multiplying by 1024 then dividing by 1024, which produced incorrect values 修复(device): 修正 CommandLsblkParse 中磁盘容量 KB 到 MB 的单位转换 - 修正磁盘容量计算,将 KB 到 MB 的转换从先乘 1024 再除 1024 修正为连续除以两次 1024,避免计算结果错误 Log: 修正 lsblk 解析中磁盘容量单位转换逻辑,将错误的乘除组合修正为连续除法以得到正确的 MB 值 Bug: https://pms.uniontech.com/bug-view-349331.html
deepin pr auto review你好!我是CodeGeeX。我已仔细审查了你提供的Git Diff,下面我将从语法逻辑、代码质量、代码性能和代码安全四个维度对这次代码变更进行详细的分析和评估。 变更概述本次修改主要包含两处:
1. 语法与逻辑
2. 代码质量
3. 代码性能
4. 代码安全
改进建议与重构代码综合以上分析,我为你提供一份优化后的代码参考。主要改进点在于:引入常量消除魔法数字、使用位移优化性能、增加正则转换的合法性校验。 // 建议在文件头或类内部定义常量,消除魔法数字
static const qint64 BYTES_PER_KB = 1024;
static const qint64 KB_PER_MB = 1024;
static const qint64 BYTES_PER_MB = BYTES_PER_KB * KB_PER_MB; // 1048576
// ... 在 CommandLsblkParse 函数中 ...
deviceCount++;
info.path = reg.cap(1);
info.label = reg.cap(2);
// 建议增加转换校验
bool ok = false;
qint64 bytesSize = reg.cap(3).toLongLong(&ok);
if (ok && bytesSize >= 0) {
// 使用右移20位替代除以 1024*1024,等价于 bytesSize / BYTES_PER_MB
// 右移10位是除以1024,右移20位是除以1048576
info.total = bytesSize >> 20; // MB
} else {
// 处理转换失败或非法数据的情况
info.total = 0;
// 可选:qWarning() << "Failed to parse device size or invalid size for:" << info.path;
}
info.uuid = reg.cap(4);
info.fstype = reg.cap(5);
type = reg.cap(6);总结:你的这次Diff修复了一个关键的容量计算逻辑Bug,并消除了原有的乘法溢出风险,方向是完全正确的。如果对代码健壮性有更高要求,可以参考我的改进建议增加校验逻辑和常量定义。 |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: max-lvs, pppanghu77 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 |
|
/forcemerge |
|
This pr force merged! (status: unstable) |
cb3c60a
into
linuxdeepin:release/eagle
…ndLsblkParse()
修复(device): 修正 CommandLsblkParse 中磁盘容量 KB 到 MB 的单位转换
Log: 修正 lsblk 解析中磁盘容量单位转换逻辑,将错误的乘除组合修正为连续除法以得到正确的 MB 值
Bug: https://pms.uniontech.com/bug-view-349331.html