Procházet zdrojové kódy

Merge pull request #2 from enisoc/format-string

Don't pass arbitrary strings as Errorf() format strings.
Dave Cheney před 10 roky
rodič
revize
4a91b9e367
2 změnil soubory, kde provedl 7 přidání a 4 odebrání
  1. 2 1
      errors.go
  2. 5 3
      errors_test.go

+ 2 - 1
errors.go

@@ -2,6 +2,7 @@
 package errors
 
 import (
+	"errors"
 	"fmt"
 	"io"
 	"os"
@@ -33,7 +34,7 @@ func New(text string) error {
 		error
 		loc
 	}{
-		fmt.Errorf(text),
+		errors.New(text),
 		loc(pc),
 	}
 }

+ 5 - 3
errors_test.go

@@ -2,6 +2,7 @@ package errors
 
 import (
 	"bytes"
+	"errors"
 	"fmt"
 	"io"
 	"reflect"
@@ -16,6 +17,7 @@ func TestNew(t *testing.T) {
 		{"", fmt.Errorf("")},
 		{"foo", fmt.Errorf("foo")},
 		{"foo", New("foo")},
+    {"string with format specifiers: %v", errors.New("string with format specifiers: %v")},
 	}
 
 	for _, tt := range tests {
@@ -121,13 +123,13 @@ func TestFprint(t *testing.T) {
 		want: "cause error\nEOF\n",
 	}, {
 		err:  x, // return from errors.New
-		want: "github.com/pkg/errors/errors_test.go:104: error\n",
+		want: "github.com/pkg/errors/errors_test.go:106: error\n",
 	}, {
 		err:  Wrap(x, "message"),
-		want: "github.com/pkg/errors/errors_test.go:126: message\ngithub.com/pkg/errors/errors_test.go:104: error\n",
+		want: "github.com/pkg/errors/errors_test.go:128: message\ngithub.com/pkg/errors/errors_test.go:106: error\n",
 	}, {
 		err:  Wrap(Wrap(x, "message"), "another message"),
-		want: "github.com/pkg/errors/errors_test.go:129: another message\ngithub.com/pkg/errors/errors_test.go:129: message\ngithub.com/pkg/errors/errors_test.go:104: error\n",
+		want: "github.com/pkg/errors/errors_test.go:131: another message\ngithub.com/pkg/errors/errors_test.go:131: message\ngithub.com/pkg/errors/errors_test.go:106: error\n",
 	}}
 
 	for i, tt := range tests {