fix(utils): use Go 1.20+ unsafe.String in UnsafeCastToString and add upfront overflow check in CalculateMaxSize - #56
Conversation
…upfront overflow check in CalculateMaxSize
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository: getoptimum/coderabbit/.coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
💤 Files with no reviewable changes (1)
📝 WalkthroughWalkthroughThe utility updates replace slice-header reinterpretation with Estimated code review effort: 2 (Simple) | ~10 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 8 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (8 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@pkg/utils/bytes.go`:
- Around line 18-21: Verify the repository targets Go 1.20 or newer, then add
focused tests for the byte-slice conversion function in pkg/utils/bytes.go
covering nil input, non-nil empty slices, and ordinary byte content. Assert only
the resulting string values; do not test pointer layout or aliasing.
In `@pkg/utils/calc.go`:
- Around line 15-20: Update the overflow validation in the calculation function
around maxAllowed and finalVal to compare the computed increase against
maxAllowed-src, allowing src = maxAllowed - maxAllowed/5 + 1 when the resulting
value fits. Perform this validation before adding to src, preserve rejection
when the increase would exceed the remaining capacity, and add a regression test
covering this boundary.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository: getoptimum/coderabbit/.coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 0ae61ca1-313d-4820-8e94-a973df452833
📒 Files selected for processing (2)
pkg/utils/bytes.gopkg/utils/calc.go
|
Thanks for the PR - happy to check the For CalculateMaxSize, the upfront Once updated, we will check |
…add focused unit tests
|
Hi @swarna1101,
|
3755754 to
d67d9e8
Compare
|
Thanks for updating Please slim the rest: drop |
…uested by maintainer
9ae71f7 to
d079ca6
Compare
|
Done! I have simplified the PR as requested:
|
There was a problem hiding this comment.
Pull request overview
This PR modernizes and hardens two small helpers in pkg/utils. It replaces the legacy unsafe.Pointer slice-header cast in UnsafeCastToString with the standard Go 1.20+ unsafe.String/unsafe.SliceData API, and rewrites the overflow guard in CalculateMaxSize to use pure integer arithmetic (checking before the addition) instead of a float-based post-addition check. These utilities support message-ID hashing (consensus layer) and max message-size calculation (mump2p node setup).
Changes:
- Switch
UnsafeCastToStringto the idiomaticunsafe.String(unsafe.SliceData(...), len(...))form. - Rewrite
CalculateMaxSizeoverflow detection with an upfront integer check (src > math.MaxInt - src/5) and integer overhead (src/5) instead of floating-point math.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| pkg/utils/bytes.go | Replaces legacy pointer-cast with Go 1.20+ unsafe.String; behaviorally equivalent and zero-length safe. |
| pkg/utils/calc.go | Adds upfront integer overflow guard and integer-based overhead, avoiding float rounding before int64 addition. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Dismissing — want to slim the PR further before merge
Updated UnsafeCastToString in pkg/utils/bytes.go to use standard Go 1.20+ unsafe.String(unsafe.SliceData(byteSlice), len(byteSlice)) with zero-length check, replacing the legacy slice header cast. Also added upfront overflow guard in CalculateMaxSize before int64 addition.
Summary by CodeRabbit