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:
@@ -1,6 +1,10 @@
|
||||
package service
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"math"
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNormalizeSubSortIndex(t *testing.T) {
|
||||
cases := []struct {
|
||||
@@ -18,3 +22,15 @@ func TestNormalizeSubSortIndex(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A size near MaxInt used to overflow the capacity math and panic in make (CodeQL #119).
|
||||
func TestChunkHugeSizeKeepsOneBatch(t *testing.T) {
|
||||
strs := []string{"a", "b", "c"}
|
||||
if got := chunkStrings(strs, math.MaxInt); len(got) != 1 || !slices.Equal(got[0], strs) {
|
||||
t.Fatalf("chunkStrings(MaxInt) = %v, want [%v]", got, strs)
|
||||
}
|
||||
ints := []int{1, 2, 3}
|
||||
if got := chunkInts(ints, math.MaxInt); len(got) != 1 || !slices.Equal(got[0], ints) {
|
||||
t.Fatalf("chunkInts(MaxInt) = %v, want [%v]", got, ints)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user