Skip to content

Commit 5008990

Browse files
barnabasbusaclaude
andcommitted
fix: resolve gosec and prealloc lint issues
- Preallocate peers slice with capacity - Add nolint:gosec annotations for safe integer conversions Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent ed3e760 commit 5008990

5 files changed

Lines changed: 7 additions & 7 deletions

File tree

pkg/beacon/default.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ func (d *Default) Healthy(ctx context.Context) (bool, error) {
373373
}
374374

375375
func (d *Default) Peers(ctx context.Context) (types.Peers, error) {
376-
peers := types.Peers{}
376+
peers := make(types.Peers, 0, len(d.nodes))
377377

378378
for _, node := range d.nodes {
379379
status := "connected"
@@ -750,7 +750,7 @@ func (d *Default) ListFinalizedSlots(ctx context.Context) ([]phase0.Slot, error)
750750

751751
latestSlot := phase0.Slot(uint64(finality.Finalized.Epoch) * uint64(sp.SlotsPerEpoch))
752752

753-
for i, val := uint64(latestSlot), uint64(latestSlot)-uint64(sp.SlotsPerEpoch)*uint64(d.config.HistoricalEpochCount); i > val; i -= uint64(sp.SlotsPerEpoch) {
753+
for i, val := uint64(latestSlot), uint64(latestSlot)-uint64(sp.SlotsPerEpoch)*uint64(d.config.HistoricalEpochCount); i > val; i -= uint64(sp.SlotsPerEpoch) { //nolint:gosec // values are always positive
754754
slots = append(slots, phase0.Slot(i))
755755
}
756756

pkg/beacon/expire_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ var (
1313
)
1414

1515
func CalculateSlotExpiration(slot phase0.Slot, slotsOfHistory int) phase0.Slot {
16-
return slot + phase0.Slot(slotsOfHistory)
16+
return slot + phase0.Slot(uint64(slotsOfHistory)) //nolint:gosec // slotsOfHistory is always positive
1717
}
1818

1919
func GetSlotTime(slot phase0.Slot, secondsPerSlot time.Duration, genesis time.Time) time.Time {
20-
return genesis.Add(time.Duration(slot) * secondsPerSlot)
20+
return genesis.Add(time.Duration(int64(slot)) * secondsPerSlot) //nolint:gosec // slot fits in int64
2121
}
2222

2323
func TestExpiresAtSlot(t *testing.T) {

pkg/eth/slot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type SlotTime struct {
1414
}
1515

1616
func CalculateSlotTime(slot phase0.Slot, genesisTime time.Time, durationPerSlot time.Duration) SlotTime {
17-
slotStartTime := genesisTime.Add(time.Duration(slot) * durationPerSlot).UTC()
17+
slotStartTime := genesisTime.Add(time.Duration(int64(slot)) * durationPerSlot).UTC() //nolint:gosec // slot fits in int64
1818

1919
return SlotTime{
2020
StartTime: slotStartTime,

pkg/service/eth/block_id.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func NewSlotFromString(id string) (phase0.Slot, error) {
8888
return 0, err
8989
}
9090

91-
return phase0.Slot(slot), nil
91+
return phase0.Slot(uint64(slot)), nil //nolint:gosec // slot parsed from string is validated
9292
}
9393

9494
func NewRootFromString(id string) (phase0.Root, error) {

pkg/service/eth/eth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ func (h *Handler) BlobSidecars(ctx context.Context, blockID BlockIdentifier, ind
572572

573573
// Find the sidecar with the given index
574574
for i, sidecar := range sidecars {
575-
if index == int(sidecar.Index) {
575+
if index == int(sidecar.Index) { //nolint:gosec // index is validated non-negative above
576576
filtered = append(filtered, sidecars[i])
577577

578578
break

0 commit comments

Comments
 (0)