Skip to content

Commit f0e1def

Browse files
Quality: Unsafe type assertion in panic recovery will re-panic for non-error values (#1295)
## Problem Both `FormatterFunc.Format` and `recoveringFormatter.Format` use `perr.(error)` to convert the recovered panic value to an error. This is a bare type assertion (no comma-ok), so if the panic value is not an `error` (e.g., `panic("something")`, `panic(42)` — both common idioms in Go and in third-party code), the type assertion itself panics with "interface conversion: interface is string, not error". This completely defeats the purpose of the panic-recovery wrapper, crashing the caller instead of converting the panic to an error. This affects both recover sites (FormatterFunc at ~line 18 and recoveringFormatter at ~line 30). **Severity**: `high` **File**: `formatter.go` ## Solution Replace the bare type assertion with a type switch or comma-ok pattern at both sites: In FormatterFunc.Format (line ~18): ## Changes - `formatter.go` (modified) ## Testing - [ ] Existing tests pass - [ ] Manual review completed - [ ] No new warnings/errors introduced Signed-off-by: kumburovicbranko682-boop <295886834+kumburovicbranko682-boop@users.noreply.github.com>
1 parent 4224e71 commit f0e1def

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

formatter.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package chroma
22

33
import (
4+
"fmt"
45
"io"
56
"iter"
67
)
@@ -21,7 +22,11 @@ type FormatterFunc func(w io.Writer, style *Style, iterator iter.Seq[Token]) err
2122
func (f FormatterFunc) Format(w io.Writer, s *Style, it iter.Seq[Token]) (err error) {
2223
defer func() {
2324
if perr := recover(); perr != nil {
24-
err = perr.(error)
25+
if e, ok := perr.(error); ok {
26+
err = e
27+
} else {
28+
err = fmt.Errorf("%v", perr)
29+
}
2530
}
2631
}()
2732
return f(w, s, it)
@@ -34,7 +39,11 @@ type recoveringFormatter struct {
3439
func (r recoveringFormatter) Format(w io.Writer, s *Style, it iter.Seq[Token]) (err error) {
3540
defer func() {
3641
if perr := recover(); perr != nil {
37-
err = perr.(error)
42+
if e, ok := perr.(error); ok {
43+
err = e
44+
} else {
45+
err = fmt.Errorf("%v", perr)
46+
}
3847
}
3948
}()
4049
return r.Formatter.Format(w, s, it)

0 commit comments

Comments
 (0)