Skip to content

Comments

protocol: safely resize header buffer based on length field#150

Closed
pires wants to merge 1 commit intomainfrom
pires/feat/dinamically_grow_header_buffer
Closed

protocol: safely resize header buffer based on length field#150
pires wants to merge 1 commit intomainfrom
pires/feat/dinamically_grow_header_buffer

Conversation

@pires
Copy link
Owner

@pires pires commented Feb 4, 2026

Fixes #123
Refs #144

WDYT @emersion @bollenberger?

@coveralls
Copy link

coveralls commented Feb 4, 2026

Coverage Status

coverage: 95.174% (+0.1%) from 95.03%
when pulling dd43193 on pires/feat/dinamically_grow_header_buffer
into 1ddde3c on main.

@pires pires force-pushed the pires/feat/dinamically_grow_header_buffer branch from 5a48d71 to a8ba414 Compare February 4, 2026 13:35
// If the header is larger than our current buffer, wrap the existing
// reader in a larger one. This preserves any bytes already buffered.
if totalSize > p.bufReader.Size() {
p.bufReader = bufio.NewReaderSize(p.bufReader, totalSize)
Copy link
Contributor

@emersion emersion Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Manually resizing the buffer seems more complicated than necessary: I don't think we need to do that. See #155 for a simpler version.

This also results in paying the memory cost for the TLVs twice: once for the internal bufio.Reader buffer, and another time when reading TLVs as a byte slice.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More generally, bufio.Reader's internal buffer size is not supposed to be client-controlled. It's supposed to act as a way to avoid large allocations.

if totalSize, ok := v2PreflightHeaderSize(peeked); ok {
// If the header is larger than our current buffer, wrap the existing
// reader in a larger one. This preserves any bytes already buffered.
if totalSize > p.bufReader.Size() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can result in a DoS if the client sends 65535 as the length.

@pires
Copy link
Owner Author

pires commented Feb 5, 2026

Closing in favor of #155.

@pires pires closed this Feb 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants