diff --git a/pkg/abi/linux/netdevice.go b/pkg/abi/linux/netdevice.go index e64d554df..b30b6d204 100644 --- a/pkg/abi/linux/netdevice.go +++ b/pkg/abi/linux/netdevice.go @@ -58,9 +58,7 @@ func (ifr *IFReq) Name() string { // SetName sets the name. func (ifr *IFReq) SetName(name string) { n := copy(ifr.IFName[:], []byte(name)) - for i := n; i < len(ifr.IFName); i++ { - ifr.IFName[i] = 0 - } + clear(ifr.IFName[n:]) } // SizeOfIFReq is the binary size of an IFReq struct (40 bytes). diff --git a/pkg/buffer/buffer.go b/pkg/buffer/buffer.go index 0962da698..3e585c008 100644 --- a/pkg/buffer/buffer.go +++ b/pkg/buffer/buffer.go @@ -189,12 +189,9 @@ func (b *Buffer) GrowTo(length int64, zero bool) { sz = int(length - b.size) } - // Zero the written section; note that this pattern is - // specifically recognized and optimized by the compiler. + // Zero the written section. if zero { - for i := v.write; i < v.write+sz; i++ { - v.chunk.data[i] = 0 - } + clear(v.chunk.data[v.write : v.write+sz]) } // Advance the index. diff --git a/pkg/coverage/coverage.go b/pkg/coverage/coverage.go index 0a7e5f585..11a42d415 100644 --- a/pkg/coverage/coverage.go +++ b/pkg/coverage/coverage.go @@ -106,9 +106,7 @@ func ClearCoverageData() { // which would drastically degrade performance. Slight discrepancies due to // racing is okay for the purposes of kcov. for _, counters := range coverdata.Counters { - for index := 0; index < len(counters); index++ { - counters[index] = 0 - } + clear(counters) } } diff --git a/pkg/sentry/kernel/sched/cpuset.go b/pkg/sentry/kernel/sched/cpuset.go index c6c436690..b10e010cf 100644 --- a/pkg/sentry/kernel/sched/cpuset.go +++ b/pkg/sentry/kernel/sched/cpuset.go @@ -88,9 +88,7 @@ func (c *CPUSet) ClearAbove(cpu uint) { return } (*c)[i] &^= 0xff << (cpu % bitsPerByte) - for i++; i < c.Size(); i++ { - (*c)[i] = 0 - } + clear((*c)[i+1 : c.Size()]) } // ForEachCPU iterates over the CPUSet and calls fn with the cpu index if diff --git a/pkg/tcpip/header/ndp_options.go b/pkg/tcpip/header/ndp_options.go index 746e5eaea..5fbae169a 100644 --- a/pkg/tcpip/header/ndp_options.go +++ b/pkg/tcpip/header/ndp_options.go @@ -339,8 +339,8 @@ func (b NDPOptions) Serialize(s NDPOptionsSerializer) int { used := o.serializeInto(b[2:]) // Zero out remaining (padding) bytes, if any exists. - for i := used + 2; i < l; i++ { - b[i] = 0 + if used+2 < l { + clear(b[used+2 : l]) } b = b[l:] @@ -566,9 +566,7 @@ func (o NDPPrefixInformation) serializeInto(b []byte) int { // Zero out the Reserved2 field. reserved2 := b[ndpPrefixInformationReserved2Offset:][:ndpPrefixInformationReserved2Length] - for i := range reserved2 { - reserved2[i] = 0 - } + clear(reserved2) return used } @@ -687,9 +685,7 @@ func (o NDPRecursiveDNSServer) serializeInto(b []byte) int { used := copy(b, o) // Zero out the reserved bytes that are before the Lifetime field. - for i := 0; i < ndpRecursiveDNSServerLifetimeOffset; i++ { - b[i] = 0 - } + clear(b[0:ndpRecursiveDNSServerLifetimeOffset]) return used } @@ -782,9 +778,7 @@ func (o NDPDNSSearchList) serializeInto(b []byte) int { used := copy(b, o) // Zero out the reserved bytes that are before the Lifetime field. - for i := 0; i < ndpDNSSearchListLifetimeOffset; i++ { - b[i] = 0 - } + clear(b[0:ndpDNSSearchListLifetimeOffset]) return used } diff --git a/pkg/tcpip/stack/stack.go b/pkg/tcpip/stack/stack.go index 532d24fd8..699da4fe1 100644 --- a/pkg/tcpip/stack/stack.go +++ b/pkg/tcpip/stack/stack.go @@ -964,14 +964,14 @@ func (s *Stack) removeNICLocked(id tcpip.NICID) tcpip.Error { // Remove routes in-place. n tracks the number of routes written. s.routeMu.Lock() n := 0 - for i, r := range s.routeTable { - s.routeTable[i] = tcpip.Route{} + for _, r := range s.routeTable { if r.NIC != id { // Keep this route. s.routeTable[n] = r n++ } } + clear(s.routeTable[n:]) s.routeTable = s.routeTable[:n] s.routeMu.Unlock()