truncate: reject a size above i64::MAX instead of creating the file - #13902
Open
AlejandroCoronadoN wants to merge 1 commit into
Open
truncate: reject a size above i64::MAX instead of creating the file#13902AlejandroCoronadoN wants to merge 1 commit into
AlejandroCoronadoN wants to merge 1 commit into
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
sylvestre
reviewed
Aug 16, 2026
| } | ||
| })?; | ||
|
|
||
| // A size that fits in u64 but exceeds the signed file offset (i64::MAX) is |
Contributor
There was a problem hiding this comment.
Please make sure you hr comment shorter
An absolute size above i64::MAX passed the size calculation and was only rejected later when opening the file, with a misleading error, after the file had been created. Check it against i64::MAX up front and report an invalid number, like GNU, so nothing is created.
Contributor
Author
|
Shortened the comment. Thanks! |
AlejandroCoronadoN
force-pushed
the
fix-truncate-oob-size
branch
from
August 17, 2026 14:25
90f72f2 to
908cbf0
Compare
|
GNU testsuite comparison: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
An absolute size in the range
(i64::MAX, u64::MAX]was accepted by the sizecalculation and only failed later when opening the file, with a misleading
message, after the file had already been created:
GNU truncate rejects it up front and creates nothing:
Fix
A file size must fit the signed file offset (
i64). Checktruncate_size > i64::MAXright after the size is computed and return the existing "invalidnumber / value too large" error, before the file is opened.
Verification
Compared against GNU truncate over
8E,9223372036854775808(i64::MAX + 1),9223372036854775807(i64::MAX),1E, and100: exit codes and whether thefile is created now match GNU in every case. Added a regression test; the full
test_truncatesuite (49 tests) passes andcargo fmt/cargo clippyareclean.