Skip to content

Commit

Permalink
fixes integer overflow in ForEachChunk
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vroldanbet authored and ecordell committed Mar 1, 2024
1 parent 850d6ed commit 8fb9c28
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions pkg/genutil/slicez/chunking.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ func ForEachChunkUntil[T any](data []T, chunkSize uint16, handler func(items []T
chunkSize = 1
}

dataLength := uint16(len(data))
chunkCount := (dataLength / chunkSize) + 1
for chunkIndex := uint16(0); chunkIndex < chunkCount; chunkIndex++ {
chunkStart := chunkIndex * chunkSize
chunkEnd := (chunkIndex + 1) * chunkSize
dataLength := uint64(len(data))
chunkSize64 := uint64(chunkSize)
chunkCount := (dataLength / chunkSize64) + 1
for chunkIndex := uint64(0); chunkIndex < chunkCount; chunkIndex++ {
chunkStart := chunkIndex * chunkSize64
chunkEnd := (chunkIndex + 1) * chunkSize64
if chunkEnd > dataLength {
chunkEnd = dataLength
}
Expand All @@ -38,5 +39,6 @@ func ForEachChunkUntil[T any](data []T, chunkSize uint16, handler func(items []T
}
}
}

return true, nil
}

0 comments on commit 8fb9c28

Please sign in to comment.