mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-12 16:15:02 +03:00
fix(service): keep chunk capacity math from overflowing
chunkStrings/chunkInts sized their result with (len+size-1)/size, so a size near MaxInt wrapped negative and make panicked with "cap out of range"; count the chunks with div+remainder instead. NormalizeUDPHop's len-hops+1 is bounded by len(masks) since hops >= 1, so cap at that. Resolves CodeQL go/allocation-size-overflow alerts #119 and #120.
This commit is contained in:
@@ -72,7 +72,7 @@ func chunkStrings(s []string, size int) [][]string {
|
||||
if size <= 0 || len(s) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make([][]string, 0, (len(s)+size-1)/size)
|
||||
out := make([][]string, 0, chunkCount(len(s), size))
|
||||
for i := 0; i < len(s); i += size {
|
||||
end := min(i+size, len(s))
|
||||
out = append(out, s[i:end])
|
||||
@@ -86,10 +86,19 @@ func chunkInts(s []int, size int) [][]int {
|
||||
if size <= 0 || len(s) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make([][]int, 0, (len(s)+size-1)/size)
|
||||
out := make([][]int, 0, chunkCount(len(s), size))
|
||||
for i := 0; i < len(s); i += size {
|
||||
end := min(i+size, len(s))
|
||||
out = append(out, s[i:end])
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// chunkCount is ceil(n/size) without the n+size-1 sum, which overflows for a size near MaxInt.
|
||||
func chunkCount(n, size int) int {
|
||||
count := n / size
|
||||
if n%size != 0 {
|
||||
count++
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user