Skip to content

Commit

Permalink
BUG/MEDIUM: fixes when client runtime is nil
Browse files Browse the repository at this point in the history
Ensure that we always check is c.runtime is valid before any use of c.runtime
  • Loading branch information
hdurand0710 authored and mjuraga committed Jan 7, 2025
1 parent 43244a3 commit e94f1ab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions runtime/runtime_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,18 @@ func (c *client) initWithMasterSocket(opt options.RuntimeOptions) error {

// GetStats returns stats from the socket
func (c *client) GetStats() models.NativeStats {
if !c.runtime.IsValid() {
return models.NativeStats{}
}
result := c.runtime.GetStats()
return result
}

// GetInfo returns info from the socket
func (c *client) GetInfo() (models.ProcessInfo, error) {
if !c.runtime.IsValid() {
return models.ProcessInfo{}, errors.New("no valid runtime found")
}
result := c.runtime.GetInfo()
return result, nil
}
Expand All @@ -104,6 +110,9 @@ func (c *client) GetVersion() (HAProxyVersion, error) {
_, err, _ = versionSfg.Do(versionKey, func() (interface{}, error) {
version := &HAProxyVersion{}
var response string
if !c.runtime.IsValid() {
return HAProxyVersion{}, errors.New("no valid runtime found")
}
response, err = c.runtime.ExecuteRaw("show info")
if err != nil {
return HAProxyVersion{}, err
Expand Down Expand Up @@ -155,6 +164,9 @@ func (c *client) Reload() (string, error) {
return "", fmt.Errorf("cannot reload: requires HAProxy 2.7 or later but current version is %v", haproxyVersion)
}

if !c.runtime.IsValid() {
return "", errors.New("cannot reload: no valid runtime found")
}
output, err := c.runtime.ExecuteMaster("reload")
if err != nil {
return "", fmt.Errorf("cannot reload: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion runtime/runtime_single_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type SingleRuntime struct {
}

func (s *SingleRuntime) IsValid() bool {
return s.socketPath != ""
return s != nil && s.socketPath != ""
}

// Init must be given path to runtime socket and a flag to indicate if it's in master-worker mode.
Expand Down

0 comments on commit e94f1ab

Please sign in to comment.