Skip to content

fix: compute actual string length in set_owning heap path - #277

Open
a2Fsa2k wants to merge 1 commit into
felixguendling:masterfrom
a2Fsa2k:fix/set-owning-size
Open

fix: compute actual string length in set_owning heap path#277
a2Fsa2k wants to merge 1 commit into
felixguendling:masterfrom
a2Fsa2k:fix/set-owning-size

Conversation

@a2Fsa2k

@a2Fsa2k a2Fsa2k commented Jun 15, 2026

Copy link
Copy Markdown

When set_owning(str, len) is called with a len larger than the actual null-terminated length of str, and the allocation falls on the heap path (len > short_length_limit), h_.size_ was set to the full len rather than the actual string length. This causes size() to report incorrect values.

Example: set_owning("", 32) would report size() == 32 instead of size() == 0 on the heap path, inconsistent with the short string path which correctly returns 0.

The fix computes the actual string length via std::char_traits<CharT>::length(str) and uses min(actual_length, len) for h_.size_.

Fixes #190.

When set_owning(str, len) is called with a len larger than the
actual null-terminated length of str, and the allocation falls on
the heap path (len > short_length_limit), h_.size_ was set to len
rather than the actual string length. This caused size() to report
incorrect values for heap strings shorter than their allocated capacity.
@a2Fsa2k

a2Fsa2k commented Jun 15, 2026

Copy link
Copy Markdown
Author

Note: the CI build failures (in vector_test.cc and nvec_test.cc) are pre-existing on the master branch since the add free_self_allocated commit on April 5, 2026. They are unrelated to this change — my fix only modifies set_owning() in string.h.

@felixguendling whenever you get a chance to review, the fix addresses the inconsistent size() reported by set_owning() when the allocated capacity exceeds the actual string length on the heap path (issue #190).

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.

String gives different size at "empty" input for short/non_short

1 participant