Skip to content

fix: prevent integer overflow and division by zero in size utils - #263

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
liyigang1:agent/bugfix/383517
Nov 23, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
liyigang1:agent/bugfix/383517

Conversation

@liyigang1

@liyigang1 liyigang1 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor
  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.

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() 检测到后中止并返回失败,不再写入
截断范围。

Influence:

  1. 测试各单位阈值档位下的正常磁盘容量显示
  2. 测试标准对齐粒度(如 4096)下的分区大小取整
  3. 验证极值(接近 LLONG_MAX)和零值 roundingSize 下不崩溃
  4. 测试正常磁盘的分区格式化与签名擦除
  5. 验证擦除范围无法表示时返回失败而非截断写入

PMS: BUG-383517

Summary by Sourcery

Harden size calculations and filesystem signature erasure against invalid rounding values and unrepresentable arithmetic results.

Bug Fixes:

  • Prevent size calculations from dividing by zero or overflowing on extreme inputs, returning safe values instead of crashing or invoking undefined behavior.
  • Make filesystem signature erasure fail safely when an erase range cannot be represented without truncation.

Enhancements:

  • Preserve overflow status from ceiling-size calculations so callers can distinguish saturated results from valid aligned sizes.

Build:

  • Enable private-member access in the test target for internal utility testing.

Tests:

  • Add unit coverage for size formatting thresholds, alignment behavior, invalid rounding sizes, arithmetic overflow, and filesystem signature-erasure failure paths.

@sourcery-ai

sourcery-ai Bot commented Nov 23, 2026

Copy link
Copy Markdown

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 @sourcery-ai review. Upgrade to get a review now.

@sourcery-ai

sourcery-ai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

API

Extend the public ceiling-rounding interface to report overflow while retaining a default argument for existing callers.

  • basestruct/utils.h L170-173

Uses of the API

Pass the overflow status from ceiling calculations through signature-erasure range construction and abort before writing if a range cannot be represented.

  • service/diskoperation/partedcore.cpp L3291-3292, L3297-3308

Implementation

Guard non-positive rounding sizes, detect ceiling-addition overflow, and saturate overflowing formatted byte totals.

  • basestruct/utils.cpp L17, L583-596, L655-676

Configuration

Enable tests to access private members by adding the compiler access-control option to the test build.

  • test/CMakeLists.txt L11-13

Tests

Add utility tests for size formatting, rounding guards, and ceiling overflow, plus signature-erasure early-return cases.

  • test/ut_diskoperation/ut_partedcore_erase.cpp L1-75
  • test/ut_diskoperation/ut_utils.cpp L1-136

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

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
@deepin-ci-robot

Copy link
Copy Markdown

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

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

@liyigang1

Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot

deepin-bot Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

This pr force merged! (status: unstable)

@deepin-bot
deepin-bot Bot merged commit 72578e5 into linuxdeepin:master Nov 23, 2026
16 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