-
Couldn't load subscription status.
- Fork 2.5k
Description
Expected Behavior
_ = redis.NewClient(nil)
When opt parameter is nil, NewClient should handle it gracefully (e.g., use default configuration or return an error) instead of causing a panic.
Current Behavior
If opt is nil, calling opt.init() triggers a nil pointer dereference panic, leading to program crash.
Possible Solution
Add a nil check for opt parameter and initialize it with default values when it's nil:
func NewClient(opt *Options) *Client {
if opt == nil {
opt = &Options{} // Use default configuration or actively panic
}
// ...
}Steps to Reproduce
- Create a
nil*Optionsinstance:var opt *redis.Options - Call
redis.NewClient(opt) - The program panics with error:
panic: runtime error: invalid memory address or nil pointer dereference
Context (Environment)
Affects developers using the Go-Redis client library. This issue can lead to unexpected application crashes when NewClient is called with an uninitialized Options pointer.
Detailed Description
The NewClient method does not validate the opt parameter for nil before calling opt.init(). When opt is nil, this results in a critical runtime panic. This violates defensive programming principles and exposes the library to misuse by clients that might pass invalid configurations.
Possible Implementation
- Add a nil check at the beginning of
NewClient:
func NewClient(opt *Options) *Client {
if opt == nil {
opt = &Options{} // Initialize with default values
}
opt.init()
// ... rest of the implementation
}
- Update documentation to clarify that
optcan benil(with defaults) or explicitly initialized.