Repository navigation
fix: prevent integer overflow and division by zero in size utils - #263
Conversation
|
Sorry @liyigang1, you've used your own review budget of 250,000 diff characters for the last 7 days. You can request another review in 1 minute by commenting |
Reviewer's GuideAPIExtend the public ceiling-rounding interface to report overflow while retaining a default argument for existing callers.
Uses of the APIPass the overflow status from ceiling calculations through signature-erasure range construction and abort before writing if a range cannot be represented.
ImplementationGuard non-positive rounding sizes, detect ceiling-addition overflow, and saturate overflowing formatted byte totals.
ConfigurationEnable tests to access private members by adding the compiler access-control option to the test build.
TestsAdd utility tests for size formatting, rounding guards, and ceiling overflow, plus signature-erasure early-return cases.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
921e843 to
74d282b
Compare
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 Additionally, when Utils::ceilSize()'s numerator addition overflows and roundingSize does not divide LLONG_MAX, the saturated value would round down to a multiple below the requested size. PartedCore:: eraseFilesystemSignatures() used that truncated value to compute the end of an erase range, leaving trailing bytes unerased. ceilSize() now reports the overflow through a bool *overflow output parameter and eraseFilesystemSignatures() aborts with failure instead of writing a truncated range. Add unit tests in test/ut_diskoperation/ut_utils.cpp covering formatSize() unit boundaries and overflow saturation, floorSize()/ ceilSize() normal values, zero/negative rounding guards, and the unrepresentable ceiling size failure path. 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 4. Test partition formatting and signature erase on normal disks 5. Verify erase reports failure rather than truncating when the ceiling size is unrepresentable (near LLONG_MAX) 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. 影响:正常值路径行为不变;极值/零值输入从崩溃或未定义行为 变为安全返回 另外,当 Utils::ceilSize() 的分子加法溢出且 roundingSize 不能整除 LLONG_MAX 时,饱和值会被向下取整到小于请求大小的倍数。 PartedCore::eraseFilesystemSignatures() 用该截断值计算擦除范围的 末尾,会遗漏尾随字节。ceilSize() 现在通过 bool *overflow 出参报告 溢出,eraseFilesystemSignatures() 检测到后中止并返回失败,不再写入 截断范围。 在 test/ut_diskoperation/ut_utils.cpp 中新增单元测试,覆盖 formatSize() 各单位边界与溢出饱和、floorSize()/ceilSize() 正常取值、零值/负值 舍入保护,以及不可表示的向上取整失败路径。 Influence: 1. 测试各单位阈值档位下的正常磁盘容量显示 2. 测试标准对齐粒度(如 4096)下的分区大小取整 3. 验证极值(接近 LLONG_MAX)和零值 roundingSize 下不崩溃 4. 测试正常磁盘的分区格式化与签名擦除 5. 验证擦除范围无法表示时返回失败而非截断写入 PMS: BUG-383517
74d282b to
9226190
Compare
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: liyigang1, lzwind 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) |
Additionally, when Utils::ceilSize()'s numerator addition overflows and roundingSize does not divide LLONG_MAX, the saturated value would round down to a multiple below the requested size. PartedCore:: eraseFilesystemSignatures() used that truncated value to compute the end of an erase range, leaving trailing bytes unerased. ceilSize() now reports the overflow through a bool *overflow output parameter and eraseFilesystemSignatures() aborts with failure instead of writing a truncated range.
Influence:
fix: 修复尺寸工具函数中的整数溢出与除零问题
另外,当 Utils::ceilSize() 的分子加法溢出且 roundingSize 不能整除
LLONG_MAX 时,饱和值会被向下取整到小于请求大小的倍数。
PartedCore::eraseFilesystemSignatures() 用该截断值计算擦除范围的 末尾,会遗漏尾随字节。ceilSize() 现在通过 bool *overflow 出参报告
溢出,eraseFilesystemSignatures() 检测到后中止并返回失败,不再写入
截断范围。
Influence:
PMS: BUG-383517
Summary by Sourcery
Harden size calculations and filesystem signature erasure against invalid rounding values and unrepresentable arithmetic results.
Bug Fixes:
Enhancements:
Build:
Tests: