From ed551131f2cd1946628f3650011499fc498968e8 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Mon, 25 Jul 2016 09:12:58 -0700 Subject: [PATCH] commands: fix panic when expected files field is nil License: MIT Signed-off-by: Jeromy --- commands/http/parse.go | 10 +++++++--- commands/request.go | 3 ++- test/sharness/t0600-issues-and-regressions-online.sh | 12 ++++++++++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/commands/http/parse.go b/commands/http/parse.go index c579a0394..f87c55579 100644 --- a/commands/http/parse.go +++ b/commands/http/parse.go @@ -104,13 +104,17 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) { contentType := r.Header.Get(contentTypeHeader) mediatype, _, _ := mime.ParseMediaType(contentType) - var f *files.MultipartFile + var f files.File if mediatype == "multipart/form-data" { - f = &files.MultipartFile{Mediatype: mediatype} - f.Reader, err = r.MultipartReader() + reader, err := r.MultipartReader() if err != nil { return nil, err } + + f = &files.MultipartFile{ + Mediatype: mediatype, + Reader: reader, + } } // if there is a required filearg, error if no files were provided diff --git a/commands/request.go b/commands/request.go index 38fd54240..05b90b965 100644 --- a/commands/request.go +++ b/commands/request.go @@ -231,7 +231,8 @@ func (r *request) VarArgs(f func(string) error) error { } if r.files == nil { - return fmt.Errorf("expected more arguments from stdin") + log.Warning("expected more arguments from stdin") + return nil } fi, err := r.files.NextFile() diff --git a/test/sharness/t0600-issues-and-regressions-online.sh b/test/sharness/t0600-issues-and-regressions-online.sh index 8b1885da3..e899060bb 100755 --- a/test/sharness/t0600-issues-and-regressions-online.sh +++ b/test/sharness/t0600-issues-and-regressions-online.sh @@ -10,15 +10,23 @@ test_launch_ipfs_daemon # Tests go here -test_expect_sucess "commands command with flag flags works via HTTP API - #2301" ' +test_expect_success "commands command with flag flags works via HTTP API - #2301" ' curl "http://$API_ADDR/api/v0/commands?flags" | grep "verbose" ' -test_expect_sucess "ipfs refs local over HTTP API returns NDJOSN not flat - #2803" ' +test_expect_success "ipfs refs local over HTTP API returns NDJOSN not flat - #2803" ' echo "Hello World" | ipfs add && curl "http://$API_ADDR/api/v0/refs/local" | grep "Ref" | grep "Err" ' +test_expect_success "args expecting stdin dont crash when not given" ' + curl "$API_ADDR/api/v0/bootstrap/add" > result +' + +test_expect_success "no panic traces on daemon" ' + test_expect_failure grep "nil pointer dereference" daemon_err +' + test_kill_ipfs_daemon test_done