From 4418d4575a419bfb9a44250c8f2320f24b6c43b5 Mon Sep 17 00:00:00 2001 From: Dmitry Panov Date: Mon, 16 May 2022 13:39:00 +0100 Subject: [PATCH] Do not use fmt.Sprintf() for plain error strings. Fixes #388. --- runtime.go | 7 ++++++- runtime_test.go | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/runtime.go b/runtime.go index eace4d61..5ba15ba0 100644 --- a/runtime.go +++ b/runtime.go @@ -470,7 +470,12 @@ func (r *Runtime) typeErrorResult(throw bool, args ...interface{}) { } func (r *Runtime) newError(typ *Object, format string, args ...interface{}) Value { - msg := fmt.Sprintf(format, args...) + var msg string + if len(args) > 0 { + msg = fmt.Sprintf(format, args...) + } else { + msg = format + } return r.builtin_new(typ, []Value{newStringValue(msg)}) } diff --git a/runtime_test.go b/runtime_test.go index 29bd293c..03dfdceb 100644 --- a/runtime_test.go +++ b/runtime_test.go @@ -2375,6 +2375,15 @@ func TestErrorStack(t *testing.T) { testScript(SCRIPT, _undefined, t) } +func TestErrorFormatSymbols(t *testing.T) { + vm := New() + vm.Set("a", func() (Value, error) { return nil, errors.New("something %s %f") }) + _, err := vm.RunString("a()") + if !strings.Contains(err.Error(), "something %s %f") { + t.Fatalf("Wrong value %q", err.Error()) + } +} + /* func TestArrayConcatSparse(t *testing.T) { function foo(a,b,c)