Skip to content

tests/truncate: add regression tests for -s %SIZE round-up bug#10901

Merged
ChrisDryden merged 1 commit intouutils:mainfrom
vjardin:vj_fixtruncatev1
Feb 14, 2026
Merged

tests/truncate: add regression tests for -s %SIZE round-up bug#10901
ChrisDryden merged 1 commit intouutils:mainfrom
vjardin:vj_fixtruncatev1

Conversation

@vjardin
Copy link
Contributor

@vjardin vjardin commented Feb 12, 2026

The old RoundUp formula fsize + fsize % size was incorrect. For example, truncate -s %128K on a 24696-byte file produced 49392 bytes instead of the correct 131072 bytes.

Maybe, this was incidentally fixed in commit fe97933 ("truncate: eliminate duplicate stat() syscall") by replacing the formula with fsize.checked_next_multiple_of(size). However, the existing test test_round_up used values (fsize=10, size=4) where both the buggy and correct formulas give the same result (12), so the bug was never caught by the test suite.

Add unit and integration tests that would have caught this bug:

  • File smaller than rounding unit (the reported scenario)
  • File larger than rounding unit but not aligned
  • File already aligned to the rounding unit
  • Division by zero (RoundUp with size=0)

Link: https://review.trustedfirmware.org/c/TF-A/trusted-firmware-a/+/48210

@github-actions
Copy link

GNU testsuite comparison:

GNU test failed: tests/cut/bounded-memory. tests/cut/bounded-memory is passing on 'main'. Maybe you have to rebase?
GNU test failed: tests/pr/bounded-memory. tests/pr/bounded-memory is passing on 'main'. Maybe you have to rebase?
Skipping an intermittent issue tests/shuf/shuf-reservoir (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/sort/sort-stale-thread-mem (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/printf/printf-surprise is now passing!

@vjardin
Copy link
Contributor Author

vjardin commented Feb 13, 2026

I am not expert with coreutils' CI ; but checking some others, it seems to be 2 scenarios that are used to fail ; I do not see how they are related to this commit.

The purpose of this contribution is to avoid other regressions that lead to wrong sizes.


/// Regression test: round up when file is smaller than the rounding unit.
///
/// `truncate -s %4K` on a 10-byte file must produce 4096 bytes, not 20.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More of a style nit and personal preference but do you think it would be possible to make the test names descriptive enough to not require the comment above to make it more consise? Same with the additional code of "expected '{expected}' got '{actual}'" I think if it fails it will provide the left and right descriptions

@ChrisDryden
Copy link
Collaborator

Thanks for adding more test cases, always good to have even if the underlying issue was already fixed. The tests seem correct to me just was hoping that we could reduce the size a bit

@vjardin
Copy link
Contributor Author

vjardin commented Feb 13, 2026

@ChrisDryden : this issue/ regression had been critical for ATF, so the purpose is to harden with the related use case thru those tests.

I'll check about your comments on cosmetics.

The old RoundUp formula `fsize + fsize % size` was incorrect. For
example, `truncate -s %128K` on a 24696-byte file produced 49392
bytes instead of the correct 131072 bytes.

This was incidentally fixed in commit fe97933 ("truncate: eliminate
duplicate stat() syscall") by replacing the formula with
`fsize.checked_next_multiple_of(size)`. However, the existing test
`test_round_up` used values (fsize=10, size=4) where both the buggy
and correct formulas give the same result (12), so the bug was never
caught by the test suite.

Add unit and integration tests that would have caught this bug:
- File smaller than rounding unit (the reported scenario)
- File larger than rounding unit but not aligned
- File already aligned to the rounding unit
- Division by zero (RoundUp with size=0)

Link: https://review.trustedfirmware.org/c/TF-A/trusted-firmware-a/+/48210
Signed-off-by: Vincent Jardin <vjardin@free.fr>
@vjardin
Copy link
Contributor Author

vjardin commented Feb 14, 2026

@ChrisDryden : done, feel free to comment if you want some more adjustments.

@github-actions
Copy link

GNU testsuite comparison:

GNU test failed: tests/pr/bounded-memory. tests/pr/bounded-memory is passing on 'main'. Maybe you have to rebase?
Skip an intermittent issue tests/shuf/shuf-reservoir (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/sort/sort-stale-thread-mem (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/tty/tty-eof (passes in this run but fails in the 'main' branch)
Note: The gnu test tests/cut/bounded-memory is now being skipped but was previously passing.
Note: The gnu test tests/unexpand/bounded-memory is now being skipped but was previously passing.
Congrats! The gnu test tests/expand/bounded-memory is now passing!

@ChrisDryden ChrisDryden merged commit 764eef5 into uutils:main Feb 14, 2026
151 of 155 checks passed
@vjardin vjardin deleted the vj_fixtruncatev1 branch February 14, 2026 14:09
abendrothj pushed a commit to abendrothj/coreutils that referenced this pull request Feb 17, 2026
…s#10901)

The old RoundUp formula `fsize + fsize % size` was incorrect. For
example, `truncate -s %128K` on a 24696-byte file produced 49392
bytes instead of the correct 131072 bytes.

This was incidentally fixed in commit fe97933 ("truncate: eliminate
duplicate stat() syscall") by replacing the formula with
`fsize.checked_next_multiple_of(size)`. However, the existing test
`test_round_up` used values (fsize=10, size=4) where both the buggy
and correct formulas give the same result (12), so the bug was never
caught by the test suite.

Add unit and integration tests that would have caught this bug:
- File smaller than rounding unit (the reported scenario)
- File larger than rounding unit but not aligned
- File already aligned to the rounding unit
- Division by zero (RoundUp with size=0)

Link: https://review.trustedfirmware.org/c/TF-A/trusted-firmware-a/+/48210

Signed-off-by: Vincent Jardin <vjardin@free.fr>
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.

2 participants