[PATCH v2 3/3] libstdc++: Replace bump+rewind in basic_filebuf::overflow [PR118099]

Jonathan Wakely jwakely@redhat.com
Wed Nov 4 11:42:13 GMT 2026


The fix for PR 118099 handled failure to write the pending output
sequence to the external file (i.e. when _M_convert_to_external returns
false or throws an exception) by rewinding the last character written to
the output sequence.

This commit changes basic_filebuf::overflow to not advance the put
area's next pointer at all, so no rewinding is needed. Instead we can
just increment the size passed to _M_convert_to_external to account for
the extra character in the put area. This relies on the previous commit
which changed _M_convert_to_external to no longer use pptr() when
retrying after a partial codecvt conversion.  If _M_convert_to_external
was still looking at pptr() directly and expecting it to have been
bumped then we could not change overflow to no longer bump it.

Because this change depends on that fix in _M_convert_to_external there
is a potential ABI issue that needs care. If the new bump-less version
of basic_filebuf::overflow gets inlined into a translation unit but the
linker resolves its call to _M_convert_to_external to an old definition
that still calls pptr(), we would not transfer the overflow character to
the external file when retrying after a partial conversion. Although
this scenario seems unlikely because std::filebuf and std::wfilebuf are
both explicitly instantiated in libstdc++.so (and the explicit
instantiations use the new definition of _M_convert_to_external) it
would be safer to ensure the problem cannot occur.

This commit adds an _M_convert_to_external(const _CharT*, streamsize)
overload which takes a const pointer and moves all the logic from the
old (non-const) overload to that new one.
This ensures that the new bump-less overflow uses a new symbol
with the new behaviour, and will never bind to an old definition that
inspects pptr(). The old overload taking a non-const pointer needs to be
preserved for ABI compatibility, but can be defined to simply forward
its arguments to the new overload.

With this change, instantiations of the old version of overflow can
safely bind to either the old definition of _M_convert_to_external
(which calls pptr()) or the new definition (which forwards to the new
overload and so doesn't use pptr()) because both definitions are
compatible with the old overflow. Instantiations of the new version of
overflow will only ever bind to the new overload because it has a
different mangled name.

libstdc++-v3/ChangeLog:

	PR libstdc++/118099
	* config/abi/pre/gnu.ver: Tighten old patterns for basic_filebuf
	and export new symbols.
	* include/bits/fstream.tcc (basic_filebuf::overflow): Do not
	bump the next pointer for the overflow character. Pass const
	pointers to _M_convert_to_external.
	(_M_convert_to_external(_CharT*, streamsize)): Replace with ...
	(_M_convert_to_external(const _CharT*, streamsize)): Define new
	overload.
	* include/std/fstream (_M_convert_to_external): Declare new
	overload. Define old overload always_inline and suppress it
	for _GLIBCXX_INLINE_VERSION.
---

v2: The buffer overflow was already fixed in PATCH v2 1/3 so unlike the
v1 version of PATCH 3/3 this isn't a correctness fix now (and so is not
strictly necessary). This is just refactoring filebuf::overflow to not
need the RAII type to rewind on error. The old non-const overload of
_M_convert_to_external is now defined in the header and always_inline.

We could decide not to apply this change, because it's adding new
symbols to the library withouy fixing any bugs. I think the refactored
basic_filebuf::overflow and basic_filebuf::_M_convert_to_external code
is simpler, and removing the _Guard destructor means no unwinding is
needed if _M_convert_to_external throws, the exception can just
propagate. I think the new symbols are worth having.

Tested x86_64-linux.

 libstdc++-v3/config/abi/pre/gnu.ver   |   7 +-
 libstdc++-v3/include/bits/fstream.tcc | 112 +++++++++++---------------
 libstdc++-v3/include/std/fstream      |  14 +++-
 3 files changed, 64 insertions(+), 69 deletions(-)

diff --git a/libstdc++-v3/config/abi/pre/gnu.ver b/libstdc++-v3/config/abi/pre/gnu.ver
index 4fbb87acee3f..d846ce6d3e36 100644
--- a/libstdc++-v3/config/abi/pre/gnu.ver
+++ b/libstdc++-v3/config/abi/pre/gnu.ver
@@ -434,7 +434,9 @@ GLIBCXX_3.4 {
     _ZNSt13basic_filebufI[cw]St11char_traitsI[cw]EE15*;
     _ZNSt13basic_filebufI[cw]St11char_traitsI[cw]EE16*;
     _ZNSt13basic_filebufI[cw]St11char_traitsI[cw]EE19*;
-    _ZNSt13basic_filebufI[cw]St11char_traitsI[cw]EE2*;
+    _ZNSt13basic_filebufI[cw]St11char_traitsI[cw]EE22_M_convert_to_externalEP[cw][ilx];
+    _ZNSt13basic_filebufI[cw]St11char_traitsI[cw]EE26_M_destroy_internal_buffer*;
+    _ZNSt13basic_filebufI[cw]St11char_traitsI[cw]EE27_M_allocate_internal_buffer*;
     _ZNSt13basic_filebufI[cw]St11char_traitsI[cw]EE3*;
     _ZNSt13basic_filebufI[cw]St11char_traitsI[cw]EE4openEPKc*;
     _ZNSt13basic_filebufI[cw]St11char_traitsI[cw]EE4sync*;
@@ -2641,6 +2643,9 @@ GLIBCXX_3.4.37 {
     _ZNSt7__cxx1112basic_stringI[cw]St11char_traitsI[cw]ESaI[cw]EE*_S_allocate_*;
     _ZNSt7__cxx1112basic_stringI[cw]St11char_traitsI[cw]ESaI[cw]EE*_M_create_*;
 
+    # basic_filebuf::_M_convert_to_external(const C*, streamsize)
+    _ZNSt13basic_filebufI[cw]St11char_traitsI[cw]EE22_M_convert_to_externalEPK[cw][ilx];
+
 } GLIBCXX_3.4.36;
 
 # Symbols in the support library (libsupc++) have their own tag.
diff --git a/libstdc++-v3/include/bits/fstream.tcc b/libstdc++-v3/include/bits/fstream.tcc
index 1679cdfa6df7..203e1e7f6b28 100644
--- a/libstdc++-v3/include/bits/fstream.tcc
+++ b/libstdc++-v3/include/bits/fstream.tcc
@@ -561,26 +561,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
             }
 	  if (this->pbase() < this->pptr())
 	    {
-	      struct _Guard {
-		~_Guard() { if (_M_p) _M_p->pbump(-1); }
-		basic_filebuf* _M_p;
-	      };
-	      _Guard __g = { };
+	      const _CharT* const __pbase = this->pbase();
+	      _CharT* const __pptr = this->pptr();
+	      streamsize __n = __pptr - __pbase;
 
 	      // If appropriate, append the overflow char.
 	      if (!__testeof)
 		{
-		  *this->pptr() = traits_type::to_char_type(__c);
-		  this->pbump(1);
-		  __g._M_p = this;
+		  *__pptr = traits_type::to_char_type(__c);
+		  ++__n;
 		}
 
 	      // Convert pending sequence to external representation,
-	      // and output.
-	      if (_M_convert_to_external(this->pbase(),
-					 this->pptr() - this->pbase()))
+	      // and output. Include the overflow char if appropriate.
+	      if (_M_convert_to_external(__pbase, __n))
 		{
-		  __g._M_p = 0;
 		  _M_set_buffer(0);
 		  __ret = traits_type::not_eof(__c);
 		}
@@ -599,10 +594,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 		}
 	      __ret = traits_type::not_eof(__c);
 	    }
-	  else
+	  else // Unbuffered.
 	    {
-	      // Unbuffered.
-	      char_type __conv = traits_type::to_char_type(__c);
+	      const char_type __conv = traits_type::to_char_type(__c);
 	      if (__testeof || _M_convert_to_external(&__conv, 1))
 		{
 		  _M_writing = true;
@@ -616,62 +610,48 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _CharT, typename _Traits>
     bool
     basic_filebuf<_CharT, _Traits>::
-    _M_convert_to_external(_CharT* __ibuf, streamsize __ilen)
+    _M_convert_to_external(const _CharT* __ibuf, streamsize __ilen)
     {
-      // Sizes of external and pending output.
-      streamsize __elen;
-      streamsize __plen;
       if (__check_facet(_M_codecvt).always_noconv())
-	{
-	  __elen = _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen);
-	  __plen = __ilen;
-	}
-      else
-	{
-	  // Worst-case number of external bytes needed.
-	  // XXX Not done encoding() == -1.
-	  streamsize __blen = __ilen * _M_codecvt->max_length();
-	  char* __buf = static_cast<char*>(__builtin_alloca(__blen));
+	return _M_file.xsputn(reinterpret_cast<const char*>(__ibuf), __ilen)
+		 == __ilen;
 
-	  char* __bend;
-	  const char_type* __iend;
-	  codecvt_base::result __r;
-	  __r = _M_codecvt->out(_M_state_cur, __ibuf, __ibuf + __ilen,
-				__iend, __buf, __buf + __blen, __bend);
+      // Worst-case number of external bytes needed.
+      // XXX Not done encoding() == -1.
+      const streamsize __maxlen = __ilen * _M_codecvt->max_length();
+      char* const __buf = static_cast<char*>(__builtin_alloca(__maxlen));
+      streamsize __blen = __maxlen;
 
-	  if (__r == codecvt_base::ok || __r == codecvt_base::partial)
-	    __blen = __bend - __buf;
-	  else if (__r == codecvt_base::noconv)
-	    {
-	      // Same as the always_noconv case above.
-	      __buf = reinterpret_cast<char*>(__ibuf);
-	      __blen = __ilen;
-	    }
-	  else
-	    __throw_ios_failure(__N("basic_filebuf::_M_convert_to_external "
-				    "conversion error"));
-  
-	  __elen = _M_file.xsputn(__buf, __blen);
-	  __plen = __blen;
+      char* __bend;
+      const char_type* __iend;
+      codecvt_base::result __r;
+      __r = _M_codecvt->out(_M_state_cur, __ibuf, __ibuf + __ilen, __iend,
+			    __buf, __buf + __blen, __bend);
+      __blen = __bend - __buf;
 
-	  // Try once more for partial conversions.
-	  if (__r == codecvt_base::partial && __elen == __plen)
-	    {
-	      __r = _M_codecvt->out(_M_state_cur,
-				    __iend, __ibuf + __ilen, __iend,
-				    __buf, __buf + __blen, __bend);
-	      if (__r != codecvt_base::error)
-		{
-		  const streamsize __rlen = __bend - __buf;
-		  __elen = _M_file.xsputn(__buf, __rlen);
-		  __plen = __rlen;
-		}
-	      else
-		__throw_ios_failure(__N("basic_filebuf::_M_convert_to_external "
-					"conversion error"));
-	    }
-	}
-      return __elen == __plen;
+      if (__r == codecvt_base::ok)
+	return _M_file.xsputn(__buf, __blen) == __blen;
+      else if (__r == codecvt_base::noconv)
+	return _M_file.xsputn(reinterpret_cast<const char*>(__ibuf), __ilen)
+		 == __ilen;
+      else if (__r == codecvt_base::error)
+	__throw_ios_failure(__N("basic_filebuf::_M_convert_to_external "
+				"conversion error"));
+
+      // Conversion was partial, some output is in __buf.
+      if (_M_file.xsputn(__buf, __blen) != __blen)
+	return false;
+
+      // Try once more for partial conversions.
+      __r = _M_codecvt->out(_M_state_cur, __iend, __ibuf + __ilen, __iend,
+			    __buf, __buf + __maxlen, __bend);
+
+      if (__r == codecvt_base::error)
+	__throw_ios_failure(__N("basic_filebuf::_M_convert_to_external "
+				"conversion error"));
+
+      __blen = __bend - __buf;
+      return _M_file.xsputn(__buf, __blen) == __blen;
     }
 
   template<typename _CharT, typename _Traits>
diff --git a/libstdc++-v3/include/std/fstream b/libstdc++-v3/include/std/fstream
index 653d417c6288..cf1dfd3e2304 100644
--- a/libstdc++-v3/include/std/fstream
+++ b/libstdc++-v3/include/std/fstream
@@ -436,10 +436,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       virtual int_type
       overflow(int_type __c = _Traits::eof());
 
-      // Convert internal byte sequence to external, char-based
+      // Convert internal _CharT sequence to external, char-based
       // sequence via codecvt.
       bool
-      _M_convert_to_external(char_type*, streamsize);
+      _M_convert_to_external(const char_type*, streamsize);
+
+#if ! _GLIBCXX_INLINE_VERSION // ABI compat
+      __attribute__((__always_inline__))
+      bool
+      _M_convert_to_external(char_type* __ibuf, streamsize __ilen)
+      {
+	const char_type* __cibuf = __ibuf;
+	return _M_convert_to_external(__cibuf, __ilen);
+      }
+#endif
 
       /**
        *  @brief  Manipulates the buffer.
-- 
2.56.0



More information about the Libstdc++ mailing list