diff --git a/service/usbip/client_assignment.go b/service/usbip/client_assignment.go index def740a36..d8836e20d 100644 --- a/service/usbip/client_assignment.go +++ b/service/usbip/client_assignment.go @@ -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 == "" { diff --git a/service/usbip/client_darwin.go b/service/usbip/client_darwin.go index 3d221c88f..749d98342 100644 --- a/service/usbip/client_darwin.go +++ b/service/usbip/client_darwin.go @@ -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 } diff --git a/service/usbip/data_protocol.go b/service/usbip/data_protocol.go index 87e8150da..5a2cd1d5c 100644 --- a/service/usbip/data_protocol.go +++ b/service/usbip/data_protocol.go @@ -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 diff --git a/service/usbip/host_darwin.go b/service/usbip/host_darwin.go index a22297704..1a55ac29f 100644 --- a/service/usbip/host_darwin.go +++ b/service/usbip/host_darwin.go @@ -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 ( diff --git a/service/usbip/host_linux.go b/service/usbip/host_linux.go index 6a3af4e46..2ffc6cbd4 100644 --- a/service/usbip/host_linux.go +++ b/service/usbip/host_linux.go @@ -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 diff --git a/service/usbip/linux_interop_test.go b/service/usbip/linux_interop_test.go index 816e44fa4..4f3dc031c 100644 --- a/service/usbip/linux_interop_test.go +++ b/service/usbip/linux_interop_test.go @@ -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) diff --git a/service/usbip/linux_test.go b/service/usbip/linux_test.go index 2a4b35f01..888248535 100644 --- a/service/usbip/linux_test.go +++ b/service/usbip/linux_test.go @@ -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) diff --git a/service/usbip/sysfs_linux.go b/service/usbip/sysfs_linux.go index 7a2501a36..1edf77a46 100644 --- a/service/usbip/sysfs_linux.go +++ b/service/usbip/sysfs_linux.go @@ -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") diff --git a/service/usbip/usbhost_darwin.go b/service/usbip/usbhost_darwin.go index 0a4fbd2c3..9230ab701 100644 --- a/service/usbip/usbhost_darwin.go +++ b/service/usbip/usbhost_darwin.go @@ -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)