Skip to content

Commit

Permalink
fix: address linter errors (#286)
Browse files Browse the repository at this point in the history
* chore: disable deprecated linter

* fix: remove unnecessary copy of loop variable

* fix: audit and address potential integer overflows
  • Loading branch information
coffeebeats authored Nov 3, 2024
1 parent 16a2e48 commit 4efc4a6
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ linters:
disable:
# Deprecated.
- execinquery
- exportloopref
- gomnd

# Handle formatting separately.
Expand Down
2 changes: 1 addition & 1 deletion cmd/gdenv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func setUpLogger() error {
// ANSI escape color.
//
// NOTE: This function assumes that the width of the level strings is '5'.
func newStyleWithColor(name string, ansiColor int) lipgloss.Style {
func newStyleWithColor(name string, ansiColor uint) lipgloss.Style {
if name == "" {
panic("missing style name")
}
Expand Down
10 changes: 6 additions & 4 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,11 @@ func (c *Client) Exists(ctx context.Context, urlBaseRaw string, urlPartsRaw ...s
// provided context.
func (c *Client) Download(ctx context.Context, u *url.URL, w ...io.Writer) error {
return c.get(ctx, u, func(r *resty.Response) error {
if r.RawResponse.ContentLength > 0 { // No progress to report if '0'.
// No progress to report if '0'.
if contentLength := r.RawResponse.ContentLength; contentLength > 0 {
// Report progress if set on the context.
if p, ok := ctx.Value(progressKey{}).(*progress.Progress); ok && p != nil {
if err := p.SetTotal(uint64(r.RawResponse.ContentLength)); err != nil {
if err := p.SetTotal(uint64(contentLength)); err != nil { //nolint:gosec
return err
}

Expand Down Expand Up @@ -239,10 +240,11 @@ func (c *Client) DownloadTo(ctx context.Context, u *url.URL, out string) error {
return c.get(ctx, u, func(r *resty.Response) error {
var w io.Writer = f

if r.RawResponse.ContentLength > 0 { // No progress to report if '0'.
// No progress to report if '0'.
if contentLength := r.RawResponse.ContentLength; contentLength > 0 {
// Report progress if set on the context.
if p, ok := ctx.Value(progressKey{}).(*progress.Progress); ok && p != nil {
if err := p.SetTotal(uint64(r.RawResponse.ContentLength)); err != nil {
if err := p.SetTotal(uint64(contentLength)); err != nil { //nolint:gosec
return err
}

Expand Down
10 changes: 9 additions & 1 deletion internal/osutil/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package osutil
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"os"
Expand All @@ -13,6 +14,8 @@ import (
// Only write to 'out'; create a new file/overwrite an existing.
const copyFileWriteFlag = os.O_WRONLY | os.O_CREATE | os.O_TRUNC

var ErrValueOutOfRange = errors.New("value out of range")

/* -------------------------------------------------------------------------- */
/* Function: CopyFile */
/* -------------------------------------------------------------------------- */
Expand Down Expand Up @@ -101,5 +104,10 @@ func SizeOf(path string) (uint64, error) {
return 0, err
}

return uint64(info.Size()), nil
size := info.Size()
if size < 0 {
return 0, fmt.Errorf("%w: size: expected value >= 0: %d", ErrValueOutOfRange, size)
}

return uint64(size), nil
}
2 changes: 0 additions & 2 deletions pkg/godot/mirror/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ func Select[T artifact.Artifact](
selected := make(chan Mirror[T])

for _, m := range mirrors {
m := m // Prevent capture of loop variable.

eg.Go(func() error {
ok, err := checkIfExists(ctx, m, a)
if err != nil {
Expand Down

0 comments on commit 4efc4a6

Please sign in to comment.