🧹 chore: Use int64 when dealing with HTTP Ranges#3874
Conversation
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. WalkthroughThe Range API and related types were widened from 32-bit to 64-bit: Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Ctx
participant DefaultReq
participant parseBound
Client->>Ctx: Range(size: int64)
Ctx->>DefaultReq: Range(size: int64)
DefaultReq->>parseBound: validate start/end strings
alt bounds valid
parseBound-->>DefaultReq: start:int64, end:int64
DefaultReq-->>Ctx: Range{Start:int64, End:int64}
Ctx-->>Client: return Range
else malformed bounds / overflow
parseBound-->>DefaultReq: ErrRangeMalformed
DefaultReq-->>Ctx: return error
Ctx-->>Client: return error
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary of ChangesHello @gaby, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly improves the robustness of HTTP Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3874 +/- ##
==========================================
- Coverage 91.64% 91.58% -0.06%
==========================================
Files 115 115
Lines 9848 9857 +9
==========================================
+ Hits 9025 9028 +3
- Misses 523 528 +5
- Partials 300 301 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull Request Overview
This PR updates the HTTP Range header parsing to use int64 instead of int for range bounds, and adds clamping to prevent overflow when converting parsed uint64 values to int64. The threshold is set to math.MaxUint64 >> 1 (which equals math.MaxInt64), ensuring values stay within safe bounds for signed 64-bit integers.
Key Changes:
- Changed
RangeSet.StartandRangeSet.Endfrominttoint64to support larger files - Updated
Range(size)method signature to acceptint64instead ofint - Added
parseBoundhelper function that clamps parsed values atmath.MaxInt64to prevent overflow
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| req.go | Core implementation: added parseBound function with overflow protection, updated RangeSet fields to int64, and changed Range method signature |
| req_interface_gen.go | Updated generated interface to reflect Range(size int64) signature change |
| ctx_interface_gen.go | Updated generated interface to reflect Range(size int64) signature change |
| docs/api/ctx.md | Updated documentation to show the new Range(size int64) signature |
| ctx_test.go | Added Test_Ctx_Range_LargeFile to verify large file range handling with values exceeding math.MaxInt32 |
There was a problem hiding this comment.
Code Review
The pull request effectively addresses the issue of clamping range parsing by migrating relevant types and parsing logic to int64 and incorporating math.MaxUint64 for robust overflow checks. The introduction of the parseBound helper function centralizes the parsing and validation logic, ensuring that range header bounds are correctly handled and preventing potential overflows when converting to int64. The updated test cases, including Test_Ctx_Range_LargeFile, adequately cover the new functionality, and the documentation has been updated to reflect the changes in the Range method signature. Overall, the changes are well-implemented and improve the reliability of range header processing.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
docs/api/ctx.md (1)
1371-1396: Documentation updated correctly.The signature change is accurately reflected in the documentation. The example remains valid since integer literals can be used where
int64is expected.Consider adding a brief note about large file support to help users understand why
int64is used:### Range Returns a struct containing the type and a slice of ranges. +Accepts file sizes up to 2^63-1 bytes to support very large files. Only the canonical `bytes` unit is recognized and any optionalThis would make it clearer why the API uses
int64rather thanint, especially for developers working with large file serving.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
ctx_interface_gen.go(1 hunks)ctx_test.go(2 hunks)docs/api/ctx.md(1 hunks)req.go(5 hunks)req_interface_gen.go(1 hunks)
🧰 Additional context used
🧠 Learnings (4)
📓 Common learnings
Learnt from: efectn
Repo: gofiber/fiber PR: 3824
File: listen_test.go:560-564
Timestamp: 2025-11-13T11:34:38.503Z
Learning: The gofiber/fiber project only supports the latest Go versions, so Go 1.22+ syntax features (such as `for range N` integer range loops) are acceptable and preferred.
Learnt from: ReneWerner87
Repo: gofiber/fiber PR: 3161
File: app.go:923-932
Timestamp: 2024-11-15T07:56:21.623Z
Learning: In the Fiber framework, breaking changes are acceptable when moving from version 2 to version 3, including modifications to method signatures such as in the `Test` method in `app.go`.
📚 Learning: 2024-11-08T04:10:42.990Z
Learnt from: gaby
Repo: gofiber/fiber PR: 3193
File: middleware/cache/cache_test.go:897-897
Timestamp: 2024-11-08T04:10:42.990Z
Learning: In the Fiber framework, `Context()` is being renamed to `RequestCtx()`, and `UserContext()` to `Context()` to improve clarity and align with Go's context conventions.
Applied to files:
docs/api/ctx.md
📚 Learning: 2024-10-16T12:12:30.506Z
Learnt from: gaby
Repo: gofiber/fiber PR: 3170
File: ctx_test.go:1721-1724
Timestamp: 2024-10-16T12:12:30.506Z
Learning: In the Go unit tests in `ctx_test.go`, it is acceptable to use invalid CIDR notation such as `"0.0.0.1/31junk"` for testing purposes.
Applied to files:
ctx_test.go
📚 Learning: 2025-10-16T07:19:52.418Z
Learnt from: grivera64
Repo: gofiber/fiber PR: 3807
File: adapter_test.go:118-144
Timestamp: 2025-10-16T07:19:52.418Z
Learning: In the Fiber codebase, the linter does not allow `require` assertions from within HTTP handlers (including net/http-style handlers). Use `t.Fatalf`, `t.Errorf`, or similar `testing.T` methods for error handling inside handler functions instead.
Applied to files:
ctx_test.go
🧬 Code graph analysis (4)
req_interface_gen.go (1)
req.go (1)
Range(18-21)
ctx_interface_gen.go (1)
req.go (1)
Range(18-21)
req.go (3)
constants.go (2)
HeaderRange(236-236)HeaderContentRange(234-234)error.go (1)
ErrRangeMalformed(30-30)res.go (1)
DefaultRes(127-129)
ctx_test.go (2)
constants.go (1)
HeaderRange(236-236)req.go (1)
Range(18-21)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
- GitHub Check: Agent
- GitHub Check: lint
- GitHub Check: Compare
- GitHub Check: repeated
- GitHub Check: unit (1.25.x, windows-latest)
- GitHub Check: unit (1.25.x, macos-latest)
🔇 Additional comments (4)
req_interface_gen.go (1)
171-171: Verified: Interface and implementation signatures are consistent.The generated interface signature in
req_interface_gen.go:171matches theDefaultReqimplementation inreq.go:741. Both useint64for the size parameter, and the implementation correctly handles large file ranges throughout the function logic.ctx_interface_gen.go (1)
305-305: LGTM! Signature change supports large file ranges.The change from
inttoint64properly supports range operations on files larger than 2GB. This is a breaking change, which is acceptable for the v2→v3 transition.Verification confirms all requirements:
- ✅ ifacemaker generator (ctx.go:52) is configured to process req.go containing the updated Range method
- ✅ req.go:741 correctly implements
Range(size int64) (Range, error)- ✅ Call sites are updated (ctx_test.go:3581 passes compatible int64 values)
ctx_test.go (1)
3609-3632: Great addition for 64-bit coverage.This test nails the >32-bit start/end and suffix cases so we won’t regress the new int64 path.
req.go (1)
748-821: Appreciate the defensive clamp.Keeping bounds ≤
math.MaxUint64 >> 1means we never overflow the int64 conversion and malformed ranges bail out early withErrRangeMalformed; much cleaner than sprinkling casts.
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
req.go (1)
749-758: Consider consistent error wrapping.The overflow check
parsed > (math.MaxUint64 >> 1)correctly prevents values exceedingMaxInt64from being converted toint64. However, the error handling is inconsistent: parse errors are wrapped with context (line 752), but the overflow case returns bareErrRangeMalformed(line 755).For consistency and better debugging, consider wrapping the overflow error as well:
if parsed > (math.MaxUint64 >> 1) { - return 0, ErrRangeMalformed + return 0, fmt.Errorf("range bound %q exceeds maximum: %w", value, ErrRangeMalformed) }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
req.go(5 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-11-13T11:34:38.503Z
Learnt from: efectn
Repo: gofiber/fiber PR: 3824
File: listen_test.go:560-564
Timestamp: 2025-11-13T11:34:38.503Z
Learning: The gofiber/fiber project only supports the latest Go versions, so Go 1.22+ syntax features (such as `for range N` integer range loops) are acceptable and preferred.
Applied to files:
req.go
🧬 Code graph analysis (1)
req.go (3)
constants.go (2)
HeaderRange(236-236)HeaderContentRange(234-234)error.go (1)
ErrRangeMalformed(30-30)res.go (1)
DefaultRes(127-129)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
- GitHub Check: lint
- GitHub Check: unit (1.25.x, windows-latest)
- GitHub Check: repeated
- GitHub Check: Compare
🔇 Additional comments (5)
req.go (5)
6-7: LGTM! Imports support the new overflow-safe parsing.The
fmtandmathimports are appropriately used in the newparseBoundhelper for error wrapping and overflow detection.
26-27: Breaking change acknowledged for v3.Widening
StartandEndtoint64is a breaking API change that requires callers to update their code. However, since this PR targets the v3 branch (as indicated by the label), this is appropriate for a major version bump and enables support for large files beyond 32-bit offsets.
742-742: Breaking change acknowledged for v3.The signature change from
Range(size int)toRange(size int64)breaks backward compatibility but is consistent with theRangeSetfield changes and necessary to handle files larger than 2GB. Since this targets v3, the breaking change is appropriate.
795-799: LGTM! Overflow errors are properly rejected.The logic correctly distinguishes between overflow errors (which should fail immediately) and parse errors from empty strings (which indicate special range forms like
-nnnornnn-). The early return ensures only valid parsing failures reach the suffix/open-ended range handling below.
819-819: LGTM! Correctly formats int64 size.The switch from
strconv.Itoatostrconv.FormatInt(size, 10)is necessary and correct for handling the widenedint64size parameter.
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
ctx_test.go (1)
3609-3649: New Range tests correctly cover int64 boundaries and overflow; one small readability nitThe two new tests nicely exercise:
- A size/start/end well beyond 32‑bit (
size/startbased onmath.MaxInt32) to verifyCtx.Rangeworks with largeint64values, including a suffix range (bytes=-200).- Overflow/validation behavior for bounds above the clamping threshold, asserting
ErrRangeMalformedwhen the parsed value exceedsmath.MaxUint64 >> 1(i.e.MaxInt64), which matches the new clamping logic and prior review feedback.As a tiny readability improvement, you could define
tooBigdirectly frommath.MaxInt64instead of mirroring the internal clamp expression:tooBig := uint64(math.MaxInt64) + 1This keeps the numeric boundary identical while making the intent (“just over MaxInt64”) more obvious to readers.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
ctx_test.go(2 hunks)
🧰 Additional context used
🧠 Learnings (3)
📚 Learning: 2024-10-16T12:12:30.506Z
Learnt from: gaby
Repo: gofiber/fiber PR: 3170
File: ctx_test.go:1721-1724
Timestamp: 2024-10-16T12:12:30.506Z
Learning: In the Go unit tests in `ctx_test.go`, it is acceptable to use invalid CIDR notation such as `"0.0.0.1/31junk"` for testing purposes.
Applied to files:
ctx_test.go
📚 Learning: 2024-07-02T13:29:56.992Z
Learnt from: gaby
Repo: gofiber/fiber PR: 3056
File: middleware/encryptcookie/utils.go:22-25
Timestamp: 2024-07-02T13:29:56.992Z
Learning: The `encryptcookie_test.go` file contains unit tests that validate key lengths for both `EncryptCookie` and `DecryptCookie` functions, ensuring that invalid key lengths raise appropriate errors.
Applied to files:
ctx_test.go
📚 Learning: 2025-10-16T07:19:52.418Z
Learnt from: grivera64
Repo: gofiber/fiber PR: 3807
File: adapter_test.go:118-144
Timestamp: 2025-10-16T07:19:52.418Z
Learning: In the Fiber codebase, the linter does not allow `require` assertions from within HTTP handlers (including net/http-style handlers). Use `t.Fatalf`, `t.Errorf`, or similar `testing.T` methods for error handling inside handler functions instead.
Applied to files:
ctx_test.go
🧬 Code graph analysis (1)
ctx_test.go (3)
constants.go (1)
HeaderRange(236-236)req.go (1)
Range(19-22)error.go (1)
ErrRangeMalformed(30-30)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
- GitHub Check: lint
- GitHub Check: unit (1.25.x, windows-latest)
- GitHub Check: unit (1.25.x, macos-latest)
- GitHub Check: repeated
- GitHub Check: Compare
🔇 Additional comments (1)
ctx_test.go (1)
7-43: math import usage is appropriateThe added
mathimport is used forMaxInt32andMaxUint64in the new range tests and keeps everything within the standard library; no issues here.
Summary