fix string corruption in string_impl::shrink_to_fit - #1176
Conversation
|
An automated preview of the documentation is available at https://1176.json.prtest2.cppalliance.org/libs/json/doc/html/index.html If more commits are pushed to the pull request, the docs will rebuild at the same URL. 2026-07-21 16:39:00 UTC |
|
GCOVR code coverage report https://1176.json.prtest2.cppalliance.org/gcovr/index.html Build time: 2026-07-21 16:54:17 UTC |
|
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #1176 +/- ##
========================================
Coverage 93.91% 93.91%
========================================
Files 91 91
Lines 9265 9267 +2
========================================
+ Hits 8701 8703 +2
Misses 564 564
Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
|
Thank you for catching this serious issue. Merged via #1177. |

Repro: build a heap-allocated
string(size past the SBO), shrink its size to at most the SBO capacity, then callshrink_to_fit(); the contents come back as unrelated bytes. Second case: shrink a heap string that stays above the SBO capacity, then readc_str(), andstrlenexceedssize().Cause: in the heap-to-SBO branch
s_.kis set toshort_string_beforedata()is read, so once the kind has flippeddata()returns the inline SBO buffer and the memcpy copies that buffer onto itself rather than the heap characters, leaving stale union bytes (the old table pointer among them). In the heap-to-smaller-heap branch onlysize()bytes are copied, so the terminator is dropped andc_str()reads into uninitialised storage.Fix: read the source pointer before switching the kind, and copy
size() + 1bytes on the reallocating branch so the terminator is carried across.