Skip to content

Commit ba1eb37

Browse files
committed
ovsnl: DRY out mininum length handling in UnmarshalBinary
1 parent af2cd53 commit ba1eb37

File tree

2 files changed

+9
-25
lines changed

2 files changed

+9
-25
lines changed

ovsnl/datapath.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package ovsnl
1616

1717
import (
1818
"encoding/binary"
19-
"fmt"
2019

2120
"github.com/digitalocean/go-openvswitch/ovsnl/internal/ovsh"
2221
"github.com/mdlayher/genetlink"
@@ -139,8 +138,9 @@ func parseDatapaths(msgs []genetlink.Message) ([]Datapath, error) {
139138
Index: int(h.Ifindex),
140139
}
141140

142-
// Skip the header to parse attributes.
143-
attrs, err := netlink.UnmarshalAttributes(m.Data[sizeofHeader:])
141+
// Skip past the header to parse attributes.
142+
hdrSize := binary.Size(h)
143+
attrs, err := netlink.UnmarshalAttributes(m.Data[hdrSize:])
144144
if err != nil {
145145
return nil, err
146146
}
@@ -170,20 +170,8 @@ func parseDatapaths(msgs []genetlink.Message) ([]Datapath, error) {
170170
return dps, nil
171171
}
172172

173-
// Sizes of various structures, used for safety checks during unmarshaling.
174-
var (
175-
sizeofHeader = binary.Size(ovsh.Header{})
176-
sizeofDPStats = binary.Size((ovsh.DPStats{}))
177-
sizeofDPMegaflowStats = binary.Size((ovsh.DPMegaflowStats{}))
178-
)
179-
180173
// parseDPStats converts a byte slice into DatapathStats.
181174
func parseDPStats(b []byte) (DatapathStats, error) {
182-
// Verify that the byte slice is the correct length before unmarshaling.
183-
if want, got := sizeofDPStats, len(b); want != got {
184-
return DatapathStats{}, fmt.Errorf("unexpected datapath stats structure size, want %d, got %d", want, got)
185-
}
186-
187175
s := new(ovsh.DPStats)
188176
err := ovsh.UnmarshalBinary(b, s)
189177
if err != nil {
@@ -200,11 +188,6 @@ func parseDPStats(b []byte) (DatapathStats, error) {
200188

201189
// parseDPMegaflowStats converts a byte slice into DatapathMegaflowStats.
202190
func parseDPMegaflowStats(b []byte) (DatapathMegaflowStats, error) {
203-
// Verify that the byte slice is the correct length before unmarshaling.
204-
if want, got := sizeofDPMegaflowStats, len(b); want != got {
205-
return DatapathMegaflowStats{}, fmt.Errorf("unexpected datapath megaflow stats structure size, want %d, got %d", want, got)
206-
}
207-
208191
s := new(ovsh.DPMegaflowStats)
209192
err := ovsh.UnmarshalBinary(b, s)
210193
if err != nil {
@@ -219,11 +202,6 @@ func parseDPMegaflowStats(b []byte) (DatapathMegaflowStats, error) {
219202

220203
// parseHeader converts a byte slice into ovsh.Header.
221204
func parseHeader(b []byte) (ovsh.Header, error) {
222-
// Verify we have enough data for what we are expecting before unmarshaling.
223-
if l := len(b); l < sizeofHeader {
224-
return ovsh.Header{}, fmt.Errorf("not enough data for OVS message header: %d bytes", l)
225-
}
226-
227205
h := new(ovsh.Header)
228206
err := ovsh.UnmarshalBinary(b, h)
229207
if err != nil {

ovsnl/internal/ovsh/binary.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package ovsh
1717
import (
1818
"bytes"
1919
"encoding/binary"
20+
"fmt"
2021
)
2122

2223
// ovsTypes defines a type set of the types defined in struct.go
@@ -33,6 +34,11 @@ func MarshalBinary[T ovsTypes](data *T) ([]byte, error) {
3334

3435
// UnmarshalBinary is a generic binary unmarshaling function for the type defined in struct.go
3536
func UnmarshalBinary[T ovsTypes](data []byte, dst *T) error {
37+
// Verify that the byte slice has enough data before unmarshaling.
38+
if want, got := binary.Size(*dst), len(data); got < want {
39+
return fmt.Errorf("unexpected size of struct %T, want at least %d, got %d", *dst, want, got)
40+
}
41+
3642
*dst = *new(T) // reset the contents, just to be safe
3743
buf := bytes.NewBuffer(data)
3844
return binary.Read(buf, binary.NativeEndian, dst)

0 commit comments

Comments
 (0)