truncate: reject a size above i64::MAX instead of creating the file - #13902
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
| } | ||
| })?; | ||
|
|
||
| // A size that fits in u64 but exceeds the signed file offset (i64::MAX) is |
There was a problem hiding this comment.
Please make sure you hr comment shorter
|
Shortened the comment. Thanks! |
90f72f2 to
908cbf0
Compare
|
GNU testsuite comparison: |
908cbf0 to
c836adb
Compare
c836adb to
d4951db
Compare
|
Shortened the comment, thanks. |
|
Thanks! |
|
Is it able to move this to clap's value parser? |
|
@oech3 the |
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.