Skip to content

Quality: Unsafe type assertion in panic recovery will re-panic for non-error values - #1295

Merged
alecthomas merged 1 commit into
alecthomas:masterfrom
kumburovicbranko682-boop:contribai/improve/quality/unsafe-type-assertion-in-panic-recovery-
Jun 27, 2026
Merged

Quality: Unsafe type assertion in panic recovery will re-panic for non-error values#1295
alecthomas merged 1 commit into
alecthomas:masterfrom
kumburovicbranko682-boop:contribai/improve/quality/unsafe-type-assertion-in-panic-recovery-

Conversation

@kumburovicbranko682-boop

Copy link
Copy Markdown
Contributor

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

…on-error values

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).


Affected files: formatter.go

Signed-off-by: kumburovicbranko682-boop <295886834+kumburovicbranko682-boop@users.noreply.github.com>
@alecthomas
alecthomas merged commit f0e1def into alecthomas:master Jun 27, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants