Skip to content

fix: don't drop drained bytes on partial reads - #81

Closed
ArniDagur wants to merge 1 commit into
rustls:mainfrom
ArniDagur:fix-drained-partial-read-truncation
Closed

ArniDagur wants to merge 1 commit into
rustls:mainfrom
ArniDagur:fix-drained-partial-read-truncation

Conversation

@ArniDagur

@ArniDagur ArniDagur commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

In KtlsStream::poll_read, let drained = &drained[*drain_index..]; shadowed the underlying Vec<u8> with the remaining unread slice:

if let Some((drain_index, drained)) = this.drained.as_mut() {
    let drained = &drained[*drain_index..];
    let len = std::cmp::min(buf.remaining(), drained.len());
    buf.put_slice(&drained[..len]);

    *drain_index += len;
    if *drain_index >= drained.len() { // <-- remaining len, not full len!
        *this.drained = None;
    }
    ...
}

As a result, once *drain_index reached half of the drained buffer across multiple partial reads (e.g., reading a 10-byte drained buffer in 3-byte chunks or 1 byte at a time), *drain_index >= remaining.len() evaluated to true, clearing this.drained and silently dropping the remaining unread bytes.

Renames the slice to remaining so *drain_index >= drained.len() compares against the full Vec length, and adds a unit test covering partial reads across multiple chunk sizes.

@ArniDagur
ArniDagur marked this pull request as draft December 3, 2026 04:29
@ArniDagur
ArniDagur force-pushed the fix-drained-partial-read-truncation branch from 82a1681 to afe5049 Compare December 3, 2026 04:32
@codecov

codecov Bot commented Dec 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.81818% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.34%. Comparing base (d9d1381) to head (afe5049).
⚠️ Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
ktls/src/ktls_stream.rs 81.81% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #81      +/-   ##
==========================================
+ Coverage   87.69%   88.34%   +0.64%     
==========================================
  Files           6        6              
  Lines         943     1064     +121     
==========================================
+ Hits          827      940     +113     
- Misses        116      124       +8     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

In `KtlsStream::poll_read`, `let drained = &drained[*drain_index..];`
shadowed the underlying `Vec<u8>` with the remaining unread slice. The
subsequent `if *drain_index >= drained.len()` check therefore compared
the cumulative read index against the remaining length instead of the
full buffer length, clearing `this.drained` and silently dropping the
remaining bytes once reads passed the midpoint.
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