fix overlapping memcpy in string::assign#1175
Conversation
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.
|
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 |
|
GCOVR code coverage report https://1175.json.prtest2.cppalliance.org/gcovr/index.html Build time: 2026-07-19 14:22:50 UTC |
|
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #1175 +/- ##
========================================
Coverage 93.91% 93.91%
========================================
Files 91 91
Lines 9265 9269 +4
========================================
+ Hits 8701 8705 +4
Misses 564 564
Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|

Summary
string::assign(char const* s, size_type count)useschar_traits::copy(memcpy) unconditionally. Whenspoints intothe 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; withoutinstrumentation 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 byimpl_.term(count).The non-aliasing path remains unchanged.
The aliasing case guarantees
count <= size() <= capacity(), so noreallocation is needed and bypassing
impl_.assignis safe.Reproducer
Labels
boost/json/stringmemcpy-param-overlap/ undefined behaviourstring::assign(char const*, size_type)and all callers(
operator=(string_view),assign(string_view), construction fromself-referencing
string_view)