Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix new gosec reported issues #4259

Merged
merged 3 commits into from
Oct 2, 2024
Merged
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
21 changes: 15 additions & 6 deletions cli/azd/pkg/input/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -925,13 +925,22 @@ func (c *AskerConsole) Handles() ConsoleHandles {
}

// consoleWidth the number of columns in the active console window
func consoleWidth() int {
width, _ := consolesize.GetConsoleSize()
return width
func consoleWidth() int32 {
widthInt, _ := consolesize.GetConsoleSize()

// Suppress G115: integer overflow conversion int -> int32 below.
// Explanation:
// consolesize.GetConsoleSize() returns an int, but the underlying implementation actually is a uint16 on both
// Windows and unix systems.
//
// In practice, console width is the number of columns (text) in the active console window.
// We don't ever expect this to be larger than math.MaxInt32, so we can safely cast to int32.
// nolint:gosec // G115
return int32(widthInt)
}

func (c *AskerConsole) handleResize(width int) {
c.consoleWidth.Store(int32(width))
func (c *AskerConsole) handleResize(width int32) {
c.consoleWidth.Store(width)

c.spinnerLineMu.Lock()
if c.spinner.Status() == yacspin.SpinnerRunning {
Expand Down Expand Up @@ -1045,7 +1054,7 @@ func NewConsole(
c.spinner, _ = yacspin.New(spinnerConfig)
c.spinnerTerminalMode = spinnerConfig.TerminalMode
if isTerminal {
c.consoleWidth = atomic.NewInt32(int32(consoleWidth()))
c.consoleWidth = atomic.NewInt32(consoleWidth())
watchTerminalResize(c)
watchTerminalInterrupt(c)
}
Expand Down
5 changes: 2 additions & 3 deletions cli/azd/pkg/ioc/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"reflect"
"regexp"
"unsafe"

"github.com/golobby/container/v3"
)
Expand Down Expand Up @@ -90,11 +89,11 @@ func NewRegistrationsOnly(from *NestedContainer) *NestedContainer {
}

func getUnexportedField(field reflect.Value) interface{} {
return reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem().Interface()
return reflect.NewAt(field.Type(), field.Addr().UnsafePointer()).Elem().Interface()
}

func setUnexportedField(field reflect.Value, value interface{}) {
reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).
reflect.NewAt(field.Type(), field.Addr().UnsafePointer()).
Elem().
Set(reflect.ValueOf(value))
}
Expand Down
3 changes: 2 additions & 1 deletion cli/azd/pkg/output/ux/list_as_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ import (
func ListAsText(items []string) string {
count := len(items)
if count < 1 {
log.Panic("calling itemsCountAsText() with empty list.")
log.Panic("calling ListAsText() with empty list.")
}

if count == 1 {
return items[0]
}

if count == 2 {
//nolint:gosec // G602: slice index out of range - false positive, we know the slice has at least 2 elements
weikanglim marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Sprintf("%s and %s", items[0], items[1])
}

Expand Down
2 changes: 1 addition & 1 deletion cli/azd/pkg/tools/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ func extractFromTar(src, dst string) (string, error) {
// cspell: disable-next-line `Typeflag` is comming fron *tar.Header
if fileHeader.Typeflag == tar.TypeReg && fileName == "gh" {
filePath := filepath.Join(dst, fileName)
ghCliFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(fileHeader.Mode))
ghCliFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fileHeader.FileInfo().Mode())
weikanglim marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return extractedAt, err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/pkg/tools/pack/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func extractFromTar(
// cspell: disable-next-line `Typeflag` is comming fron *tar.Header
if fileHeader.Typeflag == tar.TypeReg && fileName == "pack" {
filePath := filepath.Join(out, fileName)
packCliFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(fileHeader.Mode))
packCliFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fileHeader.FileInfo().Mode())
if err != nil {
return extractedAt, err
}
Expand Down
Loading