Do not swallow non-error panics in Encode/Decode - #438
Merged
Merged
Conversation
guelfey
approved these changes
Sep 24, 2026
guelfey
left a comment
Member
There was a problem hiding this comment.
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
force-pushed
the
recover-non-error
branch
2 times, most recently
from
September 24, 2026 12:11
ae28e3a to
d8208ef
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The encoder and decoder report errors by panicking with an error value, which is recovered in
(*encoder).Encodeand(*decoder).Decode. The recover code, though, silently discards a panic whose value is not anerror:Encodereturnsnil, andDecodereturns a partially decoded result with anilerror. Such panics do not come from the encoder/decoder itself (e.g. they may come from the underlyingio.Readerorio.Writer), and hiding them is wrong.Re-panic if the recovered value is not an
error, and add tests.