Skip to content

Commit 93331d0

Browse files
vroldanbetjzelinskie
authored andcommitted
fixes integer overflow in ForEachChunk
the calculation of the length of the input data is now done with a 64 unsigned integer. A 64 unsigned integer overflow is still possible, but if that's happening, we have bigger problems.
1 parent 72dc9b1 commit 93331d0

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

pkg/genutil/slicez/chunking.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ func ForEachChunkUntil[T any](data []T, chunkSize uint16, handler func(items []T
1818
chunkSize = 1
1919
}
2020

21-
dataLength := uint16(len(data))
22-
chunkCount := (dataLength / chunkSize) + 1
23-
for chunkIndex := uint16(0); chunkIndex < chunkCount; chunkIndex++ {
24-
chunkStart := chunkIndex * chunkSize
25-
chunkEnd := (chunkIndex + 1) * chunkSize
21+
dataLength := uint64(len(data))
22+
chunkSize64 := uint64(chunkSize)
23+
chunkCount := (dataLength / chunkSize64) + 1
24+
for chunkIndex := uint64(0); chunkIndex < chunkCount; chunkIndex++ {
25+
chunkStart := chunkIndex * chunkSize64
26+
chunkEnd := (chunkIndex + 1) * chunkSize64
2627
if chunkEnd > dataLength {
2728
chunkEnd = dataLength
2829
}
@@ -38,5 +39,6 @@ func ForEachChunkUntil[T any](data []T, chunkSize uint16, handler func(items []T
3839
}
3940
}
4041
}
42+
4143
return true, nil
4244
}

0 commit comments

Comments
 (0)