Browse Source

Do not use fmt.Sprintf() for plain error strings. Fixes #388.

Dmitry Panov 3 years ago
parent
commit
4418d4575a
2 changed files with 15 additions and 1 deletions
  1. 6 1
      runtime.go
  2. 9 0
      runtime_test.go

+ 6 - 1
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 {
 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)})
 	return r.builtin_new(typ, []Value{newStringValue(msg)})
 }
 }
 
 

+ 9 - 0
runtime_test.go

@@ -2375,6 +2375,15 @@ func TestErrorStack(t *testing.T) {
 	testScript(SCRIPT, _undefined, 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) {
 func TestArrayConcatSparse(t *testing.T) {
 function foo(a,b,c)
 function foo(a,b,c)