Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix API breakage on Header.Unmarshal #228

Merged
merged 3 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ Michael Uti <[email protected]>
Raphael Derosso Pereira <[email protected]>
Rob Lofthouse <[email protected]>
Robin Raymond <[email protected]>
Sean <[email protected]>
Sean DuBois <[email protected]>
Sean DuBois <[email protected]>
Sean DuBois <[email protected]>
Sean DuBois <[email protected]>
Simone Gotti <[email protected]>
Steffen Vogel <[email protected]>
Expand Down
22 changes: 13 additions & 9 deletions packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ func (p Packet) String() string {
}

// Unmarshal parses the passed byte slice and stores the result in the Header.
func (h *Header) Unmarshal(buf []byte) error { //nolint:gocognit
// It returns the number of bytes read n and any error.
func (h *Header) Unmarshal(buf []byte) (n int, err error) { //nolint:gocognit
if len(buf) < headerLength {
return fmt.Errorf("%w: %d < %d", errHeaderSizeInsufficient, len(buf), headerLength)
return 0, fmt.Errorf("%w: %d < %d", errHeaderSizeInsufficient, len(buf), headerLength)
}

/*
Expand Down Expand Up @@ -112,9 +113,10 @@ func (h *Header) Unmarshal(buf []byte) error { //nolint:gocognit
h.CSRC = h.CSRC[:nCSRC]
}

n := csrcOffset + (nCSRC * csrcLength)
n = csrcOffset + (nCSRC * csrcLength)
if len(buf) < n {
return fmt.Errorf("size %d < %d: %w", len(buf), n, errHeaderSizeInsufficient)
return n, fmt.Errorf("size %d < %d: %w", len(buf), n,
errHeaderSizeInsufficient)
}

h.Marker = (buf[1] >> markerShift & markerMask) > 0
Expand All @@ -135,7 +137,7 @@ func (h *Header) Unmarshal(buf []byte) error { //nolint:gocognit

if h.Extension {
if expected := n + 4; len(buf) < expected {
return fmt.Errorf("size %d < %d: %w",
return n, fmt.Errorf("size %d < %d: %w",
len(buf), expected,
errHeaderSizeInsufficientForExtension,
)
Expand All @@ -147,7 +149,7 @@ func (h *Header) Unmarshal(buf []byte) error { //nolint:gocognit
n += 2

if expected := n + extensionLength; len(buf) < expected {
return fmt.Errorf("size %d < %d: %w",
return n, fmt.Errorf("size %d < %d: %w",
len(buf), expected,
errHeaderSizeInsufficientForExtension,
)
Expand Down Expand Up @@ -198,7 +200,8 @@ func (h *Header) Unmarshal(buf []byte) error { //nolint:gocognit

default: // RFC3550 Extension
if len(buf) < n+extensionLength {
return fmt.Errorf("%w: %d < %d", errHeaderSizeInsufficientForExtension, len(buf), n+extensionLength)
return n, fmt.Errorf("%w: %d < %d",
errHeaderSizeInsufficientForExtension, len(buf), n+extensionLength)
}

extension := Extension{id: 0, payload: buf[n : n+extensionLength]}
Expand All @@ -209,12 +212,13 @@ func (h *Header) Unmarshal(buf []byte) error { //nolint:gocognit

h.PayloadOffset = n

return nil
return n, nil
}

// Unmarshal parses the passed byte slice and stores the result in the Packet.
func (p *Packet) Unmarshal(buf []byte) error {
if err := p.Header.Unmarshal(buf); err != nil {
_, err := p.Header.Unmarshal(buf)
if err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ func TestUnmarshal_ErrorHandling(t *testing.T) {
testCase := testCase
t.Run(name, func(t *testing.T) {
h := &Header{}
err := h.Unmarshal(testCase.input)
_, err := h.Unmarshal(testCase.input)
if !errors.Is(err, testCase.err) {
t.Errorf("Expected error: %v, got: %v", testCase.err, err)
}
Expand Down