@cryptotaxi247 / kubo / commits / dbf0185a9

fix panic in cli arg parsing

License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>

Jeromy committed Feb 17, 2016 at 12:08 UTC dbf0185a91b1170d664f8c406e6b5c210dfe2795
3 files changed +20 -12
commands/cli/parse.go
+8 -1
@@ -297,10 +297,17 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
297
298 var err error
299 if argDef.Type == cmds.ArgString {
300 - if stdin == nil || !argDef.SupportsStdin {
300 + if stdin == nil {
301 // add string values
302 stringArgs, inputs = appendString(stringArgs, inputs)
303 + } else if !argDef.SupportsStdin {
304 + if len(inputs) == 0 {
305 + // failure case, we have stdin, but our current
306 + // argument doesnt want stdin
307 + break
308 + }
309
310 + stringArgs, inputs = appendString(stringArgs, inputs)
311 } else {
312 if len(inputs) > 0 {
313 // don't use stdin if we have inputs
commands/cli/parse_test.go
+11 -10
@@ -212,7 +212,7 @@ func TestArgumentParsing(t *testing.T) {
212 }
213 }
214
215 - testFail := func(cmd words, msg string) {
215 + testFail := func(cmd words, fi *os.File, msg string) {
216 _, _, _, err := Parse(cmd, nil, rootCmd)
217 if err == nil {
218 t.Errorf("Should have failed: %v", msg)
@@ -220,18 +220,18 @@ func TestArgumentParsing(t *testing.T) {
220 }
221
222 test([]string{"noarg"}, nil, []string{})
223 - testFail([]string{"noarg", "value!"}, "provided an arg, but command didn't define any")
223 + testFail([]string{"noarg", "value!"}, nil, "provided an arg, but command didn't define any")
224
225 test([]string{"onearg", "value!"}, nil, []string{"value!"})
226 - testFail([]string{"onearg"}, "didn't provide any args, arg is required")
226 + testFail([]string{"onearg"}, nil, "didn't provide any args, arg is required")
227
228 test([]string{"twoargs", "value1", "value2"}, nil, []string{"value1", "value2"})
229 - testFail([]string{"twoargs", "value!"}, "only provided 1 arg, needs 2")
230 - testFail([]string{"twoargs"}, "didn't provide any args, 2 required")
229 + testFail([]string{"twoargs", "value!"}, nil, "only provided 1 arg, needs 2")
230 + testFail([]string{"twoargs"}, nil, "didn't provide any args, 2 required")
231
232 test([]string{"variadic", "value!"}, nil, []string{"value!"})
233 test([]string{"variadic", "value1", "value2", "value3"}, nil, []string{"value1", "value2", "value3"})
234 - testFail([]string{"variadic"}, "didn't provide any args, 1 required")
234 + testFail([]string{"variadic"}, nil, "didn't provide any args, 1 required")
235
236 test([]string{"optional", "value!"}, nil, []string{"value!"})
237 test([]string{"optional"}, nil, []string{})
@@ -239,14 +239,14 @@ func TestArgumentParsing(t *testing.T) {
239
240 test([]string{"optionalsecond", "value!"}, nil, []string{"value!"})
241 test([]string{"optionalsecond", "value1", "value2"}, nil, []string{"value1", "value2"})
242 - testFail([]string{"optionalsecond"}, "didn't provide any args, 1 required")
243 - testFail([]string{"optionalsecond", "value1", "value2", "value3"}, "provided too many args, takes 2 maximum")
242 + testFail([]string{"optionalsecond"}, nil, "didn't provide any args, 1 required")
243 + testFail([]string{"optionalsecond", "value1", "value2", "value3"}, nil, "provided too many args, takes 2 maximum")
244
245 test([]string{"reversedoptional", "value1", "value2"}, nil, []string{"value1", "value2"})
246 test([]string{"reversedoptional", "value!"}, nil, []string{"value!"})
247
248 - testFail([]string{"reversedoptional"}, "didn't provide any args, 1 required")
249 - testFail([]string{"reversedoptional", "value1", "value2", "value3"}, "provided too many args, only takes 1")
248 + testFail([]string{"reversedoptional"}, nil, "didn't provide any args, 1 required")
249 + testFail([]string{"reversedoptional", "value1", "value2", "value3"}, nil, "provided too many args, only takes 1")
250
251 // Use a temp file to simulate stdin
252 fileToSimulateStdin := func(t *testing.T, content string) *os.File {
@@ -296,6 +296,7 @@ func TestArgumentParsing(t *testing.T) {
296 fstdin = fileToSimulateStdin(t, "stdin1")
297 test([]string{"stdinenablednotvariadic2args", "value1"}, fstdin, []string{"value1", "stdin1"})
298 test([]string{"stdinenablednotvariadic2args", "value1", "value2"}, fstdin, []string{"value1", "value2"})
299 + testFail([]string{"stdinenablednotvariadic2args"}, fstdin, "cant use stdin for non stdin arg")
300
301 fstdin = fileToSimulateStdin(t, "stdin1")
302 test([]string{"noarg"}, fstdin, []string{})
core/commands/dht.go
+1 -1
@@ -12,8 +12,8 @@ import (
12 notif "github.com/ipfs/go-ipfs/notifications"
13 path "github.com/ipfs/go-ipfs/path"
14 ipdht "github.com/ipfs/go-ipfs/routing/dht"
15 - u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
15 peer "gx/ipfs/QmUBogf4nUefBjmYjn6jfsfPJRkmDGSeMhNj4usRKq69f4/go-libp2p/p2p/peer"
16 + u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
17 )
18
19 var ErrNotDHT = errors.New("routing service is not a DHT")