Skip to content

fix overlapping memcpy in string::assign#1175

Open
Ramya-9353 wants to merge 1 commit into
boostorg:developfrom
Ramya-9353:fix/string-assign-self-alias
Open

fix overlapping memcpy in string::assign#1175
Ramya-9353 wants to merge 1 commit into
boostorg:developfrom
Ramya-9353:fix/string-assign-self-alias

Conversation

@Ramya-9353

Copy link
Copy Markdown
Contributor

Summary

string::assign(char const* s, size_type count) uses
char_traits::copy (memcpy) unconditionally. When s points into
the string's own buffer — e.g. via s.assign(string_view(s).substr(3))
the source and destination regions overlap, which is undefined behaviour
per [mem.res]/[char.traits.require].

Under AddressSanitizer this fires memcpy-param-overlap; without
instrumentation it silently corrupts the result on some compilers/optimisation
levels.

Fix

Detect aliasing with detail::ptr_in_range. When the source is internal,
use char_traits::move (memmove) directly followed by impl_.term(count).
The non-aliasing path remains unchanged.

The aliasing case guarantees count <= size() <= capacity(), so no
reallocation is needed and bypassing impl_.assign is safe.

Reproducer

boost::json::string s("abcdefghijklmnopqrstuvwxyz0123456789");
s.assign(boost::json::string_view(s).substr(3));
// ASAN: memcpy-param-overlap
// Without ASAN: silent data corruption (null terminator overwrites source)

Labels

  • component: boost/json/string
  • type: memcpy-param-overlap / undefined behaviour
  • severity: medium — data corruption on common self-referencing patterns
  • affected: string::assign(char const*, size_type) and all callers
    (operator=(string_view), assign(string_view), construction from
    self-referencing string_view)

When the source pointer aliases the string's own buffer,
char_traits::copy (memcpy) is called on overlapping regions,
which is undefined behaviour.  Detect aliasing with
ptr_in_range and use char_traits::move (memmove) followed by
term() instead of delegating to impl_.assign, which would
write a null terminator that corrupts the source before the
copy.
@cppalliance-bot

Copy link
Copy Markdown

An automated preview of the documentation is available at https://1175.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-19 14:08:59 UTC

@cppalliance-bot

Copy link
Copy Markdown

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

Build time: 2026-07-19 14:22:50 UTC

@cppalliance-bot

Copy link
Copy Markdown

@codecov

codecov Bot commented Jul 19, 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 (f54d353).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##           develop    #1175   +/-   ##
========================================
  Coverage    93.91%   93.91%           
========================================
  Files           91       91           
  Lines         9265     9269    +4     
========================================
+ Hits          8701     8705    +4     
  Misses         564      564           
Files with missing lines Coverage Δ
include/boost/json/impl/string.ipp 100.00% <100.00%> (ø)

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...f54d353. Read the comment docs.

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

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.

2 participants