Skip to content

Commit e3c2916

Browse files
committed
refactor(eviction): replace unsafe int→uint32 casts with safe converter
Replace two `//nolint:gosec` suppressed `uint32(...)` casts in `Sharded` with explicit calls to `converters.ToUint32` from `github.com/hyp3rd/sectools`. - `NewSharded`: compute shard mask via `converters.ToUint32(shardCount - 1)` and propagate any conversion error instead of silencing the lint warning. - `Evict`: convert `len(s.shards)` via `converters.ToUint32`, returning a zero-value on error rather than casting unchecked. - Add the `sectools/pkg/converters` import to `sharded.go`. - Register unsharded in `cspell.config.yaml` to keep spell-check clean.
1 parent 4a9f54f commit e3c2916

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

cspell.config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ words:
159159
- traefik
160160
- ugorji
161161
- unmarshals
162+
- unsharded
162163
- upserted
163164
- upserts
164165
- varnamelen

pkg/eviction/sharded.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"sync/atomic"
55

66
"github.com/hyp3rd/ewrap"
7+
"github.com/hyp3rd/sectools/pkg/converters"
78

89
"github.com/hyp3rd/hypercache/internal/sentinel"
910
cachev2 "github.com/hyp3rd/hypercache/pkg/cache/v2"
@@ -52,9 +53,15 @@ func NewSharded(algorithmName string, totalCapacity, shardCount int) (*Sharded,
5253
shards[i] = shard
5354
}
5455

56+
// shardCount validated power-of-two above
57+
mask, err := converters.ToUint32(shardCount - 1)
58+
if err != nil {
59+
return nil, ewrap.Wrapf(err, "shardCount %d", shardCount)
60+
}
61+
5562
return &Sharded{
5663
shards: shards,
57-
mask: uint32(shardCount - 1), //nolint:gosec // shardCount validated power-of-two above
64+
mask: mask,
5865
}, nil
5966
}
6067

@@ -82,7 +89,11 @@ func (s *Sharded) Delete(key string) {
8289
// always hitting the first non-empty one — relevant when one shard sees
8390
// disproportionately fewer Set calls than others.
8491
func (s *Sharded) Evict() (string, bool) {
85-
n := uint32(len(s.shards)) //nolint:gosec // len(s.shards) bounded by shardCount param
92+
// len(s.shards) bounded by shardCount param
93+
n, err := converters.ToUint32(len(s.shards))
94+
if err != nil {
95+
return "", false
96+
}
8697

8798
start := s.evictCursor.Add(1) - 1
8899

0 commit comments

Comments
 (0)