Skip to content

Commit e6d82b9

Browse files
authored
fix: remove unsafe pkg usage from util.mempool (#15428)
1 parent 336ceb7 commit e6d82b9

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

‎pkg/util/mempool/pool.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
package mempool
22

33
import (
4-
"errors"
54
"fmt"
65
"sync"
76
"time"
8-
"unsafe"
97

108
"github.com/dustin/go-humanize"
119
"github.com/prometheus/client_golang/prometheus"
1210
)
1311

1412
var (
15-
errSlabExhausted = errors.New("slab exhausted")
16-
17-
reasonSizeExceeded = "size-exceeded"
18-
reasonSlabExhausted = "slab-exhausted"
13+
reasonSizeExceeded = "size-exceeded"
1914
)
2015

2116
type slab struct {
22-
buffer chan unsafe.Pointer
17+
buffer chan []byte
2318
size, count int
2419
once sync.Once
2520
metrics *metrics
@@ -39,11 +34,10 @@ func newSlab(bufferSize, bufferCount int, m *metrics) *slab {
3934
}
4035

4136
func (s *slab) init() {
42-
s.buffer = make(chan unsafe.Pointer, s.count)
37+
s.buffer = make(chan []byte, s.count)
4338
for i := 0; i < s.count; i++ {
4439
buf := make([]byte, 0, s.size)
45-
ptr := unsafe.Pointer(unsafe.SliceData(buf)) //#nosec G103 -- Simple arena allocator implementation, does not appear to allow for any unsafe operations.
46-
s.buffer <- ptr
40+
s.buffer <- buf
4741
}
4842
s.metrics.availableBuffersPerSlab.WithLabelValues(s.name).Set(float64(s.count))
4943
}
@@ -54,8 +48,7 @@ func (s *slab) get(size int) ([]byte, error) {
5448

5549
waitStart := time.Now()
5650
// wait for available buffer on channel
57-
ptr := <-s.buffer
58-
buf := unsafe.Slice((*byte)(ptr), s.size) //#nosec G103 -- Simple arena allocator implementation, does not appear to allow for any unsafe operations.
51+
buf := <-s.buffer
5952
s.metrics.waitDuration.WithLabelValues(s.name).Observe(time.Since(waitStart).Seconds())
6053

6154
return buf[:size], nil
@@ -67,9 +60,8 @@ func (s *slab) put(buf []byte) {
6760
panic("slab is not initialized")
6861
}
6962

70-
ptr := unsafe.Pointer(unsafe.SliceData(buf)) //#nosec G103 -- Simple arena allocator implementation, does not appear to allow for any unsafe operations.
7163
// Note that memory is NOT zero'd on return, but since all allocations are of defined widths and we only ever then read a record of exactly that width into the allocation, it will always be overwritten before use and can't leak.
72-
s.buffer <- ptr
64+
s.buffer <- buf
7365
}
7466

7567
// MemPool is an Allocator implementation that uses a fixed size memory pool

0 commit comments

Comments
 (0)