Skip to content

operations: fix cat --offset before the start of a file printing its end - #10216

Merged
ncw merged 1 commit into
rclone:masterfrom
b84330808:cat-offset-before-start
Oct 31, 2026
Merged

ncw merged 1 commit into
rclone:masterfrom
b84330808:cat-offset-before-start

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

What does this change do?

rclone cat with a negative --offset that reaches back past the start of a file, combined with --count, prints bytes from the end of the file instead of from its start. On a 10 byte file:

$ rclone cat --offset -12 --count 5 file1.txt
IJ

The expected output is ABCDE, which is what tail -c 12 file1.txt | head -c 5 prints. When catting a directory, every file shorter than the offset is affected: for a 10 byte and a 9 byte file, rclone cat --offset -12 --count 5 dir printed IJ8 instead of ABCDE01234.

catObject adds the object size to a negative offset, but when the offset is larger than the file the start stays negative. A RangeOption with a negative Start and an End set is a suffix range (the last End bytes), so the wrong part of the file is read. This clamps the start to 0. --tail, and --offset without --count, behave as before. The same function backs the rc operations/cat call.

I added two cases to TestCat; both fail without the change.

Linked issue

None, it's a small bug fix.

Checklist

  • This change is trivial OR it has been discussed and agreed in the linked issue.
  • I have read the contribution guidelines.
  • (If I used AI tools to help write this code) I have read and understood the AI-assisted contributions guidance, and I have tested and take ownership of this change myself.
  • I have added tests for all changes in this PR if appropriate.
  • I have added documentation for the changes if appropriate.
  • All commit messages are in house style.
  • (Backend changes only) test_all passes for this backend and if submitting a new backend can provide a test account for the integration tester - see CONTRIBUTING.md.
  • This Pull Request is ready for review.

I used Claude Code to write this change.

When a negative --offset reached back past the start of a file and
--count was set, rclone cat printed bytes from the end of the file
instead of from its start. For example on a 10 byte file

    rclone cat --offset -12 --count 5 file

printed the last 2 bytes rather than the first 5. With several files
of different sizes, each file shorter than -offset was affected.

The start of the range stayed negative after the file size was added,
and a negative Start with an End set is a suffix range (the last End
bytes). Clamp the start to 0 instead, which matches what
`tail -c 12 file | head -c 5` prints. --tail and --offset without
--count were not affected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@b84330808
b84330808 requested review from ncw and nielash as code owners October 30, 2026 18:21

@ncw ncw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you :-)

@ncw
ncw merged commit 4e19a96 into rclone:master Oct 31, 2026
9 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.

2 participants