Skip to content

Commit 44b2f1e

Browse files
author
Anthony Yeh
committed
Don't pass arbitrary strings as Errorf() format strings.
Sometimes they happen to contain format specifiers, which results in things like "%!v(MISSING)".
1 parent 92a59f4 commit 44b2f1e

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

errors.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package errors
33

44
import (
5+
"errors"
56
"fmt"
67
"io"
78
"os"
@@ -33,7 +34,7 @@ func New(text string) error {
3334
error
3435
loc
3536
}{
36-
fmt.Errorf(text),
37+
errors.New(text),
3738
loc(pc),
3839
}
3940
}

errors_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package errors
22

33
import (
44
"bytes"
5+
"errors"
56
"fmt"
67
"io"
78
"reflect"
@@ -16,6 +17,7 @@ func TestNew(t *testing.T) {
1617
{"", fmt.Errorf("")},
1718
{"foo", fmt.Errorf("foo")},
1819
{"foo", New("foo")},
20+
{"string with format specifiers: %v", errors.New("string with format specifiers: %v")},
1921
}
2022

2123
for _, tt := range tests {
@@ -121,13 +123,13 @@ func TestFprint(t *testing.T) {
121123
want: "cause error\nEOF\n",
122124
}, {
123125
err: x, // return from errors.New
124-
want: "github.com/pkg/errors/errors_test.go:104: error\n",
126+
want: "github.com/pkg/errors/errors_test.go:106: error\n",
125127
}, {
126128
err: Wrap(x, "message"),
127-
want: "github.com/pkg/errors/errors_test.go:126: message\ngithub.com/pkg/errors/errors_test.go:104: error\n",
129+
want: "github.com/pkg/errors/errors_test.go:128: message\ngithub.com/pkg/errors/errors_test.go:106: error\n",
128130
}, {
129131
err: Wrap(Wrap(x, "message"), "another message"),
130-
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",
132+
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",
131133
}}
132134

133135
for i, tt := range tests {

0 commit comments

Comments
 (0)