Repository navigation
rtpklv: do not reuse buffer of returned KLV units - #1154
Merged
Merged
Conversation
Decode() returns a slice that points to the decoder's internal buffer, and the next call to Decode() overwrites that same array in place. A caller that holds the previous unit - for instance while it is being recorded or forwarded on another goroutine - ends up reading the bytes of the next unit, or a torn mix of both when the sizes differ. Clear the buffer instead of truncating it, so each unit gets its own array. This is what the h264, h265, av1, vp8 and mpeg1video decoders already do, at the cost of one allocation per unit.
Member
|
verified and merged, thanks! |
aler9
enabled auto-merge (squash)
September 5, 2026 06:53
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1154 +/- ##
==========================================
- Coverage 87.17% 87.15% -0.02%
==========================================
Files 133 133
Lines 10587 10587
==========================================
- Hits 9229 9227 -2
- Misses 1358 1360 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Decode()returns a slice that points into the decoder's internal buffer, and the next call toDecode()overwrites that same array in place (reset()truncates it to zero length, then the next unit is appended into it).A caller that still holds the previous unit therefore ends up reading the bytes of the next one, or a torn mix of the two when the sizes differ. In mediamtx the decoded unit is handed to readers through a ring buffer and consumed on a different goroutine (recorder, HLS, MPEG-TS, WebRTC), so this is also a data race.
rtpklv is the only decoder under pkg/format that returns its own reusable state. The others either build a fresh slice in joinFragments() or clear the buffer, with the comment "do not reuse frameBuffer to avoid race conditions" in rtph264, rtph265, rtpav1, rtpvp8 and rtpmpeg1video.
The fix clears the buffer instead of truncating it, so every unit gets its own array. It costs one allocation per KLV unit, the same tradeoff the other decoders already make. Tests cover a following unit of the same size and a shorter one.