Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pkg/build/controller/build/build_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package build
import (
"context"
"fmt"
"io/ioutil"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -1761,7 +1760,7 @@ func TestHandleControllerConfig(t *testing.T) {
}

// Ensure that the generated configuration is actually valid.
registriesConf, err := ioutil.TempFile("", "registries.conf")
registriesConf, err := os.CreateTemp("", "registries.conf")
defer os.Remove(registriesConf.Name())
if err != nil {
t.Errorf("unexpected error creating temp file: %s", err.Error())
Comment on lines +1763 to 1766
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Guard CreateTemp error before using registriesConf.

At Line 1764, registriesConf.Name() is called before checking err. If os.CreateTemp fails, this can panic on nil and mask the real failure.

💡 Suggested fix
 registriesConf, err := os.CreateTemp("", "registries.conf")
-defer os.Remove(registriesConf.Name())
 if err != nil {
 	t.Errorf("unexpected error creating temp file: %s", err.Error())
+	return
 }
+defer os.Remove(registriesConf.Name())
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
registriesConf, err := os.CreateTemp("", "registries.conf")
defer os.Remove(registriesConf.Name())
if err != nil {
t.Errorf("unexpected error creating temp file: %s", err.Error())
registriesConf, err := os.CreateTemp("", "registries.conf")
if err != nil {
t.Errorf("unexpected error creating temp file: %s", err.Error())
return
}
defer os.Remove(registriesConf.Name())
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/build/controller/build/build_controller_test.go` around lines 1763 -
1766, The test calls os.CreateTemp and immediately defers
os.Remove(registriesConf.Name()) before checking err, which can panic if
CreateTemp failed; update the code in build_controller_test.go to check the
error returned by os.CreateTemp first (i.e., if err != nil { t.Errorf(...);
return } or t.Fatalf), only then use registriesConf and establish the defer
os.Remove(registriesConf.Name()), ensuring registriesConf is non-nil when
referenced.

Expand Down
5 changes: 2 additions & 3 deletions pkg/image/controller/imagestream_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"reflect"
"strings"
Expand Down Expand Up @@ -478,7 +477,7 @@ func objBody(object interface{}) io.ReadCloser {
if err != nil {
panic(err)
}
return ioutil.NopCloser(bytes.NewReader([]byte(output)))
return io.NopCloser(bytes.NewReader([]byte(output)))
}

func header() http.Header {
Expand All @@ -488,7 +487,7 @@ func header() http.Header {
}

func decode(reader io.ReadCloser, decoder runtime.Decoder) (runtime.Object, error) {
data, err := ioutil.ReadAll(reader)
data, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/template/controller/templateinstance_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package controller
import (
"bytes"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"testing"
"time"
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestControllerCheckReadiness(t *testing.T) {
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewBuffer(b)),
Body: io.NopCloser(bytes.NewBuffer(b)),
}, nil
})
},
Expand Down