Skip to content

Commit 311fcae

Browse files
committed
fix(utils): add nil and pointer validation to ReadYAML function across multiple packages
1 parent 3f95e90 commit 311fcae

6 files changed

Lines changed: 46 additions & 13 deletions

File tree

agent/utils/files.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"os"
1010
"path/filepath"
11+
"reflect"
1112

1213
"gopkg.in/yaml.v2"
1314
)
@@ -22,6 +23,15 @@ func GetMyPath() string {
2223
}
2324

2425
func ReadYAML(path string, result interface{}) error {
26+
if result == nil {
27+
return fmt.Errorf("result interface is nil")
28+
}
29+
30+
rv := reflect.ValueOf(result)
31+
if rv.Kind() != reflect.Ptr || rv.IsNil() {
32+
return fmt.Errorf("result must be a non-nil pointer")
33+
}
34+
2535
file, err := os.Open(path)
2636
if err != nil {
2737
return err

installer/utils/os.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"os/exec"
99
"path/filepath"
10+
"reflect"
1011

1112
"gopkg.in/yaml.v3"
1213
)
@@ -51,12 +52,12 @@ func RunCmd(command string, arg ...string) error {
5152

5253
func RunCmdWithOutput(command string, arg ...string) ([]string, error) {
5354
cmd := exec.Command(command, arg...)
54-
55+
5556
output, err := cmd.Output()
5657
if err != nil {
5758
return nil, fmt.Errorf("error running command: %v", err)
5859
}
59-
60+
6061
lines := bytes.Split(output, []byte("\n"))
6162
result := make([]string, 0, len(lines))
6263
for _, line := range lines {
@@ -65,7 +66,7 @@ func RunCmdWithOutput(command string, arg ...string) ([]string, error) {
6566
result = append(result, string(trimmed))
6667
}
6768
}
68-
69+
6970
return result, nil
7071
}
7172

@@ -127,6 +128,15 @@ func WriteYAML(url string, data any) error {
127128
}
128129

129130
func ReadYAML(path string, result any) error {
131+
if result == nil {
132+
return fmt.Errorf("result interface is nil")
133+
}
134+
135+
rv := reflect.ValueOf(result)
136+
if rv.Kind() != reflect.Ptr || rv.IsNil() {
137+
return fmt.Errorf("result must be a non-nil pointer")
138+
}
139+
130140
file, err := os.Open(path)
131141
if err != nil {
132142
return err

plugins/config/main.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -803,11 +803,6 @@ func writeRules(rules []Rule) error {
803803
return catcher.Error("failed to write to file", err, map[string]any{"process": "plugin_com.utmstack.config"})
804804
}
805805

806-
err = file.Close()
807-
if err != nil {
808-
return catcher.Error("failed to close file", err, map[string]any{"process": "plugin_com.utmstack.config"})
809-
}
810-
811806
return nil
812807
}()
813808

@@ -851,10 +846,5 @@ func writePatterns(patterns map[string]string) error {
851846
return catcher.Error("failed to write to file", err, map[string]any{"process": "plugin_com.utmstack.config"})
852847
}
853848

854-
err = file.Close()
855-
if err != nil {
856-
return catcher.Error("failed to close file", err, map[string]any{"process": "plugin_com.utmstack.config"})
857-
}
858-
859849
return nil
860850
}

plugins/feeds/internal/client/dependencies.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func NewClientDependencies(cfg *config.TWConfig) (*ClientDependencies, error) {
2222

2323
deps := &ClientDependencies{
2424
Backend: NewBackendClient(cfg),
25+
CM: &CustomersManagerClient{},
2526
ThreadWinds: NewThreadWindsClient(cfg),
2627
OpenSearch: opensearch,
2728
}

plugins/feeds/utils/files.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@ package utils
22

33
import (
44
"os"
5+
"reflect"
56

7+
"github.com/threatwinds/go-sdk/catcher"
68
"gopkg.in/yaml.v2"
79
)
810

911
func ReadYAML(path string, result interface{}) error {
12+
if result == nil {
13+
return catcher.Error("result interface is nil", nil, nil)
14+
}
15+
16+
rv := reflect.ValueOf(result)
17+
if rv.Kind() != reflect.Ptr || rv.IsNil() {
18+
return catcher.Error("result must be a non-nil pointer", nil, nil)
19+
}
20+
1021
file, err := os.Open(path)
1122
if err != nil {
1223
return err

utmstack-collector/utils/files.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package utils
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"os"
67
"path/filepath"
8+
"reflect"
79

810
"gopkg.in/yaml.v2"
911
)
@@ -18,6 +20,15 @@ func GetMyPath() string {
1820
}
1921

2022
func ReadYAML(path string, result interface{}) error {
23+
if result == nil {
24+
return fmt.Errorf("result interface is nil")
25+
}
26+
27+
rv := reflect.ValueOf(result)
28+
if rv.Kind() != reflect.Ptr || rv.IsNil() {
29+
return fmt.Errorf("result must be a non-nil pointer")
30+
}
31+
2132
file, err := os.Open(path)
2233
if err != nil {
2334
return err

0 commit comments

Comments
 (0)