Repository navigation
Conversation
Compact decided whether to merge the next buffer by comparing the total length with Size, but the bytes are copied after the end of the previous buffer. When that buffer had been advanced (SplitSize, partial Read, header stripping) or wraps a byte slice (FromBytes), the room after its end is smaller than Size - Len(), so ReadFrom copied only part of the next buffer and the rest was released and lost. Check the room after the end instead.
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.
buf.Compactcan silently drop bytes. It is called on the write path of the TLS, WebSocket, HTTPUpgrade and QUIC transports (WriteMultiBuffer), so the dropped bytes are missing from the stream sent to the peer.Compact merges the next buffer into the previous one when their total length fits in
Size:v2ray-core/common/buf/multi_buffer.go
Lines 147 to 152 in ec5ce8b
The bytes are copied with
Buffer.ReadFrom, which writes afterendand copies at most the room left there in oneRead:v2ray-core/common/buf/buffer.go
Lines 253 to 257 in ec5ce8b
Len() + curr.Len() <= Sizedoes not mean the next buffer fits afterend:startis not 0 (afterAdvance, e.g. inSplitSize, a partialRead, or header stripping in a protocol reader) has onlySize - endbytes of room;FromByteshas no room at all.In both cases only part of
curris copied, thencurris released, so the rest is lost. For example, a buffer withstart=1000, end=1500followed by a 1000-byte buffer: 500 + 1000 <= 2048, but only 548 bytes fit, so 452 bytes are dropped.The fix checks the room after
end(last.end+curr.Len() > last.Cap()) instead of the total length. For a fresh buffer (start == 0, capacitySize) the result is the same as before.Tested with a new
TestCompactBufferWithoutRoom(advanced buffer andFromBytesbuffer):unexpected Compact result length 1048, want 1500andlength 2, want 4go test ./common/... ./transport/internet/{tls,websocket,httpupgrade,quic}/...passes, golangci-lint with.github/linters/.golangci.ymlreports nothing new.