Skip to content

Handle short reads when matching reader headers - #167

Open
omrzk wants to merge 1 commit into
h2non:masterfrom
omrzk:fix/fragmented-reader-header
Open

omrzk wants to merge 1 commit into
h2non:masterfrom
omrzk:fix/fragmented-reader-header

Conversation

@omrzk

@omrzk omrzk commented Oct 30, 2026

Copy link
Copy Markdown

MatchReader assumes one Read fills its header buffer. A valid PNG or JPEG supplied through iotest.OneByteReader therefore returns unknown, while a three-byte ICO prefix is accepted after zero padding supplies a missing signature byte.

Read at most 8192 bytes across short reads and pass only the received bytes to Match. EOF after a short header is accepted; empty input now returns ErrEmptyBuffer, consistently with Match. Other read errors propagate. The reader remains consumed; an unclosed stream may wait for the full prefix or EOF.

Validation: the new fragmented-header, truncated-ICO, empty-input, read-limit and read-error regressions fail on current master. All tests pass with Go 1.16.15 and with Go 1.26.10 under the race detector (five repetitions, vet disabled). Formatting and git diff --check pass. Go 1.26 vet's existing sync.Map copy diagnostic in filetype.go:11 was already reproduced on this unchanged master in the previous contribution; it is outside this patch. Hosted Go 1.19–1.21 CI has not run locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant