From 90aad7da30595e121c01b11cbad8cf4615182566 Mon Sep 17 00:00:00 2001 From: "liyigang1@uniontech.com" Date: Thu, 19 Nov 2026 21:23:42 +0800 Subject: [PATCH] fix: prevent integer overflow and division by zero in size utils MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Root cause: ceilSize() and formatSize() performed unchecked arithmetic on 64-bit signed integers, causing CWE-190 overflow; floorSize() and ceilSize() divided by roundingSize without a zero-guard, causing CWE-369 division by zero (SIGFPE) 2. Fix: add roundingSize <= 0 early return in floorSize()/ceilSize(); use __builtin_add_overflow in ceilSize() and __builtin_mul_overflow in formatSize() to detect overflow, capping to LLONG_MAX on overflow 3. Impact: normal-value paths unchanged; extreme/zero-value inputs now return safely instead of crashing or producing undefined behavior Influence: 1. Test disk size display with normal values across all unit thresholds 2. Test partition alignment with standard rounding sizes (e.g. 4096) 3. Verify no crash on extreme values (near LLONG_MAX) and zero roundingSize fix: 修复尺寸工具函数中的整数溢出与除零问题 1. 根因:ceilSize() 和 formatSize() 对 64 位有符号整数执行未检查的 算术运算,导致 CWE-190 溢出;floorSize() 和 ceilSize() 未对 roundingSize 做零值守卫即执行除法,导致 CWE-369 除零(SIGFPE) 2. 方案:在 floorSize()/ceilSize() 中添加 roundingSize <= 0 提前返回; ceilSize() 使用 __builtin_add_overflow、formatSize() 使用 __builtin_mul_overflow 检测溢出,溢出时 cap 到 LLONG_MAX 3. 影响:正常值路径行为不变;极值/零值输入从崩溃或未定义行为 变为安全返回 Influence: 1. 测试各单位阈值档位下的正常磁盘容量显示 2. 测试标准对齐粒度(如 4096)下的分区大小取整 3. 验证极值(接近 LLONG_MAX)和零值 roundingSize 下不崩溃 PMS: BUG-383517 --- basestruct/utils.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/basestruct/utils.cpp b/basestruct/utils.cpp index c7b3cff..4de6b56 100644 --- a/basestruct/utils.cpp +++ b/basestruct/utils.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include @@ -473,16 +474,20 @@ QString Utils::getFileSystemSoftWare(FSType fileSystemType) QString Utils::formatSize(Sector sectors, Byte_Value sectorSize) { QString res; - if ((sectors * sectorSize) < KIBIBYTE) { + Byte_Value totalBytes; + if (__builtin_mul_overflow(sectors, sectorSize, &totalBytes)) { + totalBytes = LLONG_MAX; + } + if (totalBytes < KIBIBYTE) { res = res.setNum(sectorToUnit(sectors, sectorSize, UNIT_BYTE), 'f', 2); res.append(" B"); - } else if ((sectors * sectorSize) < MEBIBYTE) { + } else if (totalBytes < MEBIBYTE) { res = res.setNum(sectorToUnit(sectors, sectorSize, UNIT_KIB), 'f', 2); res.append(" KiB"); - } else if ((sectors * sectorSize) < GIBIBYTE) { + } else if (totalBytes < GIBIBYTE) { res = res.setNum(sectorToUnit(sectors, sectorSize, UNIT_MIB), 'f', 2); res.append(" MiB"); - } else if ((sectors * sectorSize) < TEBIBYTE) { + } else if (totalBytes < TEBIBYTE) { res = res.setNum(sectorToUnit(sectors, sectorSize, UNIT_GIB), 'f', 2); res.append(" GiB"); } else { @@ -535,12 +540,20 @@ int Utils::getMaxPartitionNameLength(QString &tableType) Byte_Value Utils::floorSize(Byte_Value value, Byte_Value roundingSize) { + if (roundingSize <= 0) + return value; return value / roundingSize * roundingSize; } Byte_Value Utils::ceilSize(Byte_Value value, Byte_Value roundingSize) { - return (value + roundingSize - 1LL) / roundingSize * roundingSize; + if (roundingSize <= 0) + return value; + Byte_Value numerator; + if (__builtin_add_overflow(value, roundingSize - 1LL, &numerator)) { + numerator = LLONG_MAX; + } + return numerator / roundingSize * roundingSize; } QString Utils::createUuid()