This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] PR libstdc++/81338 correctly manage string capacity


This fixes two problems with basic_stringbuf when using SSO strings.

Firstly, overflow() won't try to use excess capacity in the string
(which is present on construction, due to the SSO buffer, which can't
be utilised immediately due to LWG 2995, see PR 80676). That causes
overflow to allocate a new buffer on the heap. If the stringbuf is
reset (by move-assignment) and then assigned to again, it will try to
reallocate an even bigger buffer, despite having one already. The
testcase in PR 81338 keeps doing that until it gets bad_alloc. That's
fixed by checking for unused capacity in overflow.

Secondly, move assignment of std::string objects was not copying the
entire buffer sequence, only the first _M_string.length() characters.
This is because basic_stringbuf writes directly into a string's unused
capacity, without modifying the string's length. The solution is to
update the string length before move operations, to make it match the
longer of the get and put sequences.

	PR libstdc++/81338
	* include/bits/basic_string.h [_GLIBCXX_USE_CXX11_ABI] (basic_string):
	Declare basic_stringbuf to be a friend.
	* include/bits/sstream.tcc (basic_stringbuf::overflow)
	[_GLIBCXX_USE_CXX11_ABI]: Use unused capacity before reallocating.
	* include/std/sstream (basic_stringbuf::__xfer_bufptrs): Update string
	length to buffer length.
	* testsuite/27_io/basic_stringstream/assign/81338.cc: New.

Tested powerpc64le-linux, committed to trunk.

Attachment: patch.txt
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]