fix: grow NgxString on write! instead of failing within capacity - #333
Merged
Merged
Conversation
fmt::Write was wired to append_within_capacity, so a write! into a freshly created NgxString failed on the first byte, and one into a partly filled one wrote what fit and returned an error alongside the truncated value. Use try_append, which reserves before it appends. Allocation stays checked, so a genuine failure still surfaces as fmt::Error rather than a panic, and a failed write leaves the string untouched rather than half written. Reserving up front is unaffected: try_reserve_exact does nothing when the spare capacity already covers the write, so the existing test asserting that no reallocation occurs still holds. Closes nginx#326 Signed-off-by: Y.Horie <u5.horie@gmail.com>
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.
Proposed changes
Closes #326, taking the fix you preferred there.
fn write_str(&mut self, s: &str) -> fmt::Result { - self.append_within_capacity(s).map_err(|_| fmt::Error) + self.try_append(s).map_err(|_| fmt::Error) }try_appendreserves before it appends, so allocation stays checked: a genuinefailure surfaces as
fmt::Errorrather than a panic, which is the propertyStringdoes not give. It also means a failed write leaves the stringuntouched instead of holding the part that fit.
No separate fixed-size formatter.
append_within_capacityis unchanged andstill public for anyone who wants that behaviour directly.
Testing
Two tests added:
write!into a string with no capacity reserved now produces the wholevalue, which previously failed on the first byte.
write!backed by an allocator that always refuses returns an error, doesnot panic, and leaves the string empty.
The existing
test_string_writeis untouched and still passes. It reserves upfront and asserts the buffer pointer and capacity do not change, which holds
because
try_reserve_exactdoes nothing when the spare capacity already coversthe write. So the reserve-once-then-fill pattern in
examples/shared_dict.rskeeps its single allocation.
Checklist