From 6016e31a7589227a4432afbf833c5f1aa9e0f66c Mon Sep 17 00:00:00 2001 From: Matt Layher Date: Wed, 1 Mar 2017 17:02:24 -0500 Subject: [PATCH] wifi: close Linux genetlink Conn on error --- client_linux.go | 3 +++ client_linux_test.go | 26 ++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/client_linux.go b/client_linux.go index 4d2c4e9..3bce719 100644 --- a/client_linux.go +++ b/client_linux.go @@ -59,6 +59,9 @@ func newClient() (*client, error) { func initClient(c genl) (*client, error) { family, err := c.GetFamily(nl80211.GenlName) if err != nil { + // Ensure the genl socket is closed on error to avoid leaking file + // descriptors. + _ = c.Close() return nil, err } diff --git a/client_linux_test.go b/client_linux_test.go index 836cce1..152b595 100644 --- a/client_linux_test.go +++ b/client_linux_test.go @@ -337,6 +337,23 @@ func TestLinux_clientStationInfoOK(t *testing.T) { } } +func TestLinux_initClientErrorCloseConn(t *testing.T) { + // Assume that nl80211 does not exist on this system. + // The genetlink Conn should be closed to avoid leaking file descriptors. + g := &testGENL{ + err: os.ErrNotExist, + } + + if _, err := initClient(g); err == nil { + t.Fatal("no error occurred, but expected one") + } + + if want, got := true, g.closed; want != got { + t.Fatalf("unexpected genetlink conn closed:\n- want: %v\n- got: %v", + want, got) + } +} + type check struct { Command uint8 Flags netlink.HeaderFlags @@ -413,14 +430,19 @@ var _ genl = &testGENL{} type testGENL struct { family genetlink.Family messages []genetlink.Message + closed bool + err error precheck func(m genetlink.Message, family uint16, flags netlink.HeaderFlags) } -func (g *testGENL) Close() error { return nil } +func (g *testGENL) Close() error { + g.closed = true + return nil +} func (g *testGENL) GetFamily(name string) (genetlink.Family, error) { - return g.family, nil + return g.family, g.err } func (g *testGENL) Execute(m genetlink.Message, family uint16, flags netlink.HeaderFlags) ([]genetlink.Message, error) {