Skip to content

file: reject a -Y range larger than the file - #23581

Closed
Ar-maan05 wants to merge 1 commit into
curl:masterfrom
Ar-maan05:file-range-beyond-size
Closed

Ar-maan05 wants to merge 1 commit into
curl:masterfrom
Ar-maan05:file-range-beyond-size

Conversation

@Ar-maan05

@Ar-maan05 Ar-maan05 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

A -Y range on a file:// URL that asks for more bytes than the file has is handled inconsistently. With a 22 byte file:

before:
$ curl -r -23 file:///tmp/f22.txt | wc -c    -> 22, exit 0
$ curl -r -100 file:///tmp/f22.txt | wc -c   -> 0, exit 36, no error message

after:
$ curl -r -23 file:///tmp/f22.txt
curl: (36) Offset (-23) was beyond file size (22)
$ curl -r -100 file:///tmp/f22.txt
curl: (36) Offset (-100) was beyond file size (22)

file.c adds the file size to the negative offset without checking it first. Off by one, the offset becomes -1, the lseek() to -1 fails and returns -1, and since that matches the expected offset the transfer just starts at 0. Off by more, the values no longer match and curl fails with 36 without saying why.

ftp.c and both sftp backends already reject this case with "Offset (...) was beyond file size (...)", so this adds the same check to file.c. -r -22 on the same file still returns the whole file.

Test 1020 now also asks for one byte too many in a second transfer (fails before, passes after), and the existing FILE tests still pass locally.

I found this with an AI-assisted code review and then checked it by hand on a local build.

@github-actions github-actions Bot added the tests label Oct 23, 2026
Comment thread tests/data/test1694 Resolved
A "last N bytes" range on a file:// URL was not checked against the
file size. With N one larger than the file, resume_from became -1, the
lseek() to -1 failed and returned -1, which matched, so the transfer
started at offset 0 and returned the whole file. With any larger N the
lseek() result did not match and curl returned 36 without saying why.

Reject the range like ftp and sftp already do, with the same message.

Extend test 1020 to verify.
@Ar-maan05
Ar-maan05 force-pushed the file-range-beyond-size branch from 4490e27 to 67f0425 Compare October 23, 2026 19:24
@Ar-maan05

Copy link
Copy Markdown
Contributor Author

Sure, done. I folded it into test 1020 as a second transfer with --next and dropped test 1694. It still fails without the fix and passes with it.

@testclutch

Copy link
Copy Markdown

Analysis of PR #23581 at 67f04254:

Test ../../tests/http/test_02_download.py::TestDownload::test_02_06_download_many_parallel[h3] failed, which has NOT been flaky recently, so there could be a real issue in this PR. Note that this test has failed in 2 different CI jobs (the link just goes to one of them).

Generated by Testclutch

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The targeted bounds check preserves valid ranges, and the regression test covers the reported off-by-one failure.

No open issues 🎉

What changed in this PR

Makes oversized suffix ranges on file:// URLs fail consistently with a clear error, matching FTP and SFTP behavior.

Changes:

  • Reject suffix ranges larger than the file before adjusting the offset, while preserving exact-size ranges.
  • Extend test 1020 with a second transfer requesting one byte too many and expecting error 36.
File Description
tests/​data/​test1020 Add regression coverage for an oversized suffix range.
lib/​file.c Validate the suffix offset against the file size and report a clear error.

Review effort: Balanced

@bagder bagder closed this in 178e507 Oct 23, 2026
@bagder

bagder commented Oct 23, 2026

Copy link
Copy Markdown
Member

Thanks!

@Ar-maan05
Ar-maan05 deleted the file-range-beyond-size branch October 24, 2026 04:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

5 participants