diff --git a/cache.go b/cache.go index d8e053c..8a58f60 100644 --- a/cache.go +++ b/cache.go @@ -70,11 +70,13 @@ var ( func init() { parallelism := xruntime.Parallelism() roundedParallelism := int(xmath.RoundUpPowerOf2(parallelism)) + //nolint:gosec // there will never be an overflow maxWriteBufferSize = uint32(128 * roundedParallelism) maxStripedBufferSize = 4 * roundedParallelism } func getTTL(ttl time.Duration) uint32 { + //nolint:gosec // there will never be an overflow return uint32((ttl + time.Second - 1) / time.Second) } @@ -132,14 +134,15 @@ func newCache[K comparable, V any](b *Builder[K, V]) *Cache[K, V] { } cache := &Cache[K, V]{ - nodeManager: nodeManager, - hashmap: hashmap, - stats: newStatsCollector(b.statsCollector), - logger: b.logger, - stripedBuffer: stripedBuffer, - writeBuffer: queue.NewGrowable[task[K, V]](minWriteBufferSize, maxWriteBufferSize), - doneClear: make(chan struct{}), - doneClose: make(chan struct{}, 1), + nodeManager: nodeManager, + hashmap: hashmap, + stats: newStatsCollector(b.statsCollector), + logger: b.logger, + stripedBuffer: stripedBuffer, + writeBuffer: queue.NewGrowable[task[K, V]](minWriteBufferSize, maxWriteBufferSize), + doneClear: make(chan struct{}), + doneClose: make(chan struct{}, 1), + //nolint:gosec // there will never be an overflow mask: uint32(maxStripedBufferSize - 1), weigher: b.weigher, deletionListener: b.deletionListener, diff --git a/cmd/generator/main.go b/cmd/generator/main.go index 67d3df6..86d0b6a 100644 --- a/cmd/generator/main.go +++ b/cmd/generator/main.go @@ -152,6 +152,7 @@ func (g *generator) printStructComment() { i := 2 for _, f := range declaredFeatures { if g.features[f] { + //nolint:staticcheck // used only for unicode featureTitle := strings.Title(strings.ToLower(f.name)) g.p("//") g.p("// %d. %s", i, featureTitle) diff --git a/internal/hashtable/map.go b/internal/hashtable/map.go index a863eb1..92b492e 100644 --- a/internal/hashtable/map.go +++ b/internal/hashtable/map.go @@ -142,6 +142,7 @@ func newMap[K comparable, V any](nodeManager *node.Manager[K, V], size int) *Map if size <= minNodeCount { t = newTable(minBucketCount, maphash.NewHasher[K]()) } else { + //nolint:gosec // there will never be an overflow bucketCount := xmath.RoundUpPowerOf2(uint32(size / bucketSize)) t = newTable(int(bucketCount), maphash.NewHasher[K]()) } diff --git a/internal/lossy/buffer.go b/internal/lossy/buffer.go index e262439..5a81df6 100644 --- a/internal/lossy/buffer.go +++ b/internal/lossy/buffer.go @@ -86,6 +86,7 @@ func (b *Buffer[K, V]) Add(n node.Node[K, V]) *PolicyBuffers[K, V] { } if b.tail.CompareAndSwap(tail, tail+1) { // success + //nolint:gosec // there will never be an overflow index := int(tail & mask) atomic.StorePointer(&b.buffer[index], n.AsPointer()) if size == capacity-1 { @@ -97,6 +98,7 @@ func (b *Buffer[K, V]) Add(n node.Node[K, V]) *PolicyBuffers[K, V] { pb := (*PolicyBuffers[K, V])(b.policyBuffers) for i := 0; i < capacity; i++ { + //nolint:gosec // there will never be an overflow index := int(head & mask) v := atomic.LoadPointer(&b.buffer[index]) if v != nil { diff --git a/internal/unixtime/unixtime.go b/internal/unixtime/unixtime.go index a446921..38f7b19 100644 --- a/internal/unixtime/unixtime.go +++ b/internal/unixtime/unixtime.go @@ -42,6 +42,7 @@ func startTimer() { for { select { case t := <-ticker.C: + //nolint:gosec // there will never be an overflow atomic.StoreUint32(&now, uint32(t.Unix()-StartTime())) case <-done: return diff --git a/internal/xruntime/runtime_1.22.go b/internal/xruntime/runtime_1.22.go index acfb18b..4827a34 100644 --- a/internal/xruntime/runtime_1.22.go +++ b/internal/xruntime/runtime_1.22.go @@ -21,5 +21,6 @@ import ( ) func Fastrand() uint32 { + //nolint:gosec // we don't need a cryptographically secure random number generator return rand.Uint32() } diff --git a/internal/xruntime/xruntime.go b/internal/xruntime/xruntime.go index 6c4e486..9c5de93 100644 --- a/internal/xruntime/xruntime.go +++ b/internal/xruntime/xruntime.go @@ -25,7 +25,9 @@ const ( // Parallelism returns the maximum possible number of concurrently running goroutines. func Parallelism() uint32 { + //nolint:gosec // there will never be an overflow maxProcs := uint32(runtime.GOMAXPROCS(0)) + //nolint:gosec // there will never be an overflow numCPU := uint32(runtime.NumCPU()) if maxProcs < numCPU { return maxProcs diff --git a/stats/counter.go b/stats/counter.go index c1ae40f..0c10109 100644 --- a/stats/counter.go +++ b/stats/counter.go @@ -65,7 +65,8 @@ func (c *Counter) Snapshot() Stats { rejectedSets: c.rejectedSets.Value(), loadSuccesses: c.loadSuccesses.Value(), loadFailures: c.loadFailures.Value(), - totalLoadTime: time.Duration(totalLoadTime), + //nolint:gosec // overflow is handled above + totalLoadTime: time.Duration(totalLoadTime), } } diff --git a/stats/stats.go b/stats/stats.go index 0356bc5..379d923 100644 --- a/stats/stats.go +++ b/stats/stats.go @@ -130,6 +130,7 @@ func (s Stats) AverageLoadPenalty() time.Duration { if loads > uint64(math.MaxInt64) { return s.totalLoadTime / time.Duration(math.MaxInt64) } + //nolint:gosec // overflow is handled above return s.totalLoadTime / time.Duration(loads) }