Skip to content

fix string corruption in string_impl::shrink_to_fit - #1176

Closed
Ramya-9353 wants to merge 1 commit into
boostorg:developfrom
Ramya-9353:string-shrink-to-fit
Closed

fix string corruption in string_impl::shrink_to_fit#1176
Ramya-9353 wants to merge 1 commit into
boostorg:developfrom
Ramya-9353:string-shrink-to-fit

Conversation

@Ramya-9353

Copy link
Copy Markdown
Contributor

Repro: build a heap-allocated string (size past the SBO), shrink its size to at most the SBO capacity, then call shrink_to_fit(); the contents come back as unrelated bytes. Second case: shrink a heap string that stays above the SBO capacity, then read c_str(), and strlen exceeds size().

Cause: in the heap-to-SBO branch s_.k is set to short_string_ before data() is read, so once the kind has flipped data() 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 only size() bytes are copied, so the terminator is dropped and c_str() reads into uninitialised storage.

Fix: read the source pointer before switching the kind, and copy size() + 1 bytes on the reallocating branch so the terminator is carried across.

@cppalliance-bot

Copy link
Copy Markdown

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

@cppalliance-bot

Copy link
Copy Markdown

GCOVR code coverage report https://1176.json.prtest2.cppalliance.org/gcovr/index.html
LCOV code coverage report https://1176.json.prtest2.cppalliance.org/genhtml/index.html
Coverage Diff Report https://1176.json.prtest2.cppalliance.org/diff-report/index.html

Build time: 2026-07-21 16:54:17 UTC

@cppalliance-bot

Copy link
Copy Markdown

@codecov

codecov Bot commented Jul 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.91%. Comparing base (ea90668) to head (dca5076).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##           develop    #1176   +/-   ##
========================================
  Coverage    93.91%   93.91%           
========================================
  Files           91       91           
  Lines         9265     9267    +2     
========================================
+ Hits          8701     8703    +2     
  Misses         564      564           
Files with missing lines Coverage Δ
include/boost/json/detail/impl/string_impl.ipp 99.11% <100.00%> (+<0.01%) ⬆️

Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ea90668...dca5076. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@grisumbras

Copy link
Copy Markdown
Member

Thank you for catching this serious issue. Merged via #1177.

@grisumbras grisumbras closed this Jul 23, 2026
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.

3 participants