From 665eceaec33d6837ca3246784b05efb38d7c1421 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Wed, 28 Apr 2021 13:08:33 +0100 Subject: [PATCH] librclone: catch panics at the language change boundary #4891 --- librclone/librclone/librclone.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/librclone/librclone/librclone.go b/librclone/librclone/librclone.go index eba479cbf..dabb02f0d 100644 --- a/librclone/librclone/librclone.go +++ b/librclone/librclone/librclone.go @@ -12,6 +12,7 @@ import ( "fmt" "net/http" "runtime" + "runtime/debug" "strings" "github.com/pkg/errors" @@ -77,8 +78,17 @@ func writeError(path string, in rc.Params, err error, status int) (string, int) // operations/uploadfile and core/command are not supported as they need request or response object // modified from handlePost in rcserver.go func RPC(method string, input string) (output string, status int) { - // create a buffer to capture the output in := make(rc.Params) + + // Catch panics + defer func() { + if r := recover(); r != nil { + output, status = writeError(method, in, fmt.Errorf("panic: %v\n%s", r, debug.Stack()), http.StatusInternalServerError) + return + } + }() + + // create a buffer to capture the output err := json.NewDecoder(strings.NewReader(input)).Decode(&in) if err != nil { return writeError(method, in, errors.Wrap(err, "failed to read input JSON"), http.StatusBadRequest)