fix(middleware): gzip Write must return len(b), not the buffer length#2981
Open
ultramcu wants to merge 1 commit into
Open
fix(middleware): gzip Write must return len(b), not the buffer length#2981ultramcu wants to merge 1 commit into
ultramcu wants to merge 1 commit into
Conversation
Contributor
|
please rebase your PR |
gzipResponseWriter.Write returned the total buffered byte count (any previously buffered chunks plus the current slice) once a write crossed MinLength, so it could return n > len(b). That violates the io.Writer contract and makes standard-library consumers that validate the count panic -- notably io.Copy (used by Context.Stream), which panics with "bytes.Reader.WriteTo: invalid Write count". Flush the buffer as before but report only len(b) as written.
8d83429 to
630e123
Compare
Author
|
Thanks @aldas — done. I retargeted the PR to the |
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.
Issue
middleware.gzipResponseWriter.Writeviolates theio.Writercontract. When a buffered write crossesMinLength, it flushes the whole buffer and returns the buffer length:Because the buffer also holds bytes from earlier (sub-
MinLength) writes, the returnedncan exceedlen(b). Theio.Writercontract requires0 <= n <= len(b), and consumers that validate this panic.In particular
io.Copy— whichecho.Context#Streamuses internally (io.Copy(c.Response(), r)) — panics with:So streaming a response through the Gzip middleware crashes the request whenever a small write was buffered before the threshold-crossing write.
Fix
Flush the buffer as before, but report only
len(b)(the bytes accepted from this call):nis alreadylen(b)from the precedingw.buffer.Write(b).Tests
Added
middleware/compress_writecount_test.go:TestGzipWriteReturnsCorrectCount—Writereportslen(b), never the buffered total.TestGzipIoCopyDoesNotPanic— streaming through gzip withio.Copyno longer panics.Both fail on
master(the second panics withinvalid Write count) and pass with the fix. Existing compress tests (incl.TestGzipWithMinLengthChunked) and the full suite stay green undergo test -race ./....