Use clear on a few more slices.

This is similar to pull request #9749.

PiperOrigin-RevId: 586512951
This commit is contained in:
Etienne Perot
2023-11-29 18:55:37 -08:00
committed by gVisor bot
parent d61fcf15de
commit 4b925cc1cd
6 changed files with 12 additions and 27 deletions
+1 -3
View File
@@ -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).
+2 -5
View File
@@ -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.
+1 -3
View File
@@ -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)
}
}
+1 -3
View File
@@ -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
+5 -11
View File
@@ -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
}
+2 -2
View File
@@ -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()