Skip to content

Commit eade0ee

Browse files
Userclaude
andcommitted
fix: resolve linting issues for Go 1.24.1 update
- Fix go:build directive spacing in pty_linux.go - Add bounds checks and #nosec annotations for integer conversions - Fix comment alignment and formatting - Address gosec G115 warnings in multiple files Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 02fd64a commit eade0ee

File tree

8 files changed

+27
-19
lines changed

8 files changed

+27
-19
lines changed

cli/clistat/disk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func (*Statter) Disk(p Prefix, path string) (*Result, error) {
1919
return nil, err
2020
}
2121
var r Result
22-
r.Total = ptr.To(float64(stat.Blocks * uint64(stat.Bsize)))
22+
r.Total = ptr.To(float64(stat.Blocks * uint64(stat.Bsize))) // #nosec G115 -- int64 to uint64 is safe for filesystem stats (always positive)
2323
r.Used = float64(stat.Blocks-stat.Bfree) * float64(stat.Bsize)
2424
r.Unit = "B"
2525
r.Prefix = p

cli/cliutil/levenshtein/levenshtein.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ func Distance(a, b string, maxDist int) (int, error) {
3232
if len(b) > 255 {
3333
return 0, xerrors.Errorf("levenshtein: b must be less than 255 characters long")
3434
}
35-
m := uint8(len(a))
36-
n := uint8(len(b))
35+
// We've already checked that len(a) and len(b) are <= 255, so conversion is safe
36+
m := uint8(len(a)) // #nosec G115 -- length is checked to be <= 255
37+
n := uint8(len(b)) // #nosec G115 -- length is checked to be <= 255
3738

3839
// Special cases for empty strings
3940
if m == 0 {
@@ -76,7 +77,7 @@ func Distance(a, b string, maxDist int) (int, error) {
7677
d[i][j]+subCost, // substitution
7778
)
7879
// check maxDist on the diagonal
79-
if maxDist > -1 && i == j && d[i+1][j+1] > uint8(maxDist) {
80+
if maxDist > -1 && i == j && maxDist <= 255 && d[i+1][j+1] > uint8(maxDist) { // #nosec G115 -- we check maxDist <= 255
8081
return int(d[i+1][j+1]), ErrMaxDist
8182
}
8283
}

coderd/tracing/slog.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ func slogFieldsToAttributes(m slog.Map) []attribute.KeyValue {
7878
case []int64:
7979
value = attribute.Int64SliceValue(v)
8080
case uint:
81-
value = attribute.Int64Value(int64(v))
81+
// If v is larger than math.MaxInt64, this will overflow, but this is acceptable for our tracing use case
82+
value = attribute.Int64Value(int64(v)) // #nosec G115 -- acceptable overflow for tracing context
8283
// no uint slice method
8384
case uint8:
8485
value = attribute.Int64Value(int64(v))
@@ -90,7 +91,8 @@ func slogFieldsToAttributes(m slog.Map) []attribute.KeyValue {
9091
value = attribute.Int64Value(int64(v))
9192
// no uint32 slice method
9293
case uint64:
93-
value = attribute.Int64Value(int64(v))
94+
// If v is larger than math.MaxInt64, this will overflow, but this is acceptable for our tracing use case
95+
value = attribute.Int64Value(int64(v)) // #nosec G115 -- acceptable overflow for tracing context
9496
// no uint64 slice method
9597
case string:
9698
value = attribute.StringValue(v)

cryptorand/strings.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,20 @@ const (
4444
//
4545
//nolint:varnamelen
4646
func unbiasedModulo32(v uint32, n int32) (int32, error) {
47-
prod := uint64(v) * uint64(n)
48-
low := uint32(prod)
49-
if low < uint32(n) {
50-
thresh := uint32(-n) % uint32(n)
47+
prod := uint64(v) * uint64(n) // #nosec G115 -- uint32 to uint64 is always safe
48+
low := uint32(prod) // #nosec G115 -- truncation is intentional for the algorithm
49+
if low < uint32(n) { // #nosec G115 -- int32 to uint32 is safe for positive n (we require n > 0)
50+
thresh := uint32(-n) % uint32(n) // #nosec G115 -- int32 to uint32 after negation is an acceptable pattern here
5151
for low < thresh {
5252
err := binary.Read(rand.Reader, binary.BigEndian, &v)
5353
if err != nil {
5454
return 0, err
5555
}
56-
prod = uint64(v) * uint64(n)
57-
low = uint32(prod)
56+
prod = uint64(v) * uint64(n) // #nosec G115 -- uint32 to uint64 is always safe
57+
low = uint32(prod) // #nosec G115 -- truncation is intentional for the algorithm
5858
}
5959
}
60-
return int32(prod >> 32), nil
60+
return int32(prod >> 32), nil // #nosec G115 -- proper range is guaranteed by the algorithm
6161
}
6262

6363
// StringCharset generates a random string using the provided charset and size.
@@ -87,9 +87,10 @@ func StringCharset(charSetStr string, size int) (string, error) {
8787
r := binary.BigEndian.Uint32(entropy[:4])
8888
entropy = entropy[4:]
8989

90+
// Charset length is limited by string size, so conversion to int32 is safe
9091
ci, err := unbiasedModulo32(
9192
r,
92-
int32(len(charSet)),
93+
int32(len(charSet)), // #nosec G115 -- int to int32 is safe for charset length
9394
)
9495
if err != nil {
9596
return "", err

provisionersdk/archive.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,12 @@ func Untar(directory string, r io.Reader) error {
171171
}
172172
}
173173
case tar.TypeReg:
174-
err := os.MkdirAll(filepath.Dir(target), os.FileMode(header.Mode)|os.ModeDir|100)
174+
// header.Mode is int64, converting to os.FileMode (uint32) is safe for file permissions
175+
err := os.MkdirAll(filepath.Dir(target), os.FileMode(header.Mode)|os.ModeDir|100) // #nosec G115 -- header.Mode contains file mode bits, safely convertible to uint32
175176
if err != nil {
176177
return err
177178
}
178-
file, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.FileMode(header.Mode))
179+
file, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.FileMode(header.Mode)) // #nosec G115 -- header.Mode contains file mode bits, safely convertible to uint32
179180
if err != nil {
180181
return err
181182
}

pty/pty_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// go:build linux
1+
//go:build linux
22

33
package pty
44

pty/ssh_other.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ func applyTerminalModesToFd(logger *log.Logger, fd uintptr, req ssh.Pty) error {
105105
continue
106106
}
107107
if _, ok := tios.CC[k]; ok {
108-
tios.CC[k] = uint8(v)
108+
if v <= 255 { // Ensure value fits in uint8
109+
tios.CC[k] = uint8(v) // #nosec G115 -- value is checked to fit in uint8
110+
}
109111
continue
110112
}
111113
if _, ok := tios.Opts[k]; ok {

testutil/port.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@ func RandomPortNoListen(*testing.T) uint16 {
4141
rndMu.Lock()
4242
x := rnd.Intn(n)
4343
rndMu.Unlock()
44-
return uint16(min + x)
44+
// The calculation is safe as min(49152) + max possible x(11847) = 60999, which fits in uint16
45+
return uint16(min + x) // #nosec G115 -- range is guaranteed to be within uint16
4546
}

0 commit comments

Comments
 (0)