Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion internal/pkg/danger/buf.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ func (b *Buf) Reset() {
}

func (b *Buf) grow(n int) {
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")
Copy link
Member

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

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:

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.

}
buf := make([]byte, len(b.buf), newCap)
copy(buf, b.buf)
b.buf = buf
}
Expand Down