Fix off-by-two padding bug in set_string and empty string edge case#586
Open
gijzelaerr wants to merge 1 commit intomasterfrom
Open
Fix off-by-two padding bug in set_string and empty string edge case#586gijzelaerr wants to merge 1 commit intomasterfrom
gijzelaerr wants to merge 1 commit intomasterfrom
Conversation
set_string had an off-by-two error in the padding loop: the range used `bytearray_[byte_index] - 2` instead of `bytearray_[byte_index]`. The -2 subtraction was incorrect because the 2-byte header offset is already handled by the `byte_index + 2 + r` indexing expression. This left the last 2 character positions unpadded with stale data when writing a shorter string over a longer one. Also fix an empty string edge case in both set_string and set_fstring where the first character position would not be cleared because the enumerate loop wouldn't execute and `i` would remain at its initial value of 0. Now use `len(value)` as the range start instead. Fixes #479 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.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.
Summary
Fixes #479
set_string()left the last 2 character positions unpadded when writing a shorter string over a longer one, leaving stale data in the buffer. Additionally, bothset_string()andset_fstring()failed to clear the first character position when writing an empty string.Root cause analysis
Bug 1: Off-by-two in
set_stringpadding (line 247)The
- 2was an incorrect attempt to account for the 2-byte string header (max_size byte + length byte). However, the header offset is already handled by thebyte_index + 2 + rindexing expression. The variableris a relative index into the data area (positions 0 through max_size-1), so the range should go up tomax_size, notmax_size - 2.Impact: When writing
"ab"into a buffer withmax_size=10, positions 8 and 9 (relative to the data area) were never cleared:Bug 2: Empty string edge case (both
set_stringandset_fstring)When
valueis empty, theenumerateloop doesn't execute, soistays at0. The padding loop then starts fromrange(0 + 1, ...), skipping position 0 entirely.For
set_string, this bug was masked byget_string()which reads the length byte (0 for empty) and returns""regardless. But forset_fstring, the stale character at position 0 would be visible throughget_fstring().Fix
range(i + 1, bytearray_[byte_index] - 2)torange(len(value), bytearray_[byte_index])inset_stringrange(i + 1, max_length)torange(len(value), max_length)inset_fstringUsing
len(value)instead ofi + 1correctly handles both the normal case and the empty string edge case, and removing- 2fixes the off-by-two.Test plan
"ab"with max_size=10🤖 Generated with Claude Code