Skip to content

Commit 2742887

Browse files
committed
Guard against err being nil
1 parent 2847c66 commit 2742887

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

conntrack_linux.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,15 @@ func (h *Handle) ConntrackDeleteFilter(table ConntrackTableType, family InetFami
161161
func (h *Handle) ConntrackDeleteFilters(table ConntrackTableType, family InetFamily, filters ...CustomConntrackFilter) (uint, error) {
162162
var errMsgs []string
163163
res, err := h.dumpConntrackTable(table, family)
164-
if err != nil && !errors.Is(err, ErrDumpInterrupted) {
165-
return 0, err
164+
if err != nil {
165+
if !errors.Is(err, ErrDumpInterrupted) {
166+
return 0, err
167+
}
168+
// This allows us to at least do a best effort to try to clean the
169+
// entries matching the filter.
170+
errMsgs = append(errMsgs, err.Error())
166171
}
167172

168-
errMsgs = append(errMsgs, err.Error())
169-
170173
var matched uint
171174
for _, dataRaw := range res {
172175
flow := parseRawData(dataRaw)
@@ -220,10 +223,11 @@ type ProtoInfo interface {
220223
type ProtoInfoTCP struct {
221224
State uint8
222225
}
226+
223227
// Protocol returns "tcp".
224-
func (*ProtoInfoTCP) Protocol() string {return "tcp"}
228+
func (*ProtoInfoTCP) Protocol() string { return "tcp" }
225229
func (p *ProtoInfoTCP) toNlData() ([]*nl.RtAttr, error) {
226-
ctProtoInfo := nl.NewRtAttr(unix.NLA_F_NESTED | nl.CTA_PROTOINFO, []byte{})
230+
ctProtoInfo := nl.NewRtAttr(unix.NLA_F_NESTED|nl.CTA_PROTOINFO, []byte{})
227231
ctProtoInfoTCP := nl.NewRtAttr(unix.NLA_F_NESTED|nl.CTA_PROTOINFO_TCP, []byte{})
228232
ctProtoInfoTCPState := nl.NewRtAttr(nl.CTA_PROTOINFO_TCP_STATE, nl.Uint8Attr(p.State))
229233
ctProtoInfoTCP.AddChild(ctProtoInfoTCPState)
@@ -233,14 +237,16 @@ func (p *ProtoInfoTCP) toNlData() ([]*nl.RtAttr, error) {
233237
}
234238

235239
// ProtoInfoSCTP only supports the protocol name.
236-
type ProtoInfoSCTP struct {}
240+
type ProtoInfoSCTP struct{}
241+
237242
// Protocol returns "sctp".
238-
func (*ProtoInfoSCTP) Protocol() string {return "sctp"}
243+
func (*ProtoInfoSCTP) Protocol() string { return "sctp" }
239244

240245
// ProtoInfoDCCP only supports the protocol name.
241-
type ProtoInfoDCCP struct {}
246+
type ProtoInfoDCCP struct{}
247+
242248
// Protocol returns "dccp".
243-
func (*ProtoInfoDCCP) Protocol() string {return "dccp"}
249+
func (*ProtoInfoDCCP) Protocol() string { return "dccp" }
244250

245251
// The full conntrack flow structure is very complicated and can be found in the file:
246252
// http://git.netfilter.org/libnetfilter_conntrack/tree/include/internal/object.h
@@ -282,7 +288,7 @@ func (t *IPTuple) toNlData(family uint8) ([]*nl.RtAttr, error) {
282288
ctTupleProtoSrcPort := nl.NewRtAttr(nl.CTA_PROTO_SRC_PORT, nl.BEUint16Attr(t.SrcPort))
283289
ctTupleProto.AddChild(ctTupleProtoSrcPort)
284290
ctTupleProtoDstPort := nl.NewRtAttr(nl.CTA_PROTO_DST_PORT, nl.BEUint16Attr(t.DstPort))
285-
ctTupleProto.AddChild(ctTupleProtoDstPort, )
291+
ctTupleProto.AddChild(ctTupleProtoDstPort)
286292

287293
return []*nl.RtAttr{ctTupleIP, ctTupleProto}, nil
288294
}
@@ -359,7 +365,7 @@ func (s *ConntrackFlow) toNlData() ([]*nl.RtAttr, error) {
359365
// <len, CTA_TIMEOUT>
360366
// <BEuint64>
361367
// <len, NLA_F_NESTED|CTA_PROTOINFO>
362-
368+
363369
// CTA_TUPLE_ORIG
364370
ctTupleOrig := nl.NewRtAttr(unix.NLA_F_NESTED|nl.CTA_TUPLE_ORIG, nil)
365371
forwardFlowAttrs, err := s.Forward.toNlData(s.FamilyType)
@@ -542,12 +548,12 @@ func parseTimeStamp(r *bytes.Reader, readSize uint16) (tstart, tstop uint64) {
542548

543549
func parseProtoInfoTCPState(r *bytes.Reader) (s uint8) {
544550
binary.Read(r, binary.BigEndian, &s)
545-
r.Seek(nl.SizeofNfattr - 1, seekCurrent)
551+
r.Seek(nl.SizeofNfattr-1, seekCurrent)
546552
return s
547553
}
548554

549555
// parseProtoInfoTCP reads the entire nested protoinfo structure, but only parses the state attr.
550-
func parseProtoInfoTCP(r *bytes.Reader, attrLen uint16) (*ProtoInfoTCP) {
556+
func parseProtoInfoTCP(r *bytes.Reader, attrLen uint16) *ProtoInfoTCP {
551557
p := new(ProtoInfoTCP)
552558
bytesRead := 0
553559
for bytesRead < int(attrLen) {
@@ -661,7 +667,7 @@ func parseRawData(data []byte) *ConntrackFlow {
661667
switch t {
662668
case nl.CTA_MARK:
663669
s.Mark = parseConnectionMark(reader)
664-
case nl.CTA_LABELS:
670+
case nl.CTA_LABELS:
665671
s.Labels = parseConnectionLabels(reader)
666672
case nl.CTA_TIMEOUT:
667673
s.TimeOut = parseTimeOut(reader)

0 commit comments

Comments
 (0)