From b3e6aa9316188fc539e3be9ee8be37acf88db92c Mon Sep 17 00:00:00 2001 From: Bjorn Neergaard Date: Sat, 21 Jan 2023 16:23:07 -0700 Subject: [PATCH] libnetwork/netutils: clean up GenerateIfaceName netlink offers the netlink.LinkNotFoundError type, which we can use with errors.As() to detect a unused link name. Additionally, early return if GenerateRandomName fails, as reading random bytes should be a highly reliable operation, and otherwise the error would be swallowed by the fall-through return. Signed-off-by: Bjorn Neergaard --- libnetwork/netutils/utils_linux.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libnetwork/netutils/utils_linux.go b/libnetwork/netutils/utils_linux.go index 89aec1ac2a..761cd433a7 100644 --- a/libnetwork/netutils/utils_linux.go +++ b/libnetwork/netutils/utils_linux.go @@ -8,7 +8,6 @@ package netutils import ( "net" "os" - "strings" "github.com/docker/docker/libnetwork/ipamutils" "github.com/docker/docker/libnetwork/ns" @@ -49,11 +48,11 @@ func GenerateIfaceName(nlh *netlink.Handle, prefix string, len int) (string, err for i := 0; i < 3; i++ { name, err := GenerateRandomName(prefix, len) if err != nil { - continue + return "", err } _, err = linkByName(name) if err != nil { - if strings.Contains(err.Error(), "not found") { + if errors.As(err, &netlink.LinkNotFoundError{}) { return name, nil } return "", err