summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/header
diff options
context:
space:
mode:
authorJulian Elischer <jrelis@google.com>2020-11-12 17:46:37 -0800
committergVisor bot <gvisor-bot@google.com>2020-11-12 17:50:24 -0800
commitd700ba22abb9e5f29749cc3843991c31dc00384d (patch)
tree973fca5f49ff91e7e169cf730aaa5fbe324ab723 /pkg/tcpip/header
parent74be0dd0d59d468a9ebdcdf52f68eda78f4ea42a (diff)
Pad with a loop rather than a copy from an allocation.
Add a unit test for ipv4.Encode and a round trip test. PiperOrigin-RevId: 342169517
Diffstat (limited to 'pkg/tcpip/header')
-rw-r--r--pkg/tcpip/header/ipv4.go10
1 files changed, 8 insertions, 2 deletions
diff --git a/pkg/tcpip/header/ipv4.go b/pkg/tcpip/header/ipv4.go
index 7e32b31b4..3d465e28a 100644
--- a/pkg/tcpip/header/ipv4.go
+++ b/pkg/tcpip/header/ipv4.go
@@ -374,8 +374,14 @@ func (b IPv4) Encode(i *IPv4Fields) {
if hdrLen > len(b) {
panic(fmt.Sprintf("encode received %d bytes, wanted >= %d", len(b), hdrLen))
}
- if aLen != copy(b[options:], i.Options) {
- _ = copy(b[options+len(i.Options):options+aLen], []byte{0, 0, 0, 0})
+ opts := b[options:]
+ // This avoids bounds checks on the next line(s) which would happen even
+ // if there's no work to do.
+ if n := copy(opts, i.Options); n != aLen {
+ padding := opts[n:][:aLen-n]
+ for i := range padding {
+ padding[i] = 0
+ }
}
}
b.SetHeaderLength(uint8(hdrLen))