Skip to content

NewClient panics when given nil Options - add nil parameter check #3358

Closed
@frankxjkuang

Description

@frankxjkuang

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

  1. Create a nil *Options instance: var opt *redis.Options
  2. Call redis.NewClient(opt)
  3. 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

  1. 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
}
  1. Update documentation to clarify that opt can be nil (with defaults) or explicitly initialized.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions