From 74338524a7178f8306cd0dceb9510a694e33473c Mon Sep 17 00:00:00 2001 From: zarazaex69 Date: Mon, 11 May 2026 00:56:40 +0300 Subject: [PATCH] fix: golangci errors --- internal/protect/protect_test.go | 2 +- internal/provider/jazz/peer.go | 2 +- internal/transport/seichannel/h264.go | 27 ++++++++++++------- internal/transport/seichannel/transport.go | 11 ++++---- .../seichannel/transport_unit_test.go | 16 ++++++++--- 5 files changed, 37 insertions(+), 21 deletions(-) diff --git a/internal/protect/protect_test.go b/internal/protect/protect_test.go index dc14c9b..0cecf50 100644 --- a/internal/protect/protect_test.go +++ b/internal/protect/protect_test.go @@ -101,7 +101,7 @@ func TestDialContextAndProxyDialer(t *testing.T) { accepted := make(chan struct{}, 2) go func() { - for i := 0; i < 2; i++ { + for range 2 { conn, err := ln.Accept() if err != nil { return diff --git a/internal/provider/jazz/peer.go b/internal/provider/jazz/peer.go index 323108f..63cce68 100644 --- a/internal/provider/jazz/peer.go +++ b/internal/provider/jazz/peer.go @@ -208,7 +208,7 @@ func (p *Peer) waitForReady(ctx context.Context, dcReady chan struct{}) error { case <-time.After(30 * time.Second): return provider.ErrDataChannelTimeout case <-ctx.Done(): - return fmt.Errorf("connect cancelled: %w", ctx.Err()) + return fmt.Errorf("connect canceled: %w", ctx.Err()) } } return p.waitForMediaReady(ctx, 30*time.Second) diff --git a/internal/transport/seichannel/h264.go b/internal/transport/seichannel/h264.go index b1f8892..b44e93a 100644 --- a/internal/transport/seichannel/h264.go +++ b/internal/transport/seichannel/h264.go @@ -4,19 +4,26 @@ import ( "bytes" "encoding/hex" "errors" - "fmt" "github.com/pion/webrtc/v4/pkg/media/h264reader" ) +// ErrInvalidH264Constant is returned by mustDecodeHex when a hardcoded +// constant cannot be parsed. +var ErrInvalidH264Constant = errors.New("invalid hardcoded h264 constant") + +// ErrCreateH264Reader wraps reader creation failures. +var ErrCreateH264Reader = errors.New("create h264 reader") + +const seiHeaderReserve = 8 + var ( - // ErrSEIPayloadTruncated is returned when the SEI payload is shorter than expected. + // ErrSEIPayloadTruncated is returned when the SEI payload is + // shorter than expected. ErrSEIPayloadTruncated = errors.New("sei payload truncated") // ErrSEIValueTruncated is returned when reading a SEI length-value runs past the buffer. ErrSEIValueTruncated = errors.New("sei value truncated") -) -var ( videoSEIUUID = [16]byte{ 0x5d, 0xc0, 0x3b, 0xa8, 0x45, 0x0f, @@ -38,13 +45,14 @@ func buildVideoAccessUnit(payload []byte) []byte { out = appendStartCode(out, sei) } out = appendStartCode(out, baseIDR) + return out } func extractVideoPayloads(accessUnit []byte) ([][]byte, error) { reader, err := h264reader.NewReaderWithOptions(bytes.NewReader(accessUnit), h264reader.WithIncludeSEI(true)) if err != nil { - return nil, fmt.Errorf("create h264 reader: %w", err) + return nil, errors.Join(ErrCreateH264Reader, err) } payloads := make([][]byte, 0, 1) @@ -73,7 +81,7 @@ func buildSEINAL(payload []byte) []byte { userData = append(userData, videoSEIUUID[:]...) userData = append(userData, payload...) - rbsp := make([]byte, 0, len(userData)+8) + rbsp := make([]byte, 0, len(userData)+seiHeaderReserve) rbsp = appendSEIValue(rbsp, 5) rbsp = appendSEIValue(rbsp, len(userData)) rbsp = append(rbsp, userData...) @@ -137,8 +145,7 @@ func appendSEIValue(dst []byte, value int) []byte { return append(dst, byte(value)) } -func consumeSEIValue(data []byte, pos int) (int, int, error) { - value := 0 +func consumeSEIValue(data []byte, pos int) (value, next int, err error) { for { if pos >= len(data) { return 0, pos, ErrSEIValueTruncated @@ -189,8 +196,8 @@ func unescapeRBSP(rbsp []byte) []byte { func mustDecodeHex(value string) []byte { data, err := hex.DecodeString(value) if err != nil { - // Hardcoded constants - if this fires the binary is corrupt. - panic(fmt.Sprintf("mustDecodeHex: invalid hardcoded constant %q: %v", value, err)) + //nolint:forbidigo // hardcoded constant; failure indicates a corrupt binary + panic(errors.Join(ErrInvalidH264Constant, err)) } return data } diff --git a/internal/transport/seichannel/transport.go b/internal/transport/seichannel/transport.go index 02e9c32..8a53584 100644 --- a/internal/transport/seichannel/transport.go +++ b/internal/transport/seichannel/transport.go @@ -169,7 +169,8 @@ func New(ctx context.Context, cfg transport.Config) (transport.Transport, error) batchSize: batchSize, } - if err := stream.AddTrack(track); err != nil { + err = stream.AddTrack(track) + if err != nil { return nil, fmt.Errorf("attach local video track: %w", err) } stream.SetTrackHandler(tr.handleRemoteTrack) @@ -351,9 +352,11 @@ func (p *streamTransport) writeBatch(idle []byte) bool { if i > 0 { return true } + //nolint:errcheck,gosec // best-effort idle keepalive frame _ = p.track.WriteSample(media.Sample{Data: idle, Duration: frameInterval}) return true } + //nolint:errcheck,gosec // best-effort sample write _ = p.track.WriteSample(media.Sample{Data: buildVideoAccessUnit(payload), Duration: frameInterval}) } return true @@ -512,6 +515,7 @@ func (p *streamTransport) handleInboundFrame(frame transportFrame) { } func (p *streamTransport) sendAck(seq, crc uint32) { + //nolint:dogsled,errcheck // ack delivery is best-effort _ = p.enqueueFrame(encodeAckFrame(seq, crc), true) } @@ -537,10 +541,7 @@ func fragmentPayload(data []byte, maxSize int) [][]byte { out := make([][]byte, 0, (len(data)+maxSize-1)/maxSize) for start := 0; start < len(data); start += maxSize { - end := start + maxSize - if end > len(data) { - end = len(data) - } + end := min(start+maxSize, len(data)) chunk := make([]byte, end-start) copy(chunk, data[start:end]) diff --git a/internal/transport/seichannel/transport_unit_test.go b/internal/transport/seichannel/transport_unit_test.go index 9ea68f2..0ade1fe 100644 --- a/internal/transport/seichannel/transport_unit_test.go +++ b/internal/transport/seichannel/transport_unit_test.go @@ -12,6 +12,11 @@ import ( "github.com/pion/webrtc/v4" ) +var ( + errBoom = errors.New("boom") + errOpenBoom = errors.New("open boom") +) + type fakeVideoSession struct { stream *fakeVideoStream err error @@ -67,7 +72,7 @@ func TestNewConnectCallbacksAndFeatures(t *testing.T) { return &fakeVideoSession{stream: stream}, nil }) - trIface, err := New(context.Background(), transport.Config{ + trIface, err := New(t.Context(), transport.Config{ Carrier: name, SEIFPS: 40, SEIBatchSize: 3, @@ -77,7 +82,10 @@ func TestNewConnectCallbacksAndFeatures(t *testing.T) { if err != nil { t.Fatalf("New() error = %v", err) } - tr := trIface.(*streamTransport) + tr, ok := trIface.(*streamTransport) + if !ok { + t.Fatalf("New() returned %T, want *streamTransport", trIface) + } if !stream.trackAdded || stream.trackCB == nil { t.Fatal("New() did not attach track and handler") } @@ -112,7 +120,7 @@ func TestNewConnectCallbacksAndFeatures(t *testing.T) { func TestNewErrorPaths(t *testing.T) { carrier.Register("seichannel-create-fails", func(context.Context, carrier.Config) (carrier.Session, error) { - return nil, errors.New("boom") + return nil, errBoom }) if _, err := New(context.Background(), transport.Config{Carrier: "seichannel-create-fails"}); err == nil || err.Error() != "create carrier transport: boom" { t.Fatalf("New() error = %v", err) @@ -126,7 +134,7 @@ func TestNewErrorPaths(t *testing.T) { } carrier.Register("seichannel-open-fails", func(context.Context, carrier.Config) (carrier.Session, error) { - return &fakeVideoSession{err: errors.New("open boom")}, nil + return &fakeVideoSession{err: errOpenBoom}, nil }) if _, err := New(context.Background(), transport.Config{Carrier: "seichannel-open-fails"}); err == nil || err.Error() != "open video track: open boom" { t.Fatalf("New() error = %v", err)