Skip to content

Do not swallow non-error panics in Encode/Decode - #438

Merged
guelfey merged 1 commit into
godbus:masterfrom
kolyshkin:recover-non-error
Sep 24, 2026
Merged

guelfey merged 1 commit into
godbus:masterfrom
kolyshkin:recover-non-error

Conversation

@kolyshkin

Copy link
Copy Markdown
Contributor

The encoder and decoder report errors by panicking with an error value, which is recovered in (*encoder).Encode and (*decoder).Decode. The recover code, though, silently discards a panic whose value is not an error: Encode returns nil, and Decode returns a partially decoded result with a nil error. Such panics do not come from the encoder/decoder itself (e.g. they may come from the underlying io.Reader or io.Writer), and hiding them is wrong.

Re-panic if the recovered value is not an error, and add tests.

@guelfey guelfey left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely better than before, but thinking about it: the decoder and encoder code explicitly panics at multiple points (using panics more like exceptions for control flow); shouldn't we even use a special error type that gets "detected" within the recover code? Otherwise also other panics that are not from "our" (dbus) code that panic with an err would get swallowed as well. (Or we could just refactor all this to not use panic, but normal error returns...)

The encoder and decoder report errors by panicking with an error value,
which is recovered in (*encoder).Encode and (*decoder).Decode. The
recover code, though, silently discards a panic whose value is not an
error: Encode returns nil, and Decode returns a partially decoded result
with a nil error. Such panics do not come from the encoder/decoder
itself (e.g. they may come from the underlying io.Reader or io.Writer),
and hiding them is wrong.

Re-panic if the recovered value is not an error, and add tests.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
@guelfey
guelfey force-pushed the recover-non-error branch 2 times, most recently from ae28e3a to d8208ef Compare September 24, 2026 12:11
@guelfey
guelfey merged commit d87fd7c into godbus:master Sep 24, 2026
13 of 14 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