-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Size computation for allocation Integer overflow #4527
Conversation
❌ Author of the following commits did not sign a Contributor Agreement: Please, read and sign the above mentioned agreement if you want to contribute to this project |
This pull request does not have a backport label. Could you fix it @odaysec? 🙏
|
/cla |
buf := make([]byte, len(b.buf), 2*cap(b.buf)+n) | ||
newCap := 2*cap(b.buf) + n | ||
if newCap < 0 { | ||
panic("danger.Buf.grow: size computation overflow") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an internal method that only has 2 calls in this file, one that already explicitly handles a negative count the same way bytes.Buffer does in Go itself https://cs.opensource.google/go/go/+/refs/tags/go1.24.0:src/bytes/buffer.go;l=164-170
fleet-server/internal/pkg/danger/buf.go
Lines 41 to 48 in ba68a24
func (b *Buf) Grow(n int) { | |
if n < 0 { | |
panic("danger.Buf.Grow: negative count") | |
} | |
if cap(b.buf)-len(b.buf) < n { | |
b.grow(n) | |
} | |
} |
And another that uses an explicitly non-negative constant:
fleet-server/internal/pkg/danger/buf.go
Lines 66 to 74 in ba68a24
func (b *Buf) WriteRune(r rune) (int, error) { | |
if r < utf8.RuneSelf { | |
b.buf = append(b.buf, byte(r)) | |
return 1, nil | |
} | |
l := len(b.buf) | |
if cap(b.buf)-l < utf8.UTFMax { | |
b.grow(utf8.UTFMax) | |
} |
This change seems unnecessary.
Closing, no functional change from current behavior. Re-open if I am missing something. |
What is the problem this PR solves?
Performing calculations involving the size of potentially large strings or slices can result in an overflow (for signed integer types) or a wraparound (for unsigned types). An overflow causes the result of the calculation to become negative, while a wraparound results in a small (positive) number. This can cause further issues. If, for the result is then used in an allocation, it will cause a runtime panic if it is negative, and allocate an unexpectedly small buffer otherwise.
How to test this PR locally
#4525
Design Checklist
Checklist
./changelog/fragments
using the changelog toolRelated issues