Skip to content

Commit

Permalink
fix: panic if message format arg is not a string
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas committed Dec 6, 2023
1 parent 0a3ee63 commit 929325d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,11 @@ func formatMsgAndArgs(dflt string, msgAndArgs ...any) string {
if len(msgAndArgs) == 0 {
return dflt
}
return fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...)
format, ok := msgAndArgs[0].(string)
if !ok {
panic("message argument to assert function must be a fmt string")
}
return fmt.Sprintf(format, msgAndArgs[1:]...)
}

func needlePosition(haystack, needle string) (quotedHaystack, quotedNeedle, positions string) {
Expand Down
6 changes: 6 additions & 0 deletions assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ func TestIsError(t *testing.T) {
})
}

func TestInvalidFormatMsg(t *testing.T) {
Panics(t, func() {
NotZero(t, Data{}, 123)
})
}

func TestNotIsError(t *testing.T) {
assertFail(t, "SameError", func(t testing.TB) {
NotIsError(t, fmt.Errorf("os error: %w", os.ErrClosed), os.ErrClosed)
Expand Down

0 comments on commit 929325d

Please sign in to comment.