From 10ad07b228278b145b6b7221e529cc35af4b5d9e Mon Sep 17 00:00:00 2001 From: Jeromy Date: Wed, 17 Feb 2016 12:08:21 -0800 Subject: [PATCH] fix panic in cli arg parsing License: MIT Signed-off-by: Jeromy --- bin/gencmdref | 2 +- commands/cli/parse.go | 9 ++++++++- commands/cli/parse_test.go | 21 +++++++++++---------- core/commands/dht.go | 2 +- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/bin/gencmdref b/bin/gencmdref index e8a9ea62e79f..d15bed5598a9 100755 --- a/bin/gencmdref +++ b/bin/gencmdref @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python2 import os import sys diff --git a/commands/cli/parse.go b/commands/cli/parse.go index ba658a89c393..0e22d8f0f7f9 100644 --- a/commands/cli/parse.go +++ b/commands/cli/parse.go @@ -297,10 +297,17 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi var err error if argDef.Type == cmds.ArgString { - if stdin == nil || !argDef.SupportsStdin { + if stdin == nil { // add string values stringArgs, inputs = appendString(stringArgs, inputs) + } else if !argDef.SupportsStdin { + if len(inputs) == 0 { + // failure case, we have stdin, but our current + // argument doesnt want stdin + break + } + stringArgs, inputs = appendString(stringArgs, inputs) } else { if len(inputs) > 0 { // don't use stdin if we have inputs diff --git a/commands/cli/parse_test.go b/commands/cli/parse_test.go index 8a2e9967b838..54f60bf69bf0 100644 --- a/commands/cli/parse_test.go +++ b/commands/cli/parse_test.go @@ -212,7 +212,7 @@ func TestArgumentParsing(t *testing.T) { } } - testFail := func(cmd words, msg string) { + testFail := func(cmd words, fi *os.File, msg string) { _, _, _, err := Parse(cmd, nil, rootCmd) if err == nil { t.Errorf("Should have failed: %v", msg) @@ -220,18 +220,18 @@ func TestArgumentParsing(t *testing.T) { } test([]string{"noarg"}, nil, []string{}) - testFail([]string{"noarg", "value!"}, "provided an arg, but command didn't define any") + testFail([]string{"noarg", "value!"}, nil, "provided an arg, but command didn't define any") test([]string{"onearg", "value!"}, nil, []string{"value!"}) - testFail([]string{"onearg"}, "didn't provide any args, arg is required") + testFail([]string{"onearg"}, nil, "didn't provide any args, arg is required") test([]string{"twoargs", "value1", "value2"}, nil, []string{"value1", "value2"}) - testFail([]string{"twoargs", "value!"}, "only provided 1 arg, needs 2") - testFail([]string{"twoargs"}, "didn't provide any args, 2 required") + testFail([]string{"twoargs", "value!"}, nil, "only provided 1 arg, needs 2") + testFail([]string{"twoargs"}, nil, "didn't provide any args, 2 required") test([]string{"variadic", "value!"}, nil, []string{"value!"}) test([]string{"variadic", "value1", "value2", "value3"}, nil, []string{"value1", "value2", "value3"}) - testFail([]string{"variadic"}, "didn't provide any args, 1 required") + testFail([]string{"variadic"}, nil, "didn't provide any args, 1 required") test([]string{"optional", "value!"}, nil, []string{"value!"}) test([]string{"optional"}, nil, []string{}) @@ -239,14 +239,14 @@ func TestArgumentParsing(t *testing.T) { test([]string{"optionalsecond", "value!"}, nil, []string{"value!"}) test([]string{"optionalsecond", "value1", "value2"}, nil, []string{"value1", "value2"}) - testFail([]string{"optionalsecond"}, "didn't provide any args, 1 required") - testFail([]string{"optionalsecond", "value1", "value2", "value3"}, "provided too many args, takes 2 maximum") + testFail([]string{"optionalsecond"}, nil, "didn't provide any args, 1 required") + testFail([]string{"optionalsecond", "value1", "value2", "value3"}, nil, "provided too many args, takes 2 maximum") test([]string{"reversedoptional", "value1", "value2"}, nil, []string{"value1", "value2"}) test([]string{"reversedoptional", "value!"}, nil, []string{"value!"}) - testFail([]string{"reversedoptional"}, "didn't provide any args, 1 required") - testFail([]string{"reversedoptional", "value1", "value2", "value3"}, "provided too many args, only takes 1") + testFail([]string{"reversedoptional"}, nil, "didn't provide any args, 1 required") + testFail([]string{"reversedoptional", "value1", "value2", "value3"}, nil, "provided too many args, only takes 1") // Use a temp file to simulate stdin fileToSimulateStdin := func(t *testing.T, content string) *os.File { @@ -296,6 +296,7 @@ func TestArgumentParsing(t *testing.T) { fstdin = fileToSimulateStdin(t, "stdin1") test([]string{"stdinenablednotvariadic2args", "value1"}, fstdin, []string{"value1", "stdin1"}) test([]string{"stdinenablednotvariadic2args", "value1", "value2"}, fstdin, []string{"value1", "value2"}) + testFail([]string{"stdinenablednotvariadic2args"}, fstdin, "cant use stdin for non stdin arg") fstdin = fileToSimulateStdin(t, "stdin1") test([]string{"noarg"}, fstdin, []string{}) diff --git a/core/commands/dht.go b/core/commands/dht.go index 094db240ee5a..1236417dff68 100644 --- a/core/commands/dht.go +++ b/core/commands/dht.go @@ -12,8 +12,8 @@ import ( notif "github.com/ipfs/go-ipfs/notifications" path "github.com/ipfs/go-ipfs/path" ipdht "github.com/ipfs/go-ipfs/routing/dht" - u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util" peer "gx/ipfs/QmUBogf4nUefBjmYjn6jfsfPJRkmDGSeMhNj4usRKq69f4/go-libp2p/p2p/peer" + u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util" ) var ErrNotDHT = errors.New("routing service is not a DHT")