From ae83b71b0890192437b0dc8f344893a0f53e21a9 Mon Sep 17 00:00:00 2001 From: James Nugent Date: Thu, 5 Jan 2017 07:58:22 -0600 Subject: [PATCH] cli: Fix panic on empty data argument to `kv put` Passing in an empty quoted argument from the shell currently panics as we never check the length being greater than 0 prior to indexing into the first rune, as illustrated in the test in this commit. We also fix the panic, treating an empty string for data as equivalent to not having passed it in the first place. --- command/kv_put.go | 5 +++++ command/kv_put_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/command/kv_put.go b/command/kv_put.go index 98c70b00c5..c03cf4f49b 100644 --- a/command/kv_put.go +++ b/command/kv_put.go @@ -236,6 +236,11 @@ func (c *KVPutCommand) dataFromArgs(args []string) (string, string, error) { key := args[0] data := args[1] + // Handle empty quoted shell parameters + if len(data) == 0 { + return key, "", nil + } + switch data[0] { case '@': data, err := ioutil.ReadFile(data[1:]) diff --git a/command/kv_put_test.go b/command/kv_put_test.go index 3735652c80..f691719b15 100644 --- a/command/kv_put_test.go +++ b/command/kv_put_test.go @@ -101,6 +101,34 @@ func TestKVPutCommand_Run(t *testing.T) { } } +func TestKVPutCommand_RunEmptyDataQuoted(t *testing.T) { + srv, client := testAgentWithAPIClient(t) + defer srv.Shutdown() + waitForLeader(t, srv.httpAddr) + + ui := new(cli.MockUi) + c := &KVPutCommand{Ui: ui} + + args := []string{ + "-http-addr=" + srv.httpAddr, + "foo", "", + } + + code := c.Run(args) + if code != 0 { + t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) + } + + data, _, err := client.KV().Get("foo", nil) + if err != nil { + t.Fatal(err) + } + + if data.Value != nil { + t.Errorf("bad: %#v", data.Value) + } +} + func TestKVPutCommand_RunBase64(t *testing.T) { srv, client := testAgentWithAPIClient(t) defer srv.Shutdown()