fix: Import math and validate request body size - #7535
Conversation
Add math import and check for bulk request body size. fix protect all allocation-size arithmetic by checking for overflow before computing/using capacities. For this specific issue, make `Buf.grow` compute a target capacity without overflowing, and panic with a clear message if growth cannot be represented as `int`. Best targeted fix (no functional change beyond safety): - Edit `internal/pkg/danger/buf.go`, function `grow`. - Replace `2*cap(b.buf)+n` with guarded arithmetic: - Validate `n >= 0` (defensive, even though `Grow` checks). - Compute required capacity `need := len(b.buf) + n` with overflow check. - Compute doubled capacity from current cap with overflow check/saturation. - Choose `newCap = max(need, doubledCap)`. - Allocate with `make([]byte, len(b.buf), newCap)`. - Add `math` import for `math.MaxInt`.
|
This pull request does not have a backport label. Could you fix it @ekuboo100? 🙏
|
|
|
||
| // Serialize request | ||
| const kSlop = 64 | ||
| if len(body) > math.MaxInt-kSlop { |
There was a problem hiding this comment.
Could you share what system you are running to have a byte slice of len(math.MaxInt) in memory ?
|
/test |
TL;DRBuildkite failed in Remediation
Investigation detailsRoot Cause
In this build, newCap := need
if doubled > newCap {
newCap = doubled
}to: newCap := max(doubled, need)That uncommitted rewrite caused Evidence
Verification
Follow-up
What is this? | From workflow: PR Buildkite Detective Give us feedback! React with 🚀 if perfect, 👍 if helpful, 👎 if not. |
Add math import and check for bulk request body size. fix protect all allocation-size arithmetic by checking for overflow before computing/using capacities. For this specific issue, make
Buf.growcompute a target capacity without overflowing, and panic with a clear message if growth cannot be represented asint.Best targeted fix (no functional change beyond safety):
internal/pkg/danger/buf.go, functiongrow.2*cap(b.buf)+nwith guarded arithmetic:n >= 0(defensive, even thoughGrowchecks).need := len(b.buf) + nwith overflow check.newCap = max(need, doubledCap).make([]byte, len(b.buf), newCap).mathimport formath.MaxInt.Design Checklist
Checklist
./changelog/fragmentsusing the changelog toolRelated issues