fix(fetcher): nil-safe charset lookup#1276
Open
SAY-5 wants to merge 2 commits into
Open
Conversation
decodeReaderWithCharset called ianaindex.IANA.Encoding(charset) and on
failure fell back to ianaindex.IANA.Encoding("utf-8") while discarding
the second error. The library can return (nil, nil) for charset names
it recognizes but has no implementation for, so encoding could be nil
when we chained .NewDecoder(), panicking.
Extract a lookupCharsetEncoding helper that tries the requested charset,
falls back to utf-8 via ianaindex, and ultimately falls back to
encoding/unicode.UTF8 so it always returns a non-nil encoding.
decodeHeader's CharsetReader had the same nil-after-success edge case;
guard it explicitly with a clear error message.
Regression tests cover an unknown charset and the helper's contract.
Signed-off-by: say <say.apm35@gmail.com>
floatpanebot
previously requested changes
May 11, 2026
Member
floatpanebot
left a comment
There was a problem hiding this comment.
Hi @SAY-5! Please fix the following issues with your PR:
- Title: Is too long (64 characters). The PR title must be strictly under 40 characters.
- Body: Missing the
## What?or## Why?headings required by the PR template.
floatpanebot
previously requested changes
May 11, 2026
Member
floatpanebot
left a comment
There was a problem hiding this comment.
Hi @SAY-5! Please fix the following issues with your PR:
- Title: Is too long (64 characters). The PR title must be strictly under 40 characters.
Formatting issues have been resolved. Thank you!
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.
Fixes #896.
What?
decodeReaderWithCharsetanddecodeHeader'sCharsetReaderinfetcher/fetcher.gocould nil-deref becauseianaindex.IANA.Encodingis allowed to return(nil, nil)for charset names it recognizes but has no implementation for. The previous code chained.NewDecoder()on that nil encoding.Refactor: introduce
lookupCharsetEncodingthat always returns a non-nilencoding.Encodingby trying the requested charset, thenianaindex's "utf-8", thenencoding/unicode.UTF8as final guarantee. Use it fromdecodeReaderWithCharset. Guard thedecodeHeaderpath explicitly so the nil-after-success case surfaces as a clean error.Why?
Without the guard, a real message with an unusual charset header can panic the fetcher. The fix is minimal, has no behavior change for the common path (a valid charset still resolves the same way), and is covered by two new tests:
TestDecodeReaderWithCharsetSurvivesUnknownCharset(issue regression)TestLookupCharsetEncodingAlwaysReturnsNonNil(table-driven contract)go test ./fetcher/...passes locally;go vet ./fetcher/...clean;gofmtclean.