public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-430] libstdc++: Set _M_string_length before calling _M_dispose() [PR109703]
@ 2023-05-03 12:19 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2023-05-03 12:19 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:cbf6c7a1d16490a1e63e9a5ce00e9a5c44c4c2f2

commit r14-430-gcbf6c7a1d16490a1e63e9a5ce00e9a5c44c4c2f2
Author: Kefu Chai <kefu.chai@scylladb.com>
Date:   Mon May 1 21:24:26 2023 +0100

    libstdc++: Set _M_string_length before calling _M_dispose() [PR109703]
    
    This always sets _M_string_length in the constructor for ranges of input
    iterators, such as stream iterators.
    
    We copy from the source range to the local buffer, and then repeatedly
    reallocate a larger one if necessary. When disposing the old buffer,
    _M_is_local() is used to tell if the buffer is the local one or not (and
    so must be deallocated). In addition to comparing the buffer address
    with the local buffer, _M_is_local() has an optimization hint so that
    the compiler knows that for a string using the local buffer, there is an
    invariant that _M_string_length <= _S_local_capacity (added for PR109299
    via r13-6915-gbf78b43873b0b7).  But we failed to set _M_string_length in
    the constructor taking a pair of iterators, so the invariant might not
    hold, and __builtin_unreachable() is reached. This causes UBsan errors,
    and potentially misoptimization.
    
    To ensure the invariant holds, _M_string_length is initialized to zero
    before doing anything else, so that _M_is_local() doesn't see an
    uninitialized value.
    
    This issue only surfaces when constructing a string with a range of
    input iterator, and the uninitialized _M_string_length happens to be
    greater than _S_local_capacity, i.e., 15 for the std::string
    specialization.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/109703
            * include/bits/basic_string.h (basic_string(Iter, Iter, Alloc)):
            Initialize _M_string_length.
    
    Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>
    Co-authored-by: Jonathan Wakely <jwakely@redhat.com>

Diff:
---
 libstdc++-v3/include/bits/basic_string.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
index 8247ee6bdc6..b16b2898b62 100644
--- a/libstdc++-v3/include/bits/basic_string.h
+++ b/libstdc++-v3/include/bits/basic_string.h
@@ -760,7 +760,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
 	_GLIBCXX20_CONSTEXPR
         basic_string(_InputIterator __beg, _InputIterator __end,
 		     const _Alloc& __a = _Alloc())
-	: _M_dataplus(_M_local_data(), __a)
+	: _M_dataplus(_M_local_data(), __a), _M_string_length(0)
 	{
 #if __cplusplus >= 201103L
 	  _M_construct(__beg, __end, std::__iterator_category(__beg));

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-05-03 12:19 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-03 12:19 [gcc r14-430] libstdc++: Set _M_string_length before calling _M_dispose() [PR109703] Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).