diff --git a/sdk/checksum/checksum.go b/sdk/checksum/checksum.go index 627afab042..5509eaaf1e 100644 --- a/sdk/checksum/checksum.go +++ b/sdk/checksum/checksum.go @@ -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) + for len(buf) >= lim { chunk, buf = buf[:lim], buf[lim:] chunks = append(chunks, chunk) } + if len(buf) > 0 { chunks = append(chunks, buf[:]) } + return chunks } diff --git a/sdk/checksum/checksum_test.go b/sdk/checksum/checksum_test.go index dad0103af4..7abe0ee42e 100644 --- a/sdk/checksum/checksum_test.go +++ b/sdk/checksum/checksum_test.go @@ -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) diff --git a/test/integration/vendor/github.com/nginx/agent/sdk/v2/checksum/checksum.go b/test/integration/vendor/github.com/nginx/agent/sdk/v2/checksum/checksum.go index 627afab042..5509eaaf1e 100644 --- a/test/integration/vendor/github.com/nginx/agent/sdk/v2/checksum/checksum.go +++ b/test/integration/vendor/github.com/nginx/agent/sdk/v2/checksum/checksum.go @@ -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) + for len(buf) >= lim { chunk, buf = buf[:lim], buf[lim:] chunks = append(chunks, chunk) } + if len(buf) > 0 { chunks = append(chunks, buf[:]) } + return chunks } diff --git a/test/performance/vendor/github.com/nginx/agent/sdk/v2/checksum/checksum.go b/test/performance/vendor/github.com/nginx/agent/sdk/v2/checksum/checksum.go index 627afab042..5509eaaf1e 100644 --- a/test/performance/vendor/github.com/nginx/agent/sdk/v2/checksum/checksum.go +++ b/test/performance/vendor/github.com/nginx/agent/sdk/v2/checksum/checksum.go @@ -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) + for len(buf) >= lim { chunk, buf = buf[:lim], buf[lim:] chunks = append(chunks, chunk) } + if len(buf) > 0 { chunks = append(chunks, buf[:]) } + return chunks } diff --git a/vendor/github.com/nginx/agent/sdk/v2/checksum/checksum.go b/vendor/github.com/nginx/agent/sdk/v2/checksum/checksum.go index 627afab042..5509eaaf1e 100644 --- a/vendor/github.com/nginx/agent/sdk/v2/checksum/checksum.go +++ b/vendor/github.com/nginx/agent/sdk/v2/checksum/checksum.go @@ -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) + for len(buf) >= lim { chunk, buf = buf[:lim], buf[lim:] chunks = append(chunks, chunk) } + if len(buf) > 0 { chunks = append(chunks, buf[:]) } + return chunks }