usbip: modernize loops and drop unused test helpers

Address `golangci-lint` modernize and unused warnings:
- replace map copy loops with `maps.Copy`
- collapse `if x > y { x = y }` clamps to `min`/`max`
- switch `for i := 0; i < n; i++` to `for i := range n`
- use `t.Context()` in the relay handoff test
- delete the unused `removeOnce` field and `openBinaryDevice` helper
This commit is contained in:
世界 2026-05-15 15:01:07 +08:00
parent 1720c65e50
commit 4bf7b76dbd
No known key found for this signature in database
GPG Key ID: CD109927C34A63C4
9 changed files with 20 additions and 46 deletions

View File

@ -3,6 +3,7 @@
package usbip
import (
"maps"
"sync"
"github.com/sagernet/sing-box/option"
@ -131,9 +132,7 @@ func (a *clientAssignment) matchedKeysForAssignmentLocked(entries []DeviceEntry,
return nil
}
assignmentKeys := make(map[string]DeviceKey, len(a.matchedKnownKeys)+len(entries)+len(knownKeys))
for busid, key := range a.matchedKnownKeys {
assignmentKeys[busid] = key
}
maps.Copy(assignmentKeys, a.matchedKnownKeys)
for i := range entries {
key := entryDeviceKey(entries[i])
if key.BusID == "" {

View File

@ -542,10 +542,7 @@ func (c *darwinVirtualController) completeSubmitInTransfer(ptr unsafe.Pointer, r
c.requestClose()
return -int32(unix.EOVERFLOW), 0
}
copyLength := actualLength
if copyLength > len(response.Buffer) {
copyLength = len(response.Buffer)
}
copyLength := min(actualLength, len(response.Buffer))
if copyLength > 0 && ptr != nil {
if len(response.IsoPackets) > 0 {
dst := unsafe.Slice((*byte)(ptr), requestLength)
@ -575,10 +572,7 @@ func scatterIsoInResponseBuffer(dst []byte, payload []byte, packets []IsoPacketD
cursor += length
continue
}
end := offset + length
if end > len(dst) {
end = len(dst)
}
end := min(offset+length, len(dst))
copy(dst[offset:end], payload[cursor:cursor+(end-offset)])
cursor += length
}

View File

@ -126,10 +126,7 @@ func ReadSubmitResponseBody(r io.Reader, header DataHeader, payloadDirection uin
ErrorCount: int32(binary.BigEndian.Uint32(raw[16:20])),
}
copy(response.Setup[:], raw[20:28])
bufferLength := response.ActualLength
if bufferLength < 0 {
bufferLength = 0
}
bufferLength := max(response.ActualLength, 0)
buffer, isoPackets, err := readUSBIPPayload(r, payloadDirection, bufferLength, response.NumberOfPackets, false)
if err != nil {
return SubmitResponse{}, err

View File

@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"maps"
"net"
"slices"
"sync"
@ -119,9 +120,7 @@ func (h *darwinExportHost) Reconcile(ctx context.Context, isBusy func(busid stri
h.access.Lock()
current := make(map[string]*darwinExport, len(h.exports))
for busid, exp := range h.exports {
current[busid] = exp
}
maps.Copy(current, h.exports)
h.access.Unlock()
var (

View File

@ -6,6 +6,7 @@ import (
"context"
"errors"
"fmt"
"maps"
"net"
"os"
"path/filepath"
@ -197,9 +198,7 @@ func (h *linuxExportHost) Reconcile(ctx context.Context, isBusy func(busid strin
h.access.Lock()
current := make(map[string]*linuxExport, len(h.exports))
for busid, exp := range h.exports {
current[busid] = exp
}
maps.Copy(current, h.exports)
h.access.Unlock()
for busid, device := range desired {
@ -258,7 +257,7 @@ func (h *linuxExportHost) bindOne(d *sysfsDevice) (*linuxExport, error) {
exp *linuxExport
err error
)
for attempt := 0; attempt < 2; attempt++ {
for attempt := range 2 {
exp, err = h.bindOneOnce(d)
if err == nil {
return exp, nil

View File

@ -67,14 +67,13 @@ type testVirtualFunction struct {
}
type testVirtualGadget struct {
path string
serial string
busid string
functions []testVirtualFunction
nodes map[string]string
closeOnce sync.Once
removeOnce sync.Once
udcName string
path string
serial string
busid string
functions []testVirtualFunction
nodes map[string]string
closeOnce sync.Once
udcName string
}
type testACMGadget struct {
@ -726,14 +725,6 @@ func (r *rawFile) Close() {
_ = r.file.Close()
}
func openBinaryDevice(t *testing.T, path string) *os.File {
t.Helper()
file, err := os.OpenFile(path, os.O_RDWR, 0)
require.NoError(t, err)
return file
}
func newTestVirtualGadget(t *testing.T, productID uint16, productName string, functions []testVirtualFunction) *testVirtualGadget {
t.Helper()
requireRoot(t)

View File

@ -299,9 +299,7 @@ func TestUSBIPConnHandoffRelaySocketpairCopies(t *testing.T) {
left, right := net.Pipe()
defer right.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
handoff, err := newKernelHandoffSession(ctx, opaqueConn{Conn: left}, newTestLogger(t), "test", "relay")
handoff, err := newKernelHandoffSession(t.Context(), opaqueConn{Conn: left}, newTestLogger(t), "test", "relay")
require.NoError(t, err)
defer handoff.Close()
require.NotNil(t, handoff.relayConn)

View File

@ -139,7 +139,7 @@ func readInterfaces(devicePath, busid string, configValue uint8, count int) []De
return nil
}
interfaces := make([]DeviceInterface, count)
for i := 0; i < count; i++ {
for i := range count {
name := fmt.Sprintf("%s:%d.%d", busid, configValue, i)
ipath := filepath.Join(filepath.Dir(devicePath), name)
class, _ := readHexU8(ipath, "bInterfaceClass")

View File

@ -485,10 +485,7 @@ func darwinDeviceInfoFromC(info *C.box_usbhost_device_info_t) darwinUSBHostDevic
}
copy(entry.Info.BusID[:], busid)
encodePathField(&entry.Info.Path, path, serial)
interfaceCount := int(info.interface_count)
if interfaceCount > C.BOX_USBHOST_MAX_INTERFACES {
interfaceCount = C.BOX_USBHOST_MAX_INTERFACES
}
interfaceCount := min(int(info.interface_count), C.BOX_USBHOST_MAX_INTERFACES)
if interfaceCount > 0 {
rawInterfaces := unsafe.Slice(&info.interfaces[0], interfaceCount)
entry.Interfaces = make([]DeviceInterface, interfaceCount)