From 4db753c0174420152957224d38fb6e8b6ae6908e Mon Sep 17 00:00:00 2001 From: Anusha Ragunathan Date: Tue, 25 Oct 2016 16:12:56 -0700 Subject: [PATCH] Fix stack overflow in ErrInAdequateCapacity. fmt package formats an error by calling its Error() method. This results in recursion. Fix this. Also remove dead code. Signed-off-by: Anusha Ragunathan --- plugin/store/store.go | 11 ----------- plugin/store/store_test.go | 34 ++++++++++++++++++++++++++++++++++ plugin/v2/plugin.go | 10 ++++++---- 3 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 plugin/store/store_test.go diff --git a/plugin/store/store.go b/plugin/store/store.go index d4d6499c9e..e1e41f38af 100644 --- a/plugin/store/store.go +++ b/plugin/store/store.go @@ -73,17 +73,6 @@ func (ps *Store) SetAll(plugins map[string]*v2.Plugin) { ps.plugins = plugins } -func (ps *Store) getByCap(name string, capability string) (*v2.Plugin, error) { - ps.RLock() - defer ps.RUnlock() - - p, err := ps.GetByName(name) - if err != nil { - return nil, err - } - return p.FilterByCap(capability) -} - func (ps *Store) getAllByCap(capability string) []plugingetter.CompatPlugin { ps.RLock() defer ps.RUnlock() diff --git a/plugin/store/store_test.go b/plugin/store/store_test.go new file mode 100644 index 0000000000..89071b997d --- /dev/null +++ b/plugin/store/store_test.go @@ -0,0 +1,34 @@ +package store + +import ( + "testing" + + "github.com/docker/docker/api/types" + "github.com/docker/docker/plugin/v2" +) + +func TestFilterByCapNeg(t *testing.T) { + p := v2.NewPlugin("test", "1234567890", "/run/docker", "latest") + + iType := types.PluginInterfaceType{"volumedriver", "docker", "1.0"} + i := types.PluginManifestInterface{"plugins.sock", []types.PluginInterfaceType{iType}} + p.PluginObj.Manifest.Interface = i + + _, err := p.FilterByCap("foobar") + if err == nil { + t.Fatalf("expected inadequate error, got %v", err) + } +} + +func TestFilterByCapPos(t *testing.T) { + p := v2.NewPlugin("test", "1234567890", "/run/docker", "latest") + + iType := types.PluginInterfaceType{"volumedriver", "docker", "1.0"} + i := types.PluginManifestInterface{"plugins.sock", []types.PluginInterfaceType{iType}} + p.PluginObj.Manifest.Interface = i + + _, err := p.FilterByCap("volumedriver") + if err != nil { + t.Fatalf("expected no error, got %v", err) + } +} diff --git a/plugin/v2/plugin.go b/plugin/v2/plugin.go index fc79fd8437..f5413c3a3a 100644 --- a/plugin/v2/plugin.go +++ b/plugin/v2/plugin.go @@ -29,10 +29,12 @@ type Plugin struct { const defaultPluginRuntimeDestination = "/run/docker/plugins" // ErrInadequateCapability indicates that the plugin did not have the requested capability. -type ErrInadequateCapability string +type ErrInadequateCapability struct { + cap string +} -func (cap ErrInadequateCapability) Error() string { - return fmt.Sprintf("plugin does not provide %q capability", cap) +func (e ErrInadequateCapability) Error() string { + return fmt.Sprintf("plugin does not provide %q capability", e.cap) } func newPluginObj(name, id, tag string) types.Plugin { @@ -75,7 +77,7 @@ func (p *Plugin) FilterByCap(capability string) (*Plugin, error) { return p, nil } } - return nil, ErrInadequateCapability(capability) + return nil, ErrInadequateCapability{capability} } // RemoveFromDisk deletes the plugin's runtime files from disk.