Skip to content

Commit

Permalink
fix CWE-703
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yuvalk committed Oct 4, 2021
1 parent 98ac7af commit 01f8f9a
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 14 deletions.
8 changes: 6 additions & 2 deletions cmd/performance-profile-creator/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ func makeClusterInfoFromClusterData(cluster ClusterData) ClusterInfo {
}

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

func showClusterInfoLog(cInfo ClusterInfo) {
Expand Down Expand Up @@ -506,7 +508,9 @@ func createProfile(profileData ProfileData) {

// write CSV to out dir
writer := strings.Builder{}
csvtools.MarshallObject(&profile, &writer)
if err := csvtools.MarshallObject(&profile, &writer); err != nil {
panic(fmt.Errorf("Could not marshal profile, err: %s", err))
}

fmt.Printf("%s", writer.String())
}
4 changes: 3 additions & 1 deletion functests/0_config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ var _ = Describe("[performance][config] Performance configuration", func() {

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

decode := serializer.NewCodecFactory(performanceScheme).UniversalDeserializer().Decode
manifest, err := ioutil.ReadFile(filepath.Clean(performanceManifest))
Expand Down
6 changes: 3 additions & 3 deletions functests/3_performance_status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ var _ = Describe("Status testing of performance profile", func() {

AfterEach(func() {
if clean != nil {
clean()
err := clean()
Expect(err).ToNot(HaveOccurred(), "Failed to clean, err: %s", err)
}

})

Context("[rfe_id:28881][performance] Performance Addons detailed status", func() {
Expand Down Expand Up @@ -135,7 +135,7 @@ var _ = Describe("Status testing of performance profile", func() {
err := testclient.Client.Get(context.TODO(), key, runtimeClass)
// if err != nil probably the resource were already deleted
if err == nil {
testclient.Client.Delete(context.TODO(), runtimeClass)
err = testclient.Client.Delete(context.TODO(), runtimeClass)
}
return err
}
Expand Down
4 changes: 3 additions & 1 deletion functests/5_latency_testing/latency_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ func setEnvAndGetDescription(tst latencyTest) string {
}

func setEnvWriteDescription(envVar string, val string, sb *bytes.Buffer, flag *bool) {
os.Setenv(envVar, val)
if err := os.Setenv(envVar, val); err != nil {
panic(err)
}
fmt.Fprintf(sb, "%s = %s \n", envVar, val)
*flag = true
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ func (r *renderOpts) AddFlags(fs *pflag.FlagSet) {

func (r *renderOpts) readFlagsFromEnv() {
if ppInFiles := os.Getenv("PERFORMANCE_PROFILE_INPUT_FILES"); len(ppInFiles) > 0 {
r.performanceProfileInputFiles.Set(ppInFiles)
if err := r.performanceProfileInputFiles.Set(ppInFiles); err != nil {
panic(err)
}
}

if assetInDir := os.Getenv("ASSET_INPUT_DIR"); len(assetInDir) > 0 {
Expand Down
5 changes: 4 additions & 1 deletion pkg/utils/csvtools/csvtools.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ func MarshallObject(obj interface{}, writer io.Writer) error {
unstructured.RemoveNestedField(deployment, "spec", "template", "metadata", "creationTimestamp")
unstructured.RemoveNestedField(deployment, "status")
}
unstructured.SetNestedSlice(r.Object, deployments, "spec", "install", "spec", "deployments")
err = unstructured.SetNestedSlice(r.Object, deployments, "spec", "install", "spec", "deployments")
if err != nil {
return err
}
}

jsonBytes, err = json.Marshal(r.Object)
Expand Down
17 changes: 13 additions & 4 deletions tools/csv-processor/csv-processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,12 @@ Performance Addon Operator provides the ability to enable advanced node performa

// write CSV to out dir
writer := strings.Builder{}
csvtools.MarshallObject(operatorCSV, &writer)
err := csvtools.MarshallObject(operatorCSV, &writer)
if err != nil {
panic(err)
}
outputFilename := filepath.Join(*outputDir, finalizedCsvFilename())
err := ioutil.WriteFile(outputFilename, []byte(writer.String()), 0600)
err = ioutil.WriteFile(outputFilename, []byte(writer.String()), 0600)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -318,10 +321,16 @@ Performance Addon Operator provides the ability to enable advanced node performa
}

// start with a fresh output directory if it already exists
os.RemoveAll(*outputDir)
err = os.RemoveAll(*outputDir)
if err != nil {
panic(err)
}

// create output directory
os.MkdirAll(*outputDir, os.FileMode(0775))
err = os.MkdirAll(*outputDir, os.FileMode(0775))
if err != nil {
panic(err)
}

generateUnifiedCSV(userData)
}
4 changes: 3 additions & 1 deletion tools/csv-replace-imageref/csv-replace-imageref.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ func processCSV(operatorImage, csvInput string, dst io.Writer) {

operatorCSV.Annotations["containerImage"] = operatorImage

csvtools.MarshallObject(operatorCSV, dst)
if err := csvtools.MarshallObject(operatorCSV, dst); err != nil {
panic(fmt.Errorf("could not marshall CSV, err: %s", err))
}
}

func main() {
Expand Down

0 comments on commit 01f8f9a

Please sign in to comment.