From fe26ba2db0213a25f4e2a81b1513898aa3a00da0 Mon Sep 17 00:00:00 2001 From: Sylvain Baubeau Date: Wed, 26 Aug 2020 10:12:55 +0200 Subject: [PATCH] Set default values for link attributes When deserializing a link related netlink message, if no network namespace id attribute is specified, we need to set it to -1 otherwise it defaults to 0 which is a valid id. Using NewLinkAttrs sets the default values and allows the user to distinguish an empty value from a missing value --- link.go | 3 ++- link_linux.go | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/link.go b/link.go index d534317..c9358e7 100644 --- a/link.go +++ b/link.go @@ -103,7 +103,8 @@ func (s LinkOperState) String() string { // NewLinkAttrs returns LinkAttrs structure filled with default values func NewLinkAttrs() LinkAttrs { return LinkAttrs{ - TxQLen: -1, + NetNsID: -1, + TxQLen: -1, } } diff --git a/link_linux.go b/link_linux.go index 8f08534..7c56698 100644 --- a/link_linux.go +++ b/link_linux.go @@ -1566,7 +1566,11 @@ func LinkDeserialize(hdr *unix.NlMsghdr, m []byte) (Link, error) { return nil, err } - base := LinkAttrs{Index: int(msg.Index), RawFlags: msg.Flags, Flags: linkFlags(msg.Flags), EncapType: msg.EncapType()} + base := NewLinkAttrs() + base.Index = int(msg.Index) + base.RawFlags = msg.Flags + base.Flags = linkFlags(msg.Flags) + base.EncapType = msg.EncapType() if msg.Flags&unix.IFF_PROMISC != 0 { base.Promisc = 1 }