Skip to content

Add additional checks in chunking functionality #671

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

Merged
merged 4 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
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
15 changes: 14 additions & 1 deletion sdk/checksum/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,26 @@ func HexChecksum(b []byte) string {
// Chunk - split bytes to chunk limits
func Chunk(buf []byte, lim int) [][]byte {
var chunk []byte
chunks := make([][]byte, 0, len(buf)/lim+1)
bufSize := len(buf)

if bufSize == 0 {
return [][]byte{}
}

if bufSize <= lim {
return [][]byte{buf}
}

chunks := make([][]byte, 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we no longer set the capacity for chunks?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Donal answered here at the office, so the reason was that the tool was complaining about our capacity potentially overflowing.

FWIW, I did the math on this one. The capacity was len(buf)/lim+1 meaning the only way we exceed the maximum length of an integer (i.e. overflow) is if the buffer (=buf) had 2^63 elements in it (so 9,223,372,036,854,775,807 elements) on a 64-bit system. Since buf is a byte slice, that means you would have 2^63 bytes, which translates to ~9,223 petabytes, meaning you definitely would have run into problem far earlier than when entering this.


for len(buf) >= lim {
chunk, buf = buf[:lim], buf[lim:]
chunks = append(chunks, chunk)
}

if len(buf) > 0 {
chunks = append(chunks, buf[:])
}

return chunks
}
1 change: 1 addition & 0 deletions sdk/checksum/checksum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func TestHexChunk(t *testing.T) {
expected: [][]byte{},
},
}

for _, test := range tests {
result := Chunk(test.input, test.limit)
assert.Equal(t, test.expected, result)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion vendor/github.com/nginx/agent/sdk/v2/checksum/checksum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading