Skip to content

fix: WriteTo empty buffer handling - #182

Open
mymmrac wants to merge 6 commits into
pires:mainfrom
mymmrac:fix/write-to-empty-buffer
Open

fix: WriteTo empty buffer handling#182
mymmrac wants to merge 6 commits into
pires:mainfrom
mymmrac:fix/write-to-empty-buffer

Conversation

@mymmrac

@mymmrac mymmrac commented Jul 30, 2026

Copy link
Copy Markdown

Added check for buffer size and cleaning of the buffer after reading.

This PR closes #181

@pires pires left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thank you for your contribution.

IMO we should take the buffer-clearing optimization but do not discard an empty bufio.Reader without accounting for its pending error state. Also, and pretty please, add tests that assert both memory cleanup and preservation of underlying read errors.

Comment thread protocol.go
}

// If the buffer has been drained, copy directly from conn.
if p.bufReader.Buffered() == 0 {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

IIUC Buffered() == 0 only means no bytes remain; the reader can still hold a pending error from an underlying Read that returned (n > 0, err). Clearing bufReader and copying directly from conn discards that error.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the comment, will try to find time to fix it over the weekends

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@pires I added check for pending errors and tests that cover this case

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 97.337% (+0.02%) from 97.32% — mymmrac:fix/write-to-empty-buffer into pires:main

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.

Read into empty buffer in Conn.WriteTo

3 participants