From e94f1ab2eea27b054ee07e3d391b910ebe366635 Mon Sep 17 00:00:00 2001 From: Helene Durand Date: Mon, 6 Jan 2025 14:11:19 +0100 Subject: [PATCH] BUG/MEDIUM: fixes when client runtime is nil Ensure that we always check is c.runtime is valid before any use of c.runtime --- runtime/runtime_client.go | 12 ++++++++++++ runtime/runtime_single_client.go | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/runtime/runtime_client.go b/runtime/runtime_client.go index 4c3bbe40..d870e42a 100644 --- a/runtime/runtime_client.go +++ b/runtime/runtime_client.go @@ -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 } @@ -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 @@ -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) diff --git a/runtime/runtime_single_client.go b/runtime/runtime_single_client.go index 8f4646b7..19d68b69 100644 --- a/runtime/runtime_single_client.go +++ b/runtime/runtime_single_client.go @@ -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.