Skip to content

Commit 51dde8f

Browse files
committed
fix CWE-703
Handle all error cases. for the future, if error is very rare and unlikely to happen on any normal (supported) user case, it's ok to use `panic(err)` in other cases, it's better to propageate error up back to user and logs
1 parent 3c80fc0 commit 51dde8f

File tree

8 files changed

+38
-14
lines changed

8 files changed

+38
-14
lines changed

cmd/performance-profile-creator/cmd/root.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,9 @@ func makeClusterInfoFromClusterData(cluster ClusterData) ClusterInfo {
291291
}
292292

293293
func showClusterInfoJSON(cInfo ClusterInfo) {
294-
json.NewEncoder(os.Stdout).Encode(cInfo)
294+
if err := json.NewEncoder(os.Stdout).Encode(cInfo); err != nil {
295+
panic(fmt.Errorf("Could not create JSON, err: %s", err))
296+
}
295297
}
296298

297299
func showClusterInfoLog(cInfo ClusterInfo) {
@@ -506,7 +508,9 @@ func createProfile(profileData ProfileData) {
506508

507509
// write CSV to out dir
508510
writer := strings.Builder{}
509-
csvtools.MarshallObject(&profile, &writer)
511+
if err := csvtools.MarshallObject(&profile, &writer); err != nil {
512+
panic(fmt.Errorf("Could not marshal profile, err: %s", err))
513+
}
510514

511515
fmt.Printf("%s", writer.String())
512516
}

functests/0_config/config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ var _ = Describe("[performance][config] Performance configuration", func() {
116116

117117
func externalPerformanceProfile(performanceManifest string) (*performancev2.PerformanceProfile, error) {
118118
performanceScheme := runtime.NewScheme()
119-
performancev2.AddToScheme(performanceScheme)
119+
if err := performancev2.AddToScheme(performanceScheme); err != nil {
120+
return nil, fmt.Errorf("Failed to add to scheme, err: %s", err)
121+
}
120122

121123
decode := serializer.NewCodecFactory(performanceScheme).UniversalDeserializer().Decode
122124
manifest, err := ioutil.ReadFile(filepath.Clean(performanceManifest))

functests/3_performance_status/status.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ var _ = Describe("Status testing of performance profile", func() {
4949

5050
AfterEach(func() {
5151
if clean != nil {
52-
clean()
52+
err := clean()
53+
Expect(err).ToNot(HaveOccurred(), "Failed to clean, err: %s", err)
5354
}
54-
5555
})
5656

5757
Context("[rfe_id:28881][performance] Performance Addons detailed status", func() {
@@ -135,7 +135,7 @@ var _ = Describe("Status testing of performance profile", func() {
135135
err := testclient.Client.Get(context.TODO(), key, runtimeClass)
136136
// if err != nil probably the resource were already deleted
137137
if err == nil {
138-
testclient.Client.Delete(context.TODO(), runtimeClass)
138+
err = testclient.Client.Delete(context.TODO(), runtimeClass)
139139
}
140140
return err
141141
}

functests/5_latency_testing/latency_testing.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ func setEnvAndGetDescription(tst latencyTest) string {
157157
}
158158

159159
func setEnvWriteDescription(envVar string, val string, sb *bytes.Buffer, flag *bool) {
160-
os.Setenv(envVar, val)
160+
if err := os.Setenv(envVar, val); err != nil {
161+
panic(err)
162+
}
161163
fmt.Fprintf(sb, "%s = %s \n", envVar, val)
162164
*flag = true
163165
}

pkg/cmd/render/render.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ func (r *renderOpts) AddFlags(fs *pflag.FlagSet) {
9595

9696
func (r *renderOpts) readFlagsFromEnv() {
9797
if ppInFiles := os.Getenv("PERFORMANCE_PROFILE_INPUT_FILES"); len(ppInFiles) > 0 {
98-
r.performanceProfileInputFiles.Set(ppInFiles)
98+
if err := r.performanceProfileInputFiles.Set(ppInFiles); err != nil {
99+
panic(err)
100+
}
99101
}
100102

101103
if assetInDir := os.Getenv("ASSET_INPUT_DIR"); len(assetInDir) > 0 {

pkg/utils/csvtools/csvtools.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ func MarshallObject(obj interface{}, writer io.Writer) error {
8181
unstructured.RemoveNestedField(deployment, "spec", "template", "metadata", "creationTimestamp")
8282
unstructured.RemoveNestedField(deployment, "status")
8383
}
84-
unstructured.SetNestedSlice(r.Object, deployments, "spec", "install", "spec", "deployments")
84+
err = unstructured.SetNestedSlice(r.Object, deployments, "spec", "install", "spec", "deployments")
85+
if err != nil {
86+
return err
87+
}
8588
}
8689

8790
jsonBytes, err = json.Marshal(r.Object)

tools/csv-processor/csv-processor.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,12 @@ Performance Addon Operator provides the ability to enable advanced node performa
245245

246246
// write CSV to out dir
247247
writer := strings.Builder{}
248-
csvtools.MarshallObject(operatorCSV, &writer)
248+
err := csvtools.MarshallObject(operatorCSV, &writer)
249+
if err != nil {
250+
panic(err)
251+
}
249252
outputFilename := filepath.Join(*outputDir, finalizedCsvFilename())
250-
err := ioutil.WriteFile(outputFilename, []byte(writer.String()), 0600)
253+
err = ioutil.WriteFile(outputFilename, []byte(writer.String()), 0600)
251254
if err != nil {
252255
panic(err)
253256
}
@@ -318,10 +321,16 @@ Performance Addon Operator provides the ability to enable advanced node performa
318321
}
319322

320323
// start with a fresh output directory if it already exists
321-
os.RemoveAll(*outputDir)
324+
err = os.RemoveAll(*outputDir)
325+
if err != nil {
326+
panic(err)
327+
}
322328

323329
// create output directory
324-
os.MkdirAll(*outputDir, os.FileMode(0775))
330+
err = os.MkdirAll(*outputDir, os.FileMode(0775))
331+
if err != nil {
332+
panic(err)
333+
}
325334

326335
generateUnifiedCSV(userData)
327336
}

tools/csv-replace-imageref/csv-replace-imageref.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ func processCSV(operatorImage, csvInput string, dst io.Writer) {
2929

3030
operatorCSV.Annotations["containerImage"] = operatorImage
3131

32-
csvtools.MarshallObject(operatorCSV, dst)
32+
if err := csvtools.MarshallObject(operatorCSV, dst); err != nil {
33+
panic(fmt.Errorf("could not marshall CSV, err: %s", err))
34+
}
3335
}
3436

3537
func main() {

0 commit comments

Comments
 (0)