]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Avoid intercepting exception in ostream::write
authorJonathan Wakely <jwakely@redhat.com>
Fri, 25 Jun 2021 17:31:23 +0000 (18:31 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 25 Jun 2021 17:47:44 +0000 (18:47 +0100)
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.

libstdc++-v3/include/bits/ostream.tcc
libstdc++-v3/include/std/ostream

index d3220e8034ba1959ca60c6338f26696f5ffd4c1b..06b2217d216133b10a713621a6db272375b67c7e 100644 (file)
@@ -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;
     }
index 981697324c9d80a4b6d69f64b72a217ec7cb619f..ddb33feb12f3cf9666791b741c5644514765fbba 100644 (file)
@@ -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
     };
 
   /**