Skip to content

Commit 712fe93

Browse files
h00810684novahe
h00810684
authored andcommitted
fix array index out of bounds
1 parent 5bd422f commit 712fe93

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

summary/percentiles.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,17 @@ func (s Uint64Slice) GetPercentile(d float64) uint64 {
4444
return 0
4545
}
4646
sort.Sort(s)
47+
if d == 1 {
48+
return s[count-1]
49+
}
4750
n := float64(d * (float64(count) + 1))
4851
idx, frac := math.Modf(n)
4952
index := int(idx)
53+
54+
if index < 1 {
55+
index = 1
56+
}
57+
5058
percentile := float64(s[index-1])
5159
if index > 1 && index < count {
5260
percentile += frac * float64(s[index]-s[index-1])

summary/percentiles_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ func TestPercentile(t *testing.T) {
4646
assertPercentile(t, s, 0.2, 21)
4747
assertPercentile(t, s, 0.7, 74)
4848
assertPercentile(t, s, 0.9, 95)
49+
50+
// boundary value
51+
assertPercentile(t, s[:5], 0.9, 5)
52+
assertPercentile(t, s[:5], 1.1, 0)
53+
assertPercentile(t, []uint64{}, 0, 0)
54+
assertPercentile(t, s, 0, 1)
55+
assertPercentile(t, s[:11], 1, 11)
56+
assertPercentile(t, s, 1.0, 105)
4957
}
5058

5159
func TestMean(t *testing.T) {

0 commit comments

Comments
 (0)