public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [committed] libstdc++: Avoid intercepting exception in ostream::write
@ 2021-06-25 19:54 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2021-06-25 19:54 UTC (permalink / raw)
  To: libstdc++, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 929 bytes --]

Currently if ostream::write fails and sets badbit and that causes an
exception, we will catch the exception, set badbit again, and rethrow
the exception.

This change delays setting badbit until after the try-catch block, so
that if it causes an exception we don't need to catch and rethrow it.

This removes the last remaining use of _M_write, so it can be made
private (or removed entirely for versioned namespace builds, where ABI
compatibility is not required). All other uses of _M_write were replaced
by calls to __ostream_insert, so make _M_write use that too.

libstdc++-v3/ChangeLog:

	* include/bits/ostream.tcc (basic_ostream::write): Call sputn
	directly instead of using _M_write. Do setstate(__err) all
	outside the try-catch block.
	* include/std/ostream (basic_ostream::_M_write): Declare
	private. Use __ostream_insert. Do not define for the versioned
	namespace.

Tested powerpc64le-linux. Committed to trunk.


[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 3135 bytes --]

commit 4a52cf2eb9d6864ad85674ab56838e0d9ce27926
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Jun 25 18:31:23 2021

    libstdc++: Avoid intercepting exception in ostream::write
    
    Currently if ostream::write fails and sets badbit and that causes an
    exception, we will catch the exception, set badbit again, and rethrow
    the exception.
    
    This change delays setting badbit until after the try-catch block, so
    that if it causes an exception we don't need to catch and rethrow it.
    
    This removes the last remaining use of _M_write, so it can be made
    private (or removed entirely for versioned namespace builds, where ABI
    compatibility is not required). All other uses of _M_write were replaced
    by calls to __ostream_insert, so make _M_write use that too.
    
    libstdc++-v3/ChangeLog:
    
            * include/bits/ostream.tcc (basic_ostream::write): Call sputn
            directly instead of using _M_write. Do setstate(__err) all
            outside the try-catch block.
            * include/std/ostream (basic_ostream::_M_write): Declare
            private. Use __ostream_insert. Do not define for the versioned
            namespace.

diff --git a/libstdc++-v3/include/bits/ostream.tcc b/libstdc++-v3/include/bits/ostream.tcc
index d3220e8034b..06b2217d216 100644
--- a/libstdc++-v3/include/bits/ostream.tcc
+++ b/libstdc++-v3/include/bits/ostream.tcc
@@ -192,8 +192,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       sentry __cerb(*this);
       if (__cerb)
 	{
+	  ios_base::iostate __err = ios_base::goodbit;
 	  __try
-	    { _M_write(__s, __n); }
+	    {
+	      if (this->rdbuf()->sputn(__s, __n) != __n)
+		__err = ios_base::badbit;
+	    }
 	  __catch(__cxxabiv1::__forced_unwind&)
 	    {
 	      this->_M_setstate(ios_base::badbit);		
@@ -201,6 +205,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	    }
 	  __catch(...)
 	    { this->_M_setstate(ios_base::badbit); }
+	  if (__err)
+	    this->setstate(ios_base::badbit);
 	}
       return *this;
     }
diff --git a/libstdc++-v3/include/std/ostream b/libstdc++-v3/include/std/ostream
index 981697324c9..ddb33feb12f 100644
--- a/libstdc++-v3/include/std/ostream
+++ b/libstdc++-v3/include/std/ostream
@@ -308,19 +308,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       __ostream_type&
       put(char_type __c);
 
-      /**
-       *  @brief  Core write functionality, without sentry.
-       *  @param  __s  The array to insert.
-       *  @param  __n  Maximum number of characters to insert.
-      */
-      void
-      _M_write(const char_type* __s, streamsize __n)
-      {
-	const streamsize __put = this->rdbuf()->sputn(__s, __n);
-	if (__put != __n)
-	  this->setstate(ios_base::badbit);
-      }
-
       /**
        *  @brief  Character string insertion.
        *  @param  __s  The array to insert.
@@ -419,6 +406,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       template<typename _ValueT>
 	__ostream_type&
 	_M_insert(_ValueT __v);
+
+    private:
+#if !_GLIBCXX_INLINE_VERSION
+      void
+      _M_write(const char_type* __s, streamsize __n)
+      { std::__ostream_insert(*this, __s, __n); }
+#endif
     };
 
   /**

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

only message in thread, other threads:[~2021-06-25 19:54 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-25 19:54 [committed] libstdc++: Avoid intercepting exception in ostream::write 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).