fix(read): handle dynamic-buffer max_size without throwing (#318)#340
Open
mvandeberg wants to merge 1 commit into
Open
fix(read): handle dynamic-buffer max_size without throwing (#318)#340mvandeberg wants to merge 1 commit into
mvandeberg wants to merge 1 commit into
Conversation
…ce#318) The read dynamic-buffer overloads passed the (grown) amount straight to dynbuf.prepare(), so when it exceeded the remaining capacity (e.g. the default initial_amount of 2048 into a bounded buffer) prepare() threw std::invalid_argument / std::length_error. This was inconsistent with read_until, which already clamps. Clamp the prepared amount to max_size() - size() and treat reaching max_size() as a successful completion. The existing eof-as-success semantics are unchanged, and read_until keeps returning error::not_found at max_size(). The clamp targets the contract that prepare(n) accepts any n with size() + n <= max_size(): string/vector grow and circular wraps to honor it. A fixed-capacity buffer may, but need not, compact; a non-compacting flat_dynamic_buffer reused after a partial consume has capacity() < max_size() - size(), so prepare throws. This is documented as a limitation — such buffers must be passed without a consumed prefix. Clamping to capacity() instead is not viable: it spins growable buffers. Also floor the prepared amount to 1 so initial_amount == 0 cannot spin, and factor the clamp into detail::read_prepare_amount so the two overloads cannot drift. Tests cover bounded string/circular/flat buffers, the flat consumed-prefix throw, and buffer contents.
|
An automated preview of the documentation is available at https://340.capy.prtest3.cppalliance.org/index.html If more commits are pushed to the pull request, the docs will rebuild at the same URL. 2026-06-25 20:28:05 UTC |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop-2 #340 +/- ##
============================================
Coverage ? 98.39%
============================================
Files ? 83
Lines ? 4237
Branches ? 0
============================================
Hits ? 4169
Misses ? 68
Partials ? 0
Flags with carried forward coverage won't be shown. Click here to find out more. Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
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.
The read dynamic-buffer overloads passed the (grown) amount straight to dynbuf.prepare(), so when it exceeded the remaining capacity (e.g. the default initial_amount of 2048 into a bounded buffer) prepare() threw std::invalid_argument / std::length_error. This was inconsistent with read_until, which already clamps.
Clamp the prepared amount to max_size() - size() and treat reaching max_size() as a successful completion. The existing eof-as-success semantics are unchanged, and read_until keeps returning error::not_found at max_size().
The clamp targets the contract that prepare(n) accepts any n with size() + n <= max_size(): string/vector grow and circular wraps to honor it. A fixed-capacity buffer may, but need not, compact; a non-compacting flat_dynamic_buffer reused after a partial consume has capacity() < max_size() - size(), so prepare throws. This is documented as a limitation — such buffers must be passed without a consumed prefix. Clamping to capacity() instead is not viable: it spins growable buffers.
Also floor the prepared amount to 1 so initial_amount == 0 cannot spin, and factor the clamp into detail::read_prepare_amount so the two overloads cannot drift.
Tests cover bounded string/circular/flat buffers, the flat consumed-prefix throw, and buffer contents.
Closes #318