fix: WithHTTPClient(nil) panics with a nil pointer dereference on the first request - #45
Merged
Merged
Conversation
WithHTTPClient stored its argument verbatim, so WithHTTPClient(nil) left
Client.httpClient nil and the first request dereferenced it:
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x28]
A nil arrives from ordinary code — a client read out of a config struct, or
returned alongside an error by a helper the caller did not check. That is a
misconfiguration, not a reason to take the process down, and the SDK already
returns *ConfigurationError rather than panicking for the equivalent bad
retry count.
A nil client is now ignored and the SDK keeps its default, exactly as if the
option had not been supplied. The default request timeout survives, and an
explicitly configured WithTimeout survives in either option order.
Closes #41
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ao5paex73xXvuM424Libo
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Merged
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.
Closes #41
The defect
WithHTTPClientstored its argument verbatim.WithHTTPClient(nil)therefore leftClient.httpClientnil andhttpClient.Do(req)dereferenced it on the first request.A nil arrives from ordinary code: a client read out of a config struct, or returned alongside an error by a helper whose error the caller did not check. The SDK already returns a typed
*ConfigurationErrorrather than panicking for the equivalent bad input (a negativeWithRetries), so panicking here was inconsistent as well as hostile.Verified still reproducing on
main(cd51e3a) before any change was written.The fix
A nil
*http.Clientis ignored and the SDK keeps its default, exactly as if the option had not been supplied. One guard inWithHTTPClient, plus the doc comment stating the contract.Explicit configuration is preserved either way round:
WithHTTPClient(nil), WithTimeout(7s)andWithTimeout(7s), WithHTTPClient(nil)both leave a 7s timeout; a bareWithHTTPClient(nil)leavesDefaultTimeout.Red
Tests written first, run against unmodified
origin/mainclient.go(git checkout origin/main -- client.go):Green
Full suite
go test ./...ok github.com/OilpriceAPI/oilpriceapi-go 16.490s— 290 pass / 0 fail (285 baseline + 5 new)go test -race ./...ok github.com/OilpriceAPI/oilpriceapi-go 18.355sgo vet ./...gofmt -l .Interaction with #38
The #38 PR (
fix/timeout-clone) also stopsClient.httpClientfrom ever being the caller's own pointer. The two branches touch different functions and do not conflict in either merge order; this guard remains the explicit statement of the nil contract regardless of which lands first.Scope
No version bump, no tag, no release. Public API unchanged — this only removes a panic.
🤖 Generated with Claude Code
https://claude.ai/code/session_015ao5paex73xXvuM424Libo